.Net Asp.Net MVC Development

Manually Logging User with Role

To logging a user with a custom role, you have to create an authentication cookies, only after user has been authenticated (Username and password has been verified).

FormsAuthenticationTicket Class
Namespace: System.Web.Security
Assembly: System.Web.dll

Constructor: public FormsAuthenticationTicket (int version, string name, DateTime issueDate, DateTime expiration, bool isPersistent, string userData, string cookiePath);

if (user_authenticated == true)
  {
     FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, TempData.Peek("userid").ToString(), DateTime.Now, DateTime.Now.AddMinutes(15)
                        , false,Session["role"].ToString(), FormsAuthentication.FormsCookiePath);
                new Helpers().LogActivity(User.Identity.Name.ToString(), "LoginAttempt", "Success");
                string hash = FormsAuthentication.Encrypt(ticket);
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
                Response.Cookies.Add(cookie);

Leave a Reply

Your email address will not be published. Required fields are marked *