FormsAuthentication authCookie仅对某些用户为空

mba*_*ema 5 c# asp.net forms-authentication

我遇到了一个asp.net表单身份验证的奇怪问题.此问题仅发生在已成功登录的30多个用户中的3个用户.我使用的是非常基本的身份验证代码,我已多次使用并且从未见过这个问题.用户成功进行身份验证并创建auth cookie后,将调用cookie,并调用response.redirect到FormsAuthentication.GetRedirect(userid,false).命中Global.asax中的Application_AuthenticateRequest方法.

// Extract the forms authentication cookie
        string cookieName = FormsAuthentication.FormsCookieName;
        HttpCookie authCookie = Context.Request.Cookies[cookieName];

        if (null == authCookie)
        {
            // There is no authentication cookie.
            return;
        }
Run Code Online (Sandbox Code Playgroud)

因此,在保存"好"cookie之后立即发生重定向,cookie就为空.我已通过调试器运行代码,并且cookie在这3个用户上仅为null.但是cookie看起来与成功登录的许多用户的cookie相同.

有任何想法吗?这是应该正常工作的标准代码.

Joe*_*ton 2

您确定“好”cookie 已保存并退出到响应吗?在 FormsAuthentication 中,可以将一个好的 cookie 添加到标头,但响应在退出时被其他一些系统错误(例如 w3wp.exe 崩溃)终止,因此会生成一个没有 cookie 的新响应,并发生重定向反正。

根据我自己遇到类似问题的经验,我有一个自定义的主体类,该类在身份验证(和 cookie 创建)后崩溃,并且没有编写适当的 cookie,而是从响应中完全删除了 cookie。