2010-07-28 12 views
6

मैं फॉर्म प्रमाणीकरण कुकी के साथ उपयोगकर्ता विवरण संग्रहीत कर रहा हूं।एक्सेस प्रमाणीकरण टिकट

FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, userName,DateTime.Now,DateTime.Now.AddMinutes(Timeout)false}; 

string encryptedTicket = FormsAuthentication.Encrypt(authTicket); 

HttpCookie authCookie = new HttpCookie(
FormsAuthentication.FormsCookieName,encryptedTicket);  

HttpContext.Current.Response.Cookies.Add(authCookie); 

मैं अतिरिक्त कुकी और उपयोगकर्ता विवरण (ऑथ टिकट) कैसे वापस प्राप्त कर सकता हूं?

उत्तर

11

आप FormsAuthenticationTicket निम्न के समान कोड का उपयोग कर प्राप्त कर सकते हैं:

// Retrieves the cookie that contains your custom FormsAuthenticationTicket. 
HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; 

// Decrypts the FormsAuthenticationTicket that is held in the cookie's .Value property. 
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); 

// The "authTicket" variable now contains your original, custom FormsAuthenticationTicket, 
// complete with User-specific custom data. You can then check that the FormsAuthenticationTicket's 
// .Name property is for the correct user, and perform the relevant functions with the ticket. 
// Here, we simply write the user-specific data to the Http Response stream. 
if (authTicket.Name == txtUserName.Text) 
{ 
    Response.Write(authTicket.UserData); 
} 

ऊपर कोड txtUserName.Text तरह बातें करने के लिए संदर्भ बनाता है इसलिए यहाँ एक पूर्ण .aspx पेज है कि आप एक खाली ASP.NET में पेस्ट कर सकते है वेबफॉर्म यह देखने के लिए कि यह कैसे काम करता है:

<%@ Page Language="C#" %> 
<%@ Import Namespace="System.Web.Security" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<script runat="server"> 


    protected void Page_Load(object sender, EventArgs e) 
    { 
     double Timeout = 15.00; 

     if (!IsPostBack) 
     { 
      FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,txtUserName.Text, 
        DateTime.Now,DateTime.Now.AddMinutes(Timeout), false, "This is my secret user-specific data"); 

      string encryptedTicket = FormsAuthentication.Encrypt(authTicket); 
      HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket); 
      HttpContext.Current.Response.Cookies.Add(authCookie); 
     } 
     else 
     { 
      // Retrieves the cookie that contains your custom FormsAuthenticationTicket. 
      HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; 

      // Decrypts the FormsAuthenticationTicket that is held in the cookie's .Value property. 
      FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); 

      // The "authTicket" variable now contains your original, custom FormsAuthenticationTicket, 
      // complete with User-specific custom data. You can then check that the FormsAuthenticationTicket's 
      // .Name property is for the correct user, and perform the relevant functions with the ticket. 
      // Here, we simply write the user-specific data to the Http Response stream. 
      if (authTicket.Name == txtUserName.Text) 
      { 
       Response.Write(authTicket.UserData); 
      } 
     } 
    }   
</script> 


<html xmlns="http://www.w3.org/1999/xhtml" > 
<head> 
    <title>Forms Authentication Login</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <div> 
      <table> 
       <tr> 
        <td> 
         UserName: 
        </td> 
        <td> 
         <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         Password: 
        </td> 
        <td> 
         <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <asp:Button ID="Button1" runat="server" Text="Login" /> 
        </td> 
       </tr> 
      </table> 
     </div> 
    </form> 
</body> 
</html> 
संबंधित मुद्दे