ASP.NET成员资格-在同一Web应用程序中调用Web服务时,经过身份验证的用户是否丢失
我正在使用ASP.NET登录控件进行身份验证 我有一些用户,他们能够成功登录。经过身份验证后,我重定向到helloworld.aspx页面。在Page_Load方法中,我首先调用Membership.GetUser()。这将正确返回经过身份验证的用户。然后调用驻留在同一web应用程序中的简单WCF web服务。我的WebService调用的第一行具有相同的成员资格。GetUser()。不过这次它返回NULL 有什么想法吗 谢谢, 贾斯汀 下面是一些代码片段 JustinPage.aspxASP.NET成员资格-在同一Web应用程序中调用Web服务时,经过身份验证的用户是否丢失,asp.net,wcf,web-services,asp.net-membership,forms-authentication,Asp.net,Wcf,Web Services,Asp.net Membership,Forms Authentication,我正在使用ASP.NET登录控件进行身份验证 我有一些用户,他们能够成功登录。经过身份验证后,我重定向到helloworld.aspx页面。在Page_Load方法中,我首先调用Membership.GetUser()。这将正确返回经过身份验证的用户。然后调用驻留在同一web应用程序中的简单WCF web服务。我的WebService调用的第一行具有相同的成员资格。GetUser()。不过这次它返回NULL 有什么想法吗 谢谢, 贾斯汀 下面是一些代码片段 JustinPage.aspx pub
public partial class JustinPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MembershipUser user = Membership.GetUser();
// user is a valid user
JustinService.JustinTestServiceClient justin = new CMS.WEB.JustinService.JustinTestServiceClient();
justin.DoWork();
}
}
JustinTestService.svc.cs
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class JustinTestService
{
[OperationContract]
public void DoWork()
{
MembershipUser user = Membership.GetUser();
// user is NULL ??? Why?
return;
}
}
正如前面提到的,服务源代码与Justin.aspx位于同一个WebApplication中,正如端点所看到的(注意,我的应用程序固定在端口19003上)
端点地址=”http://localhost:19003/Services/JustinTestService.svc"
binding=“basicHttpBinding”bindingConfiguration=“basicHttpBinding\u JustinTestService”
contract=“JustinService.JustinTestService”name=“BasicHttpBinding\u JustinTestService”/
而且绑定看起来像这样
可能与???有关。问题在于web服务调用不是从浏览器发起的,用户在浏览器中进行了身份验证。相反,您是从应用程序发起web服务调用(您的web服务器正在向您的web服务器创建HTTP请求!)。获取fiddler并查看验证cookie是否通过网络发送 如果不是,您可能需要将其打包到服务请求中 像这样的
Service1Client ws = new Service1Client(); // Name of webclient proxy
using (OperationContextScope scope = new OperationContextScope(ws.InnerChannel))
{
HttpRequestMessageProperty httpRequest = new HttpRequestMessageProperty();
OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, httpRequest);
HttpCookieCollection cc = Page.Request.Cookies;
if (Request.Cookies[".ASPXAUTH"] != null)
{
HttpCookie aCookie = Request.Cookies[".ASPXAUTH"];
String authcookieValue = Server.HtmlEncode(aCookie.Value);
httpRequest.Headers.Add("Cookie: " + ".ASPXAUTH=" + authcookieValue);
}
// Make call to webservice here
ws.MyWCFCall();
HttpResponseMessageProperty response = (HttpResponseMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpResponseMessageProperty.Name];
}
人力资源管理。。。好的,我同意。我之所以有意这么做,是因为我想创建一个所有客户都可以沟通的通用合同。我不想仅仅因为这个WebApp驻留在同一个应用程序中,就授予它对业务层的特殊访问权限。所有客户端应用程序都应该通过相同的API。那么,建议的解决方案是直接呼叫服务吗?我已经知道这是可行的,但我试图保持一致,可能是错误的。我假设您可以获取当前登录用户的凭据并将其发送到web服务,但我不确定这是否安全。您的web服务是对外开放的还是只对内开放的?