winner

古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

保存Cookie 设置免登陆时间

Posted on 2007-04-27 11:12  狂ミ风  阅读(593)  评论(0)    收藏  举报

FormsAuthenticationTicket 类  [C#]请参见
FormsAuthenticationTicket 成员 | System.Web.Security 命名空间 | FormsAuthenticationTicket 成员(Visual J# 语法) | C++ 托管扩展编程
要求
命名空间: System.Web.Security

平台: Windows 2000, Windows XP Professional, Windows Server 2003 系列

程序集: System.Web (在 System.Web.dll 中)
语言
C#

全部显示
提供一种创建 FormsAuthenticationModule 使用的 Forms 身份验证 cookie(包含身份验票)并读取其值的方法。无法继承此类。

有关此类型所有成员的列表,请参阅 FormsAuthenticationTicket 成员。

System.Object
   System.Web.Security.FormsAuthenticationTicket

    private bool Authenticated(string email, string password)
    {
    // This method authenticates the user for the application.
    // In this demonstration application it always returns
    // true.

    return true;
    }

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

    string email        = UserEmail.Text;
    string password     = UserPass.Text;
    bool   isPersistent = Persist.Checked;

    if (Authenticated(email,password)) {

         string userData = "ApplicationSpecific data for this user.";

       FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
        1,
        email,
        System.DateTime.Now,
        System.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(email,isPersistent));
    }
    }