public static OpenIdUser FromCookieString(string cookieString) { if (cookieString.Contains(",")) { int claimedId; string claimedIdString = String.Empty; string claimedHashValue = String.Empty; string claimedIdentifier = String.Empty; var stringParts = cookieString.Split(','); if (stringParts.Length > 0) { claimedIdString = stringParts[0]; } if (stringParts.Length > 1) { claimedHashValue = stringParts[1]; } if (stringParts.Length > 2) { claimedIdentifier = stringParts[2]; } bool isValidId = int.TryParse(claimedIdString, out claimedId); if (isValidId && !String.IsNullOrWhiteSpace(claimedHashValue)) { using (CookiesRepository cookiesRep = new CookiesRepository()) using (AllUsersRepository userRep = new AllUsersRepository()) { bool isCookieValid = cookiesRep.GetList().Any(x => x.UserId == claimedId && x.HashValue == claimedHashValue); if (isCookieValid) { User loggingUser = userRep.GetEntity(claimedId); if (loggingUser != null) { return(new OpenIdUser() { UserId = loggingUser.Id, CompanyId = loggingUser.CompanyId, CompanyName = loggingUser.Company.Name, CompanyCoinSign = loggingUser.Company.CoinSign, Email = loggingUser.Email, NotificationEmail = loggingUser.NotificationEmail, NotificationCode = loggingUser.NotificationCode, FirstName = loggingUser.FirstName, LastName = loggingUser.LastName, Roles = loggingUser.Roles, CreationTime = loggingUser.CreationTime, LastLogInTime = loggingUser.LastLogInTime, IsSignedByProvider = false, ClaimedIdentifier = claimedIdentifier, OrdersApprovalRouteId = loggingUser.DefaultApprovalRouteId, IsActive = loggingUser.IsActive, LanguageCode = loggingUser.Language.Code }); } } } } } return(null); }
protected override bool AuthorizeCore(HttpContextBase httpContext) { if ( HttpContext.Current.Session["User"] == null ) { var authenticatedCookie = httpContext.Request.Cookies[OpenIdMembershipService.LOGIN_COOKIE_NAME]; if (authenticatedCookie != null) { var authenticatedCookieValue = authenticatedCookie.Value.ToString(); if (!string.IsNullOrWhiteSpace(authenticatedCookieValue)) { var user = OpenIdUser.FromCookieString(authenticatedCookieValue); if (user != null && user.IsActive) { HttpContext.Current.Session.Add("User", user); } else { HttpCookie myCookie = new HttpCookie(OpenIdMembershipService.LOGIN_COOKIE_NAME); myCookie.Expires = DateTime.Now.AddDays(-1d); httpContext.Response.Cookies.Add(myCookie); } } } } else { OpenIdUser sessionUser = (OpenIdUser)HttpContext.Current.Session["User"]; User databaseUser; using (AllUsersRepository allUserRep = new AllUsersRepository()) { databaseUser = allUserRep.GetEntity(sessionUser.UserId); } if (databaseUser != null) { sessionUser.Roles = databaseUser.Roles; } if (databaseUser == null || !databaseUser.IsActive) { HttpContext.Current.Session.Remove("User"); HttpCookie myCookie = new HttpCookie(OpenIdMembershipService.LOGIN_COOKIE_NAME); myCookie.Expires = DateTime.Now.AddDays(-1d); HttpContext.Current.Response.Cookies.Add(myCookie); } } if (HttpContext.Current.Session["User"] != null) { //Create culture info object CultureInfo ci = new CultureInfo(((OpenIdUser)HttpContext.Current.Session["User"]).LanguageCode); System.Threading.Thread.CurrentThread.CurrentUICulture = ci; System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name); } return HttpContext.Current.Session["User"] != null; }
protected override bool AuthorizeCore(HttpContextBase httpContext) { if ( HttpContext.Current.Session["User"] == null ) { var authenticatedCookie = httpContext.Request.Cookies[OpenIdMembershipService.LOGIN_COOKIE_NAME]; if (authenticatedCookie != null) { var authenticatedCookieValue = authenticatedCookie.Value.ToString(); if (!string.IsNullOrWhiteSpace(authenticatedCookieValue)) { var user = OpenIdUser.FromCookieString(authenticatedCookieValue); if (user != null && user.IsActive) { HttpContext.Current.Session.Add("User", user); } else { HttpCookie myCookie = new HttpCookie(OpenIdMembershipService.LOGIN_COOKIE_NAME); myCookie.Expires = DateTime.Now.AddDays(-1d); httpContext.Response.Cookies.Add(myCookie); } } } } else { OpenIdUser sessionUser = (OpenIdUser)HttpContext.Current.Session["User"]; User databaseUser; using (AllUsersRepository allUserRep = new AllUsersRepository()) { databaseUser = allUserRep.GetEntity(sessionUser.UserId); } if (databaseUser != null) { sessionUser.Roles = databaseUser.Roles; } if (databaseUser == null || !databaseUser.IsActive) { HttpContext.Current.Session.Remove("User"); HttpCookie myCookie = new HttpCookie(OpenIdMembershipService.LOGIN_COOKIE_NAME); myCookie.Expires = DateTime.Now.AddDays(-1d); HttpContext.Current.Response.Cookies.Add(myCookie); } } if (HttpContext.Current.Session["User"] != null) { //Create culture info object CultureInfo ci = new CultureInfo(((OpenIdUser)HttpContext.Current.Session["User"]).LanguageCode); System.Threading.Thread.CurrentThread.CurrentUICulture = ci; System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name); } return(HttpContext.Current.Session["User"] != null); }