protected void Application_PostAuthenticateRequest(Object sender, EventArgs e) { HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); sRecipePrincipalSerializeModel userSerializeModel = JsonConvert.DeserializeObject <sRecipePrincipalSerializeModel>(authTicket.UserData); sRecipePrincipal userPrincipal = new sRecipePrincipal(authTicket.Name); userPrincipal.UserId = userSerializeModel.UserId; userPrincipal.NickName = userSerializeModel.NickName; userPrincipal.Role = userSerializeModel.Role; userPrincipal.Profile = userSerializeModel.Profile; HttpContext.Current.User = userPrincipal; } }
public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName) { Controller controller = base.CreateController(requestContext, controllerName) as Controller; string viewTheme = string.Empty; string colorTheme = "Default"; if (requestContext.HttpContext.Request.IsAuthenticated && requestContext.HttpContext.User != null) { sRecipePrincipal user = requestContext.HttpContext.User as sRecipePrincipal; (controller as ThemeControllerBase).User = user; if (user.Profile != null) { viewTheme = user.Profile.ViewTheme; colorTheme = user.Profile.ColorTheme; } } else { if (string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["ViewTheme"])) { viewTheme = ConfigurationManager.AppSettings["ViewTheme"]; } if (string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["ColorTheme"])) { colorTheme = ConfigurationManager.AppSettings["ColorTheme"]; } } controller.ViewBag.ColorTheme = colorTheme; if (!string.IsNullOrWhiteSpace(viewTheme)) { var eng = controller.ViewEngineCollection[0]; if (eng is ThemeViewEngine) { (eng as ThemeViewEngine).SetViewTheme(viewTheme); } else { controller.ViewEngineCollection.Insert(0, new ThemeViewEngine(viewTheme)); } } return(controller); }