【发布时间】:2009-01-23 05:35:56
【问题描述】:
我正在使用 ASP.NET Ajax 从 ASP.NET 页面调用 WCF。我正在使用表单身份验证来保护网站。
在开发过程中一切都按预期工作,直到它被部署到生产服务器上,然后我开始收到 JavaScript 错误,因为找不到服务。生产服务器使用 SSL,所以我在 web.config 中添加了以下内容:
<webHttpBinding>
<binding name="webBinding">
<security mode="Transport" />
</binding>
</webHttpBinding>
这阻止了 JavaScript 错误的发生,但现在 WCF 服务的行为不再像以前那样。
在将安全设置为传输之前,从 ASP.NET Ajax 调用 WCF 服务将在我的 Global.asax 中执行 Application_AuthenticateRequest。这将根据表单身份验证票在HttpContext.Current.User 上设置自定义IPrinciple。我的 WCF 服务的构造函数设置为 Thread.CurrentPrinciple = HttpContext.Current.User,因此我的服务可以访问在 Application_AuthenticateRequest 期间设置的 IPrinciple。
将安全性更改为 Transport 后,它似乎没有通过 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);
}
【问题讨论】:
标签: wcf asp.net-ajax web-config forms-authentication