您的位置:首页 > 移动开发 > Objective-C

登录验证

2008-05-31 16:39 162 查看
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信息  
        }  
}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息