Thursday, September 23, 2010

Use FormsAuthenticationTicket to save LoggedIn UserID

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2,id,DateTime.Now,DateTime.Now.AddMinutes(10), false, testid);

string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
HttpContext.Current.Response.Cookies.Add(cookie);


Fetch Data from FormsAuthenticationTicket :-


if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.AuthenticationType == "Forms")
{
System.Web.Security.FormsIdentity formsIdentity = default(System.Web.Security.FormsIdentity);
formsIdentity = (System.Web.Security.FormsIdentity)HttpContext.Current.User.Identity;
string str = formsIdentity.Ticket.UserData;
}
}

No comments: