Asp.net 为什么Identity.IsAuthenticated()有时为真有时为假

Asp.net 为什么Identity.IsAuthenticated()有时为真有时为假,asp.net,asp.net-mvc-3,forms-authentication,Asp.net,Asp.net Mvc 3,Forms Authentication,我有一个使用身份验证服务的MVC应用程序,使用IsAuthenticated()方法返回true/false 它似乎没有连接到FormsAuthentication.SignOut()方法,或者当我在登录时添加新的授权cookie时 登录: HttpContext.Current.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTick

我有一个使用身份验证服务的MVC应用程序,使用IsAuthenticated()方法返回true/false

它似乎没有连接到FormsAuthentication.SignOut()方法,或者当我在登录时添加新的授权cookie时

登录:

HttpContext.Current.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket))); 
注销:

 FormsAuthentication.SignOut();
我的控制器上有一个自定义authorize属性,它使用IsAuthenticated()调用auth服务,但返回的值错误

有人知道为什么下面的代码有时会返回true或false吗

userPrincipal.Identity.IsAuthenticated

如果从非安全页面调用并在web配置上设置了requireSSL=“true”,则IsAuthenticated
始终返回false,因为无法读取经过身份验证的cookie

在其他情况下,如果用户经过身份验证,则返回true,否则返回false。设置一个类似这样的断言,以检查您是否从安全页面请求它

Debug.Assert(HttpContext.Current.Request.IsSecureConnection, "oops, the IsAuthenticated is not working here");

if (HttpContext.Current.User.Identity.IsAuthenticated)
{

}
(摘自上述评论)


我的身份验证服务依赖于IPrincipal,它在启动期间由StructureMap注入。然而,在这一点上,它将是未经验证的。我需要在当前线程之外使用IPrincipal。这将被传递到AuthorizeCore()中,该类将在我的CustomAuthorizate类(从AuthorizeAttribute继承)上被调用。

您好,我没有SSL设置。IsAuthenticated从AuthorizeCore()上的服务调用时返回false,这是我的自定义授权属性。@Jon可能无法读取Cookie和会话。您是否设置了读写cookies和会话数据的服务?我想我可能已经意识到了这个问题。我的身份验证服务依赖于IPrincipal,它在启动期间由StructureMap注入。然而,在这一点上,它将是未经验证的。我需要在当前线程之外使用IPrincipal。这将被传递到AuthorizeCore()中,AuthorizeCore()将在我的CustomAuthorize类上被调用(从AuthorizeAttribute继承)。