Mul*_*ner 9 asp.net authentication session forms-authentication
我正在分析几个月前离开公司的一位同事所做的旧asp.net网站的问题.
问题是我们有几次表示两个用户会话混淆了,所以如果例如两个用户登录,则一个用户看到其他用户数据.因为它很少发生(一个月左右),很难弄清楚出了什么问题.
我现在已经通过他的代码进行身份验证,它是这样的:
Page_LoadMasterpage上,代码在mySql数据库中检查用户名/密码是否有效,未过期等,如果确定则返回唯一的用户IDHttpContext.Current.Session(Consts.CCookieName_LoginUrl) = Request.RawUrlFormsAuthentication.SetAuthCookie(userid, False)Context.Response.Redirect(secureurl, False)Page_Init安全区域的母版页中,用户标识由以下内容读取: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>
Run Code Online (Sandbox Code Playgroud)
您的登录页面(回发后)检查凭据(而不是您的母版页).如果用户有效,则设置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
}
}
}
Run Code Online (Sandbox Code Playgroud)
显然,我已经简化了,但这是你必须遵循的正确行事路径.
| 归档时间: |
|
| 查看次数: |
2233 次 |
| 最近记录: |