标签:
<!-- 通过 <authentication> 节可以配置 ASP.NET 使用的 安全身份验证模式, 以标识传入的用户。 --> <authentication mode="Forms"> <forms name=".Bk5173" protection="All" timeout="30" path="/" requireSSL="false" cookieless="UseDeviceProfile" enableCrossAppRedirects="false"> </forms> </authentication>2. 验证用户的用户名和密码合法之后:
//设置用户的 cookie 的值 FormsAuthentication.SetAuthCookie(loginName, false);3 . 上一步就是把用户的名称写 cookie, 之后的话就可以 获取用户的名称.
//设置用户的 cookie 的值 FormsAuthentication.SetAuthCookie(loginName, false);为以下方法:
//设置用户的 cookie 的值 FormsAuthentication.SetAuthCookie(loginName, false); //获取用户的 cookie HttpCookie cookie = FormsAuthentication.GetAuthCookie(loginName, false); //给用户的 cookie 的值加上 cookie 的域 和 过期日期 //更新 用户 cookie FormsAuthenticationTicket oldTicket = FormsAuthentication.Decrypt(cookie.Value); FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(1, oldTicket.Name, oldTicket.IssueDate, DateTime.Now.AddMinutes(30), oldTicket.IsPersistent, oldTicket.UserData, FormsAuthentication.FormsCookiePath); cookie.Domain = cookieDomain; cookie.Value = FormsAuthentication.Encrypt(newTicket); //更新 cookie HttpContext.Current.Response.SetCookie(cookie);2). 修改用户退出时的方法
HttpCookie cookie = HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName]; cookie.Domain = cookieDomain; cookie.Value = null; cookie.Expires = DateTime.Now.AddDays(-1); //更新cookie HttpContext.Current.Response.Cookies.SetCookie(cookie); FormsAuthentication.SignOut();但是如果按照以上的做法的是无法获取到 Identity.Name 的. 经过分析和查找了相关的资料后将以上方法修改成
//设置用户的 cookie 的值 FormsAuthentication.SetAuthCookie(loginName, false); //获取用户的 cookie HttpCookie cookie = FormsAuthentication.GetAuthCookie(loginName, false); //给用户的 cookie 的值加上 cookie 的域 和 过期日期 //向客户端重写同名的 用户 cookie FormsAuthenticationTicket oldTicket = FormsAuthentication.Decrypt(cookie.Value); FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(1, oldTicket.Name, oldTicket.IssueDate, DateTime.Now.AddMinutes(30), oldTicket.IsPersistent, oldTicket.UserData, FormsAuthentication.FormsCookiePath); cookie.Domain = cookieDomain; cookie.Value = FormsAuthentication.Encrypt(newTicket); HttpContext.Current.Response.Cookies.Add(cookie); 2). HttpCookie cookie = HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName]; cookie.Domain = cookieDomain; cookie.Value = null; cookie.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(cookie); FormsAuthentication.SignOut();这样的话用户就可以成功登陆和退出了!
FormsAuthentication.SetAuthCookie
标签:
原文地址:http://www.cnblogs.com/qqhfeng/p/5017728.html