我正在分析一个由几个月前离开公司的同事创建的旧 asp.net 站点的问题。
问题是我们有几次经历过两个用户 session 混合在一起,例如,如果两个用户登录,一个用户会看到其他用户的数据。由于这种情况很少发生(大约一个月一次),因此很难弄清楚哪里出了问题。
我现在已经通过他的代码进行身份验证,它是这样的:
Page_Load
在 Masterpage 上,代码在 mySql 数据库中检查用户名/密码是否有效、未过期等,如果正常则返回唯一的用户 ID HttpContext.Current.Session(Consts.CCookieName_LoginUrl) = Request.RawUrl
FormsAuthentication.SetAuthCookie(userid, False)
Context.Response.Redirect(secureurl, False)
Page_Init
安全区域母版页的用户 ID 由以下人员读取:userid = Context.User.Identity.Name
我对出了什么问题有一些想法,但想在修改代码之前有一些输入,所以请任何人?
最佳答案
这里很难说。
您是否配置了表单例份验证?
这是表单例份验证必须遵循的过程:
在您的 web.config 中,您设置身份验证系统:
<authentication mode="Forms">
<forms loginUrl="Login.aspx" defaultUrl="Home.aspx" timeout="30" slidingExpiration="true" />
</authentication>
<authorization>
<deny users="?"/>
</authorization>
您的登录页面(回发)检查凭据(不是您的母版页)。
如果用户有效,那么您设置 cookie:
FormsAuthentication.SetAuthCookie(userid, False)
并重定向到另一个页面。
现在,您必须在此处设置您的主体读取 cookie:
protected void Application_AuthenticateRequest(Object sender, EventArgs e) {
if (HttpContext.Current.User != null) {
if (Request.IsAuthenticated == true) {
// Debug#1
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Context.Request.Cookies[FormsAuthentication.FormsCookieName].Value);
// In this case, ticket.UserData = "Admin"
string[] roles = new string[1] { ticket.UserData };
FormsIdentity id = new FormsIdentity(ticket);
Context.User = new System.Security.Principal.GenericPrincipal(id, roles);
// Debug#2
}
}
}
显然,我已经简化了,但这是你必须遵循的正确做事的路径。
关于asp.net - session 在 asp.net 网站上混在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6200172/