Asp.net User.Identity.IsAuthenticated有时返回false

Asp.net User.Identity.IsAuthenticated有时返回false,asp.net,forms-authentication,iprincipal,Asp.net,Forms Authentication,Iprincipal,Im使用asp.net 4.0和Form auth。 要检查用户是否经过身份验证,我使用user.Identity.IsAuthenticated。 大多数时候它工作得很完美,但我不知道怎么做,有时它返回false,即使用户拥有auth。 My web.config: <authentication mode="Forms"> <forms name=".xyz" loginUrl="~/" timeout="120" protection="All" path="/"

Im使用asp.net 4.0和Form auth。 要检查用户是否经过身份验证,我使用user.Identity.IsAuthenticated。 大多数时候它工作得很完美,但我不知道怎么做,有时它返回false,即使用户拥有auth。 My web.config:

<authentication mode="Forms">
    <forms name=".xyz" loginUrl="~/" timeout="120" protection="All" path="/" slidingexpiration=true/>
</authentication>
在登录页面中:

FormsAuthenticationTicket authTick = new FormsAuthenticationTicket(1, email.Text, DateTime.Now, DateTime.Now.AddDays(360), true, password.Text, FormsAuthentication.FormsCookiePath);
string encriptTicket = FormsAuthentication.Encrypt(authTick);

HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encriptTicket);
authCookie.Expires = DateTime.Now.AddDays(360);
Response.Cookies.Add(authCookie);
我还每隔5分钟使用一次ajax请求,以保持会话的活动性,这也会重置身份验证超时,因为滑动过期值。
我不知道它出了什么问题。有时在同一个会话中,在同一分钟内,一个页面返回false,即使另一个页面返回true。我从来没有遇到过这个错误,但是我的访问者声称我遇到了这个问题。

我发现了这个问题。问题在于www.address.com和address.com之间的区别。
www版本像子域一样,创建新的会话和身份验证。若当用户来时并没有www前缀,服务器重定向到www地址,则会发生错误。我将尝试url重写来解决此问题。

您是否尝试过Context.Request.IsAuthenticated?否。它是否更可靠?对我来说,它是一个我忘记的Web.config转换。@davidcefreitas,您能提供有关您的solution@ebramtharwat,我有一个production/development web.config.debug类型的转换文件,它在构建后更改了值。尝试在visual studio浏览器中展开web.config文件,或者如果有其他web.config.*文件可能正在转换或更改生成时的值,请检入项目文件夹。
FormsAuthenticationTicket authTick = new FormsAuthenticationTicket(1, email.Text, DateTime.Now, DateTime.Now.AddDays(360), true, password.Text, FormsAuthentication.FormsCookiePath);
string encriptTicket = FormsAuthentication.Encrypt(authTick);

HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encriptTicket);
authCookie.Expires = DateTime.Now.AddDays(360);
Response.Cookies.Add(authCookie);