asp.net-mvc – 谁设置HttpContext.User.Identity的IsAuthenticated属性

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 谁设置HttpContext.User.Identity的IsAuthenticated属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这段代码来自asp.net mvc RTM源代码

谁设置HttpContext.User.Identity的IsAuthenticated属性

protected virtual bool AuthorizeCore(HttpContextBase httpContext) {
        if (httpContext == null) {
            throw new ArgumentNullException("httpContext");
        }

        IPrincipal user = httpContext.User;
        if (!user.Identity.IsAuthenticated) {
            return false;
        }
    }

通过调用方法(asp.net mvc 4.0示例项目)设置IsAuthenticated属性

FormsAuthentication.SetAuthCookie(model.UserName,model.RememberMe);

当我调试asp.net mvc 4.0示例项目的logon方法代码之后,以上的FormsAuth …方法调用。执行

User.Identity.IsAuthenticated

仍然返回FALSE。只有当我调试logoff方法的时候

User.Identity.IsAuthenticated

说真的。那么谁将这个属性设置为TRUE和WHEN?

更新:

这是关于FORMS身份验证!

我现在调试了asp.net mvc示例项目的logon方法,并且在logon操作返回后,我已经覆盖的AuthorizeCore方法调用,然后IsAuthenticated属性为TRUE!

TRUE的设置是否取决于ModelState.Value.Error集合?

如果错误集合中的count == 0,则IsAuthenticated为TRUE
否则IsAuthenticated是FALSE

你可以确认吗

解决方法

属性由表单认证模块通过从请求中读取和解析表单认证cookie来设置。我已经提出了大胆的请求,因为我怀疑这是你遵循这种行为的原因。让我解释。成功验证后,当您调用FormsAuthentication.SetAuthCookie时,您将认证cookie添加到响应中。此cookie将存储在客户端浏览器上,并将在后续请求中发送。因此,只有在随后的请求中,用户才被认为是认证的。所以你需要总是重新调用SetAuthCookie方法。在调用方法的请求中,您已经知道用户是否提供正确的凭据,因此您不需要检查IsAuthenticated属性
原文链接:https://www.f2er.com/aspnet/252624.html

猜你在找的asp.Net相关文章