1、protected void Page_Load(object sender, EventArgs e) String ReturnUrl = Request.QueryStringReturnUrl; if (ReturnUrl = null | ReturnUrl.Equals(String.Empty) /默认情况下,按普通用户进行登录 Response.Redirect(/UserLogin.aspx); else if (ReturnUrl.ToLower().Contains(/admin/) /AdminLogin.aspx?ReturnUrl= + Server.UrlEnco
2、de(ReturnUrl); else /UserLogin.aspx?protected void Page_Load(object sender, EventArgs e)if (ReturnUrl = null | ReturnUrl.Equals(String.Empty) /默认情况下,按普通用户进行登录else) else在这个文件的代码中,如果ReturnUrl中含有,就重定向到AdminLogin.aspx登录界面;否则,就重定向到 UserLogin.aspx 登录界面。UserLogin.aspx这个文件的内容如下:<% Page Language=C# %>!
3、DOCTYPE html PUBLIC -/W3C/DTD XHTML 1.0 Transitional/EN http:/www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtdmce:script runat=server- protected void Button1_Click(object sender, EventArgs e) /密码验证过程在此省略,假如用户名是mxh,密码是mengxianhui String UserName = mxh; FormsAuthenticationTicket ticket = new FormsAuth
4、enticationTicket(2,/票证的版本号 UserName,/与身分验证票关联的用户名 DateTime.Now, /票证发出时的本地日期和时间 DateTime.Now.AddHours(1),/票证过期的本地日期和时间 true,/ 如果票证存储在持久性cookie中(跨浏览器会话保存)则为 true 否则为false 如果票证储存在URL中,将忽略此值 reader,/储存在票证中持定的用户信息,本页面供 reader 登录使用 FormsAuthentication.FormsCookiePath /票证储存在cookie中的路径 ); /如果 forms 元素的 prot
5、ection 属性设置为 All 或 Encryption,则窗体身份验证使用 Encrypt 方法对窗体身份验证票进行加密和签名。 string encTicket = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); Response.Cookies.Add(cookie); Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName,
6、true);/ -&/mce:script&html xmlns=/www.w3.org/1999/xhtmlhead runat= &title&孟宪会之多用户登录测试页面&/title&/head&body&form id=form1 runat= 普通用户登录界面省略&br /&asp:Button ID=Button1 OnClick=Button1_Click Text=普通用户登录 /&/form&/body&/html&- protected void Button1_Click(object sender, EventArgs e) /密码验证过程在此省略,假如用户名是mxh,
7、密码是mengxianhui FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2,/票证的版本号 UserName,/与身分验证票关联的用户名 DateTime.Now, /票证发出时的本地日期和时间 DateTime.Now.AddHours(1),/票证过期的本地日期和时间 true,/ 如果票证存储在持久性cookie中(跨浏览器会话保存)则为 true 否则为false 如果票证储存在URL中,将忽略此值,/储存在票证中持定的用户信息,本页面供 reader 登录使用 FormsAuthenticatio
8、n.FormsCookiePath /票证储存在cookie中的路径这个文件将验证信息保存后,返回最初的请求页面。注意:这里连接数据库验证用户名和密码的过程省略过去了。AdminLogin.aspx这个文件的全部内容如下: /密码验证过程在此省略,假如用户名是Admin,密码是mengxianhui Admin,/与身分验证票关联的用户名 admin|manager|editor,/储存在票证中持定的用户信息,本页面供 admin,manager,editor登录使用 Response.Redirect(FormsAuthentication.GetRedirectUrl(, true); 管
9、理员登录界面,省略 登 录 /密码验证过程在此省略,假如用户名是Admin,密码是mengxianhui,/与身分验证票关联的用户名,/储存在票证中持定的用户信息,本页面供 admin,manager,editor登录使用 管理员登录界面,省略3,在Global的AuthenticateRequest 事件(一定要注意:不是 AuthorizeRequest 事件)里将角色信息附加到当前用户的上下文中。protected void Application_AuthenticateRequest(object sender, EventArgs e) string cookieName = Fo
10、rmsAuthentication.FormsCookieName; HttpCookie authCookie = Context.Request.CookiescookieName; if (null = authCookie) return; FormsAuthenticationTicket authTicket = null; try authTicket = FormsAuthentication.Decrypt(authCookie.Value); catch (Exception ex) if (null = authTicket) FormsIdentity id = new
11、 FormsIdentity(authTicket); String roles = id.Ticket.UserData.Split(| /读出在登录时设置的角色列表。 System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(id, roles); Context.User = principal;/将验证信息附加到当前用户上下文。 protected void Application_AuthenticateRequest(object sen
12、der, EventArgs e) if (null = authCookie) try catch (Exception ex) if (null = authTicket)4,在web.config文件中,允许登录文件的匿名访问,以便在未登录的情况下显示登录界面,注意:如果包含图片、css等文件,也需要设置这些资源允许匿名访问。configuration&location path=AdminLogin.aspxsystem.web&authorization&allow users=?/&/authorization&/system.web&/location&UserLogin.aspxauthentication mode=forms loginUrl=Login.aspx path=/ protection=Encryption/forms&/authentication&deny users=*
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1