The session will not be available during AuthenticateRequest: you will need to mark the required information in Identity.userData; for example, if you use forms authentication, follow these steps:
void Application_AuthenticateRequest(object sender, EventArgs e) { if (Context.User != null) { if (Context.User.Identity.IsAuthenticated) {
To enter forms using forms, you need to write a custom cookie: MVC -> FormsAuthenticationService class: IFormsAuthenticationService
public static void SetAuthenticationCookie(HttpContextBase context, FormsAuthenticationTicket ticket) { var cookie = new HttpCookie(FormsAuthentication.FormsCookieName) { Value = FormsAuthentication.Encrypt(ticket), Secure = FormsAuthentication.RequireSSL, Domain = FormsAuthentication.CookieDomain, HttpOnly = true, Expires = DateTime.Now.AddMinutes(15) }; if (!context.Request.IsSecureConnection && FormsAuthentication.RequireSSL) { throw new HttpException("Ticket requires SSL."); } context.Response.Cookies.Add(cookie); } public static FormsAuthenticationTicket CreateTicket(HttpContextBase context, string emailAddress, string userData, bool persist) { return new FormsAuthenticationTicket(1, emailAddress, DateTime.Now, DateTime.Now.AddMinutes(15), persist, userData, FormsAuthentication.FormsCookiePath); }
Finally, in SignIn, you will create the required ticket by calling CreateTicket (...), and then you will write it SetAuthenticationCookie (...).
public void SignIn(string userName, string password) { if(CheckUserValid(userName,password, out string email)) { var ticket = CreateTicket(email, "Here you are", true); SetAuthenticationCookie(HttpContext.Current.Base(), ticket); } }
I have never used it, but I assume that session state is assigned during AcquireRequestState:
public void Init(HttpApplication context) { context.AcquireRequestState += new EventHandler(context_AcquireRequestState); }