I'm not sure if this is the best way to do this, but I want to keep the user live object during all requests of the current user. From reading several resources, I found out that you must create your own IPrinciple that contains this. But I do not want to run the database every authentication request. Any recommendations on how to handle this? Is caching a db request a good idea?
protected void Application_AuthenticateRequest(Object sender, EventArgs e) { HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); User user; using (HgDataContext hg = new HgDataContext()) { if (Session["user"] != null) { user = (from u in hg.Users where u.EmailAddress == authTicket.Name select u).Single(); } else { user = Session["user"] as User; } } var principal = new HgPrincipal(user); Context.User = principal; } }
Tomhastjarjanto
source share