【问题标题】:ASP.NET Authentification fails with IE 10使用 IE 10 的 ASP.NET 身份验证失败
【发布时间】:2013-03-21 21:32:58
【问题描述】:

今天我遇到了一个奇怪的问题。 我的网站在 .NET 4 下的应用程序池上运行,今天我们注意到它无法在 IE 10 下对用户进行身份验证(使用其他浏览器一切正常)。这是引发的异常:

“/”应用程序中的服务器错误。

无法转换类型的对象 'System.Security.Principal.GenericIdentity' 键入 'System.Web.Security.FormsIdentity'。描述:一个未处理的 执行当前 Web 请求期间发生异常。 请查看堆栈跟踪以获取有关错误的更多信息和 它起源于代码的地方。

异常详细信息:System.InvalidCastException:无法转换对象 要键入的“System.Security.Principal.GenericIdentity”类型 'System.Web.Security.FormsIdentity'。

但将 .NET 4 升级到 4.5 版后,错误消失了。奇怪的是,我们并没有更改应用程序池上 .NET 的版本,它仍然是 .NET 4。

顺便说一下,我使用的是自定义主体,并且我正在将 userData 附加到 AuthenticationTicket。 这是我来自 Global.asax 的代码:

protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
    {
        HttpCookie authCooke = Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCooke != null)
        {
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCooke.Value);
            if (authTicket != null)
            {
                var identity = new GenericIdentity(authTicket.Name, "Forms");
                var principal = new CustomPrincipal(identity);

                string userData = ((FormsIdentity)(Context.User.Identity)).Ticket.UserData;
                var serializer = new JavaScriptSerializer();
                principal.User = (User)serializer.Deserialize(userData, typeof(User));

                Context.User = principal;
                Thread.CurrentPrincipal = principal;
            }
        }
    }

谁能向我解释我做错了什么以及在不更改应用程序池的情况下更新 .NET 版本会如何影响网站?

【问题讨论】:

    标签: asp.net .net asp.net-mvc iis internet-explorer-10


    【解决方案1】:

    我的网站上有类似的问题。身份验证仅在 IE10 中失败,但在 Firefox 和 IE8、IE9 中可以正常工作。 为了解决这个问题,我在 web.config 中添加了参数 cookieless="UseCookies"

    <authentication mode="Forms" >
      <forms loginUrl="~/Account/LogOn" timeout="2880" cookieless="UseCookies" />
    </authentication>
    

    【讨论】:

    【解决方案2】:

    可能是由于您的代码拼写错误(HttpCookie authCooke)。应该是“authCookie”吗?

    【讨论】:

    • -1 authCooke 只是一个变量名,虽然它可能应该authCookie,但这无关紧要。如果 OP 愿意,它可以被称为 fred
    猜你喜欢
    • 1970-01-01
    • 2018-11-02
    • 2021-06-19
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    相关资源
    最近更新 更多