title: Application_AuthenticateRequest Executing first before a Forms authentication ticket is created
body: <p>I am having trouble while using role based security with Forms Authentication, every time I try and login for the first time ticket seems to have no UserData as Application_Authenticate request is executing first, and my if statements is not executing as the roles are none after first post back.</p>
  
  <p>please help!</p>
  
  <p>My Sign In click event:</p>
  
  <pre><code> protected void signin_click(object sender, EventArgs e)
  {
      if (con.State == ConnectionState.Closed)
      {
          con.Open();
      }
      HashData ob = new HashData();//Custom Class used for Hashing Passwords
      SqlCommand cmd = new SqlCommand("Logincheck", con);
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = txt_username.Text.Trim();
      string pass = ob.Encrypt(txt_pass.Text.Trim());
      cmd.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = pass;
      SqlParameter result = new SqlParameter("@result", SqlDbType.Int) { Direction = ParameterDirection.Output };
      SqlParameter userrole = new SqlParameter("@userrole", SqlDbType.VarChar,50) { Direction = ParameterDirection.Output };
      cmd.Parameters.Add(result); cmd.Parameters.Add(userrole);
  
      cmd.ExecuteNonQuery();
      int rslt = Convert.ToInt32(result.Value);
      if (rslt == -1)
      {
          string message = "Login Failed";
          string url = "Login.aspx";
          string script = "window.onload = function(){ alert('";
          script += message;
          script += "');";
          script += "window.location = '";
          script += url;
          script += "'; }";
          ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
      }
      string u_role = userrole.Value.ToString();
      FormsAuthenticationTicket ticket = new FormsAuthenticationTicket
          (1, txt_username.Text.Trim(), DateTime.Now,
          DateTime.Now.AddMinutes(30), false, u_role,
          FormsAuthentication.FormsCookiePath);
      string hash = FormsAuthentication.Encrypt(ticket);
      HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
      if (ticket.IsPersistent)
      {
          cookie.Expires = ticket.Expiration;
      }
      Response.Cookies.Add(cookie);
      if (User.IsInRole("admin"))
      {
          Response.Redirect("~/Admin/Admin.aspx");
      }
      if (User.IsInRole("manager"))
      {
          Response.Redirect("~/Manager/Manager.aspx");
      }
      if (User.IsInRole("teamlead"))
      {
          Response.Redirect("~/Teamlead/Teamlead.aspx");
      }
      if (User.IsInRole("qa"))
      {
          Response.Redirect("~/Default.aspx");
      }
      cmd.Dispose();
      con.Close();
  }
  </code></pre>
  
  <p>And my Global.ASAX File</p>
  
  <pre><code>protected void Application_AuthenticateRequest(Object sender, EventArgs e)
  {
      if (HttpContext.Current.User != null)
      {
          if (HttpContext.Current.User.Identity.IsAuthenticated)
          {
              if (HttpContext.Current.User.Identity is FormsIdentity)
              {
                  FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                  FormsAuthenticationTicket ticket = id.Ticket;
                  string userData = ticket.UserData;
                  string[] roles = userData.Split(',');
                  HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
              }
          }
      }
  }
  </code></pre>