还是FORM身份验证

1 指示是否发生身份验证的方法
System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
或者Request.IsAuthenticated;
2 获取用户名的方法
   FormsIdentity Id = (FormsIdentity)HttpContext.Current.User.Identity;
   FormsAuthenticationTicket Ticket = Id.Ticket ; //取得身份验证票
   string userName=Ticket.Name;
或者string userName=User.Identity.Name;
3 登录的部分代码

     if (FormsAuthentication.GetRedirectUrl(userName, false).EndsWith("default.aspx"))
     {
      //FormsAuthentication.SetAuthCookie(userName, persist.Checked);
       FormsAuthenticationTicket authTicket = new
       FormsAuthenticationTicket(1,userName,DateTime.Now,DateTime.Now.AddMinutes(20),persist.Checked, role);
       string encryptedTicket = FormsAuthentication.Encrypt(authTicket); //加密
       HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
       Response.Cookies.Add(authCookie);

      HttpContext.Current.Response.Redirect("default.aspx", true);
     }
     else
     {
      //FormsAuthentication.SetAuthCookie(userName, persist.Checked);
      FormsAuthenticationTicket authTicket = new
       FormsAuthenticationTicket(1,userName,DateTime.Now,DateTime.Now.AddMinutes(20),persist.Checked, role);
      string encryptedTicket = FormsAuthentication.Encrypt(authTicket); //加密
      HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
      Response.Cookies.Add(authCookie);

      HttpContext.Current.Response.Redirect(FormsAuthentication.GetRedirectUrl(userName, false), true);
     }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值