登录验证

private void Login_Click(Object sender, EventArgs e)
  {
    // Create a custom FormsAuthenticationTicket containing
    // application specific data for the user.

    string username     = UserNameTextBox.Text;
    string password     = UserPassTextBox.Text;
    bool   isPersistent = PersistCheckBox.Checked;

    if (Membership.ValidateUser(username, password))
    {
      string userData = "ApplicationSpecific data for this user.";

      FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
        username,
        DateTime.Now,
        DateTime.Now.AddMinutes(30),
        isPersistent,
        userData,
        FormsAuthentication.FormsCookiePath);

      // Encrypt the ticket.
      string encTicket = FormsAuthentication.Encrypt(ticket);

      // Create the cookie.
      Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

      // Redirect back to original URL.
      Response.Redirect(FormsAuthentication.GetRedirectUrl(username, isPersistent));
    }
    else
    {
      Msg.Text = "Login failed. Please check your user name and password and try again.";
    }
  }
global.asax文件  

protected   void   Application_AuthorizeRequest(object   sender,   System.EventArgs   e)  
{  
        HttpApplication   App   =   (HttpApplication)sender;  
        HttpContext   Ctx   =   App.Context;   //获取本次Http请求相关的HttpContext对象  
        if   (Ctx.Request.IsAuthenticated   ==   true)   //验证过的用户才进行role的处理  
        {  
                FormsIdentity   Id   =   (FormsIdentity)Ctx.User.Identity;  
                FormsAuthenticationTicket   Ticket   =   Id.Ticket;   //取得身份验证票  
                string[]   Roles   =   Ticket.UserData.Split(',');   //将身份验证票中的role数据转成字符串数组  
                Ctx.User   =   new   GenericPrincipal(Id,   Roles);   //将原有的Identity加上角色信息新建一个GenericPrincipal表示当前用户,这样当前用户就拥有了role信息  
        }  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值