我使用ASP.NET Ajax从ASP.NET页面调用WCF。我正在使用表单身份验证来保护网站。使用ASP.NET Ajax通过SSL调用WCF
一切都如预期一样工作,直到它被部署到生产服务器上,然后我开始收到JavaScript错误,因为找不到服务。生产服务器使用SSL,所以我说下面我的web.config:
<webHttpBinding>
<binding name="webBinding">
<security mode="Transport" />
</binding>
</webHttpBinding>
该停止了JavaScript错误的发生,但现在WCF服务不表现为用它来。
将安全设置为从ASP.NET Ajax传输到WCF服务的调用将执行我的Global.asax中的Application_AuthenticateRequest
。这将基于Forms Authentication票据在HttpContext.Current.User
上设置自定义IPrinciple
。我的WCF服务的构造函数设置为Thread.CurrentPrinciple = HttpContext.Current.User
,因此我的服务可以访问在Application_AuthenticateRequest
期间设置的IPrinciple
。
将安全更改为传输后,它似乎不会通过Application_AuthenticateRequest
运行,因为我的Thread.CurrentPrinciple
不是我的自定义IPrinciple
。
有谁知道我可以像使用Transport之后使用Transport一样获得相同的行为吗?
我的网页上使用以下引用WCF服务:
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="
<Services>
<asp:ServiceReference Path="~/Services/MyService.svc" />
</Services>
</asp:ScriptManagerProxy>
代码在我Application_AuthenticateRequest
使用:
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
String cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
if (authCookie == null)
{
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch
{
return;
}
if (authTicket == null)
{
return;
}
HttpContext.Current.User = new CustomPrinciple(authTicket.Name);
}