System.Web.HttpContext.Current.User.Identity.IsAuthenticated有时会失败

我的生产网站(不是我的开发网站)遇到了问题。 有时Firefox和Chrome都无法登录用户(所有用户都在我们的客户网络和一般网络上)。 但奇怪的是,Internet Explorer始终正常工作,并且从未失败过一次(我在浏览器中删除了缓存和cookie,但仍然会发生同样的事情)。

然后经过一个小时或一段时间后,Firefox和Chrome会再次开始正常运行。

我有一个缩小到下面的function,即使在登录后总是返回false。

public bool isLoggedIn() { return System.Web.HttpContext.Current.User.Identity.IsAuthenticated; } 

所以这个过程如下,用户将使用此function登录:

 public void Login_OnClick(object sender, EventArgs args) { string email = UserName.Text; string password = Password.Text; string errorMsg = string.Empty; bool cb = cb_agreeterms.Checked; if (tests) { // The code in here tests to see if email, password, etc. have been filled out. // This works 100% of the time and is NOT a problem. } else { // Validate user. if (Membership.ValidateUser(email, password)) { // Get the logged in user MembershipUser user = Membership.GetUser(email); if (user.IsLockedOut) { user.UnlockUser(); } // Gets a datatable of the user details in our general database DataTable dtUserData = this.dbData.GetUserByEmail(user.UserName); if (dtUserData.Rows.Count > 0) { FormsAuthentication.SetAuthCookie(user.UserName, true); // The details for the userId, screenName, etc. below get set by looking at the row 0 in datatable // The LoginSession function intializes a session with a guid and saves all the data into an Application Context. This creates a SessionGuid cookie which I see get created on FF and Chrome (and always on IE). LoginSession(userId, screenName, permissionLevel, user.UserName); Response.Redirect("../myinternalsite.aspx"); } } else if (UserExistsInMembership(email)) { // Tested this out and entering bad credentials fails the login and error is shown correctly on screen in the login control. // We have failed to login. ShowLoginError("E-mail or password is incorrect."); } } } 

因此,当用户进行身份validation时,重定向将转至../myinternalsite.aspx。 在页面加载页面上调用VerifyLogin函数并调用:

 public bool isLoggedIn() 

以上ALWAYS在Chrome和FF中返回falso,提示重定向到主页。 几个小时后,这就解决了。 IE 100%的时间都在工作。

web.config是这样的:

 // authenticationConnection works and links correctly to the auth database just fine.             

Chrome和Firefox中的Cookie已设置完毕。 我删除了它们,看到它们正确重置。 但是这个问题是什么? 为什么IsAuthenticated仅针对某些浏览器失败并为其他浏览器工作然后自行修复?

我的所有不同步骤的登录模板也是这样的:

        

如果您使用MembershipProvider ,则无需自行创建表单身份validation cookie。

我回答了你的一个问题 ,但在阅读完之后,请忽略该答案,因为您正在使用会员提供程序 ,它将自动为您创建IPrincipal对象。

您所要做的就是使用ASP.Net 登录控件。

  

注意:成员资格和roleManager的applicationName应该相同。 它们在您的web.config中有所不同。

如何查看经过身份validation的用户信息

 protected void Page_Load(object sender, EventArgs e) { if (User.Identity.IsAuthenticated) { var sb = new StringBuilder(); var id = (FormsIdentity) User.Identity; var ticket = id.Ticket; sb.Append("Authenticated"); sb.Append("
CookiePath: " + ticket.CookiePath); sb.Append("
Expiration: " + ticket.Expiration); sb.Append("
Expired: " + ticket.Expired); sb.Append("
IsPersistent: " + ticket.IsPersistent); sb.Append("
IssueDate: " + ticket.IssueDate); sb.Append("
Name: " + ticket.Name); sb.Append("
UserData: " + ticket.UserData); sb.Append("
Version: " + ticket.Version); Label1.Text = sb.ToString(); } else Label1.Text = "Not Authenticated"; }