Forms身份验证

private void Button1_Click(object sender, System.EventArgs e)
  {
   string user = this.uid.Text; //读取用户名
   string password = this.upwd.Text; //读取密码
   if(Confirm(user,password) == true) //confirm方法用来验证用户合法性的
   {
    //string userRoles = UserToRole(user); //调用UserToRole方法来获取role字符串
    FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket (1,user,DateTime.Now,DateTime.Now.AddMinutes(30), false,"/") ; //建立身份验证票对象
    string HashTicket = FormsAuthentication.Encrypt (Ticket) ; //加密序列化验证票为字符串
    HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName,HashTicket) ;
    //生成Cookie
   
    Context.Response.Cookies.Add (UserCookie) ; //输出Cookie
    Context.Response.Redirect("./community/main.aspx") ; // 重定向到用户申请的初始页面

   }
   else
   {
    // 用户身份未被确认时的代码
    Response.Write("<script language=\"javascript\">alert('用户身份验证失败,密码错误或者没有这个用户!')</script>");
   }
  }

 

 

Web.config文件内容

    
  <authentication mode="Forms">
   <forms name="kefan" loginUrl="Default.aspx" protection="All" timeout="60"/>
  </authentication>
  <authorization>
   <deny users="?"/>
  </authorization>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值