public static MvcHtmlString ApplyUserTheme(this HtmlHelper helper) { UrlHelper url = new UrlHelper(helper.ViewContext.RequestContext); AuthCookieModel user = Authentication.GetLoggedUser(); string fn = null; if (user != null && user.UserTheme != null) { fn = String.Format("~/Content/themes/{0}.css", user.UserTheme.ToLower()); if (false == File.Exists(HttpContext.Current.Server.MapPath(fn))) { fn = null; } } if (fn == null) { //fn = "~/Content/bootstrap/bootstrap-theme.min.css"; fn = "~/Content/themes/united.css"; } var css = new TagBuilder("link"); css.MergeAttribute("href", url.Content(fn)); css.MergeAttribute("rel", "stylesheet"); return(MvcHtmlString.Create(css.ToString(TagRenderMode.SelfClosing))); }
public static bool IsLoggedUserManager() { AuthCookieModel user = GetLoggedUser(); using (var ctx = new Entities()) { var ret = from t in ctx.Teams where t.UserID == user.UserID && t.Role.RoleName.ToLower().Contains("dono") select t; return(ret.Count() != 0); } }
/// <summary> /// Gets logged user from the authentication ticket. /// </summary> /// <returns></returns> public static AuthCookieModel GetLoggedUser() { if (HttpContext.Current.User.Identity.IsAuthenticated) { var id = (FormsIdentity)HttpContext.Current.User.Identity; var data = id.Ticket.UserData; AuthCookieModel user = (AuthCookieModel) new JavaScriptSerializer().Deserialize(data, typeof(AuthCookieModel)); return(user); } return(null); }
private void HandleUserAuthorization(HttpActionContext actionContext) { var headerString = default(string); if (actionContext.Request.Headers.Contains(ConfigurationValue.ApiAuthHeaderName)) { headerString = actionContext.Request.Headers.GetValues(ConfigurationValue.ApiAuthHeaderName).First(); } if (string.Equals(headerString, "HEADER_NULL")) { HandleUnauthorizedRequest(actionContext); return; } if (string.IsNullOrEmpty(headerString)) { HandleUnauthorizedRequest(actionContext); return; } AuthCookieModel header = null; try { #if DEBUG var result = headerString;// CspCrossPlatformExchangeWrapper.UnWrapKey(headerString); #else var result = EncryptUtils.Base64Decrypt(AesEncryptHelper.DecryptAes(headerString)); #endif header = JsonConvert.DeserializeObject <AuthCookieModel>(result); } catch (Exception ex) { throw; } //todo:判断账号超时 if (header == null || string.IsNullOrEmpty(header.UserName) || string.IsNullOrEmpty(header.Token) || DateTime.Now > header.Expired) { HandleUnauthorizedRequest(actionContext); return; } ThreadStaticObject.UserId = header.Id; ThreadStaticObject.UserName = header.UserName; ThreadStaticObject.Token = header.Token; }
/// <summary> /// Refresh authentication cookie with updated details of the logged user. /// </summary> public static void RefreshCookie() { AuthCookieModel user = GetLoggedUser(); if (user != null) { using (var ctx = new Entities()) { UserAccount ua = ctx.UserAccounts.FirstOrDefault(i => i.UserID == user.UserID); if (ua != null) { var ticket = MakeAuthCookie(ua, user.RememberMe); HttpCookie ck = new HttpCookie(FormsAuthentication.FormsCookieName, ticket); HttpContext.Current.Response.Cookies.Set(ck); } } } }
/// <summary> /// Creates a new ticket for a forms authentication cookie. /// </summary> /// <param name="user"></param> /// <param name="remember"></param> /// <returns></returns> private static string MakeAuthCookie(UserAccount user, bool remember) { AuthCookieModel data = new AuthCookieModel() { UserID = user.UserID, Email = user.Email, UserTheme = (user.UserProfile != null ? user.UserProfile.UserTheme : null), RememberMe = remember }; data.FullName = (user.UserProfile != null && user.UserProfile.FullName != null && user.UserProfile.FullName.Trim().Length != 0) ? user.UserProfile.FullName : user.Email.Substring(0, user.Email.IndexOf('@')); var json = new JavaScriptSerializer().Serialize(data); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, data.FullName, DateTime.Now, DateTime.Now.AddMinutes(240) , data.RememberMe, json, FormsAuthentication.FormsCookiePath); return(FormsAuthentication.Encrypt(ticket)); }