Asp.net HttpContext.Current.User.Identity.Name出现问题
在这样一个环境中,大约有100多个用户使用 表单身份验证,调用HttpContext.Current.User.Identity.Name 返回正确登录的用户Asp.net HttpContext.Current.User.Identity.Name出现问题,asp.net,forms-authentication,httpcontext,Asp.net,Forms Authentication,Httpcontext,在这样一个环境中,大约有100多个用户使用 表单身份验证,调用HttpContext.Current.User.Identity.Name 返回正确登录的用户 FormsAuthentication.SetAuthCookie(user.SYS_Users_ID.ToString(), false); if (Request["ReturnURL"] == null) FormsAuthentication.RedirectFromLoginPage(user.SYS_Users_
FormsAuthentication.SetAuthCookie(user.SYS_Users_ID.ToString(), false);
if (Request["ReturnURL"] == null)
FormsAuthentication.RedirectFromLoginPage(user.SYS_Users_ID.ToString(), false);
else
Response.Redirect("/" + SysConfig.ApplicationName + appConfig.DefaultPages.DefaultPage);
但是,有10%的时间返回错误的用户全名信息。
我的测试机从来没有出现过这样的问题,只有在生产中才会出现。我无法在我的测试机器上与许多用户重新创建相同的环境
FormsAuthentication.SetAuthCookie(user.SYS_Users_ID.ToString(), false);
if (Request["ReturnURL"] == null)
FormsAuthentication.RedirectFromLoginPage(user.SYS_Users_ID.ToString(), false);
else
Response.Redirect("/" + SysConfig.ApplicationName + appConfig.DefaultPages.DefaultPage);
此应用程序的逻辑:
FormsAuthentication.SetAuthCookie(user.SYS_Users_ID.ToString(), false);
if (Request["ReturnURL"] == null)
FormsAuthentication.RedirectFromLoginPage(user.SYS_Users_ID.ToString(), false);
else
Response.Redirect("/" + SysConfig.ApplicationName + appConfig.DefaultPages.DefaultPage);
1) 用户输入用户名并通过,通过SQL DB调用查找信息,如果匹配,则通过
FormsAuthentication.RedirectFromLoginPage(用户名,false)
FormsAuthentication.SetAuthCookie(user.SYS_Users_ID.ToString(), false);
if (Request["ReturnURL"] == null)
FormsAuthentication.RedirectFromLoginPage(user.SYS_Users_ID.ToString(), false);
else
Response.Redirect("/" + SysConfig.ApplicationName + appConfig.DefaultPages.DefaultPage);
2) 重定向后,我将用户全名放入隐藏字段
FormsAuthentication.SetAuthCookie(user.SYS_Users_ID.ToString(), false);
if (Request["ReturnURL"] == null)
FormsAuthentication.RedirectFromLoginPage(user.SYS_Users_ID.ToString(), false);
else
Response.Redirect("/" + SysConfig.ApplicationName + appConfig.DefaultPages.DefaultPage);
if (!IsPostBack)
userFullName.Value = Helper.GetCurrentUserFullName();
...
public static string GetCurrentUserFullName()
{
string _userFullName = string.Empty;
try
{
_userFullName = new AgrotMasofim.DAL.Users.Users().GetUserFullName(GetCurrentUserID());
}
catch (Exception ex)
{
Logs.WriteToFileLog(string.Empty,ex);
}
return _userFullName;
}
public static Decimal GetCurrentUserID()
{
Decimal _userID = 0;
if (HttpContext.Current.User != null)
{
try
{
_userID = Convert.ToDecimal(HttpContext.Current.User.Identity.Name);
}
catch (Exception ex)
{
Logs.WriteToFileLog(string.Empty, ex);
}
}
return _userID;
}
3) 在用户访问的所有页面上,他/她的信息显示在母版页上的标签内
FormsAuthentication.SetAuthCookie(user.SYS_Users_ID.ToString(), false);
if (Request["ReturnURL"] == null)
FormsAuthentication.RedirectFromLoginPage(user.SYS_Users_ID.ToString(), false);
else
Response.Redirect("/" + SysConfig.ApplicationName + appConfig.DefaultPages.DefaultPage);
lblUserName.Text = HttpUtility.HtmlDecode("Hello " + userFullName.Value);
这几乎一直有效。你知道为什么它会失败吗
有时?没有更多的代码,我只能猜测你的问题。由于其他人可能会发现您的问题并有类似的问题,我猜您的问题在于静态类或属性的错误使用
FormsAuthentication.SetAuthCookie(user.SYS_Users_ID.ToString(), false);
if (Request["ReturnURL"] == null)
FormsAuthentication.RedirectFromLoginPage(user.SYS_Users_ID.ToString(), false);
else
Response.Redirect("/" + SysConfig.ApplicationName + appConfig.DefaultPages.DefaultPage);
您的GetCurrentUserFullName()
方法可能依赖于在所有线程之间静态共享的数据访问方法。数据访问类中可能存在竞争条件,这有时会导致正在搜索的用户id在检索数据之前被另一个请求的id替换。解决方案是:(a)在数据访问类的所有关键部分使用锁,或者(b)使用一个解决方案,为每个请求(实际上是每个工作单元)实例化一个新的数据访问类。后一种设计要求数据访问类是轻量级的,但更可取,因为它也更易于测试
FormsAuthentication.SetAuthCookie(user.SYS_Users_ID.ToString(), false);
if (Request["ReturnURL"] == null)
FormsAuthentication.RedirectFromLoginPage(user.SYS_Users_ID.ToString(), false);
else
Response.Redirect("/" + SysConfig.ApplicationName + appConfig.DefaultPages.DefaultPage);
如果您正在缓存静态属性中的值或线程之间共享的其他静态类中的值,那么您也有可能在缓存和使用这些值时遇到类似的竞争条件。类似的解决方案也适用-使用锁定或使用每线程实例而不是静态实例。请向我们展示您的
GetCurrentUserFullName()
方法的代码。错误信息是什么意思?是别人的名字吗?它是空的吗?变量userFullName是如何声明的?它显示了其他人的姓名。userFullName是页面上的一个隐藏字段。非常感谢您的良好解释,我认为这是发生的,因为它是一个静态方法,我将对其进行更改。@user1023623可以在GetCurrentUserID()
或DAL中再降低一级。在某些情况下,您会有一个方法引用线程之间共享的静态变量。局部变量应该是线程安全的,因为每个线程都有自己的堆栈。GetCurrentUserID()也是一个返回HttpContext.Current.User.Identity.Name的静态方法。它会返回错误的标识吗。名称是因为这个吗?关于userFullName
-这是一个静态变量吗?userFullName是页面上的一个隐藏字段
FormsAuthentication.SetAuthCookie(user.SYS_Users_ID.ToString(), false);
if (Request["ReturnURL"] == null)
FormsAuthentication.RedirectFromLoginPage(user.SYS_Users_ID.ToString(), false);
else
Response.Redirect("/" + SysConfig.ApplicationName + appConfig.DefaultPages.DefaultPage);