public HttpCookie CreateFormsAuthenticationCookie(OpenIdUser user) { Random rand = new Random(); int randomInt = rand.Next(0, int.MaxValue); string hashValue = MD5Encryptor.GetHash(randomInt.ToString()); using (CookiesRepository cookiesRep = new CookiesRepository()) { Cooky existingCookie = cookiesRep.GetList().FirstOrDefault(x => x.UserId == user.UserId); if (existingCookie != null) { if (cookiesRep.Delete(existingCookie.Id) == false) { return(null); } } Cooky newCookie = new Cooky() { UserId = user.UserId, HashValue = hashValue }; if (cookiesRep.Create(newCookie) == false) { return(null); } } //var ticket = new FormsAuthenticationTicket(1, user.FullName, DateTime.Now, DateTime.Now.AddDays(7), true, user.GetCookieString(hashValue)); //var encrypted = FormsAuthentication.Encrypt(ticket).ToString(); var cookie = new HttpCookie(LOGIN_COOKIE_NAME, user.GetCookieString(hashValue)); return(cookie); }
public ActionResult DeleteConfirmed(int id) { if (Authorized(RoleType.SystemManager)) { User user; using (CookiesRepository cookieRep = new CookiesRepository()) using (UsersRepository userRep = new UsersRepository(CurrentUser.CompanyId)) { user = userRep.GetEntity(id); if (user == null) { return(Error(Loc.Dic.error_user_not_found)); } if (user.Id == CurrentUser.UserId) { return(Error(Loc.Dic.error_user_cannot_delete_self)); } if (user.CompanyId != CurrentUser.CompanyId || user.Roles == (int)RoleType.SuperAdmin) { return(Error(Loc.Dic.error_no_permission)); } user.IsActive = false; userRep.Update(user); Cooky expiredCookie = cookieRep.GetList().SingleOrDefault(x => x.UserId == user.Id); if (expiredCookie != null) { cookieRep.Delete(expiredCookie.Id); } } return(RedirectToAction("Index")); } else { return(Error(Loc.Dic.error_no_permission)); } }
public HttpCookie CreateFormsAuthenticationCookie(OpenIdUser user) { Random rand = new Random(); int randomInt = rand.Next(0, int.MaxValue); string hashValue = MD5Encryptor.GetHash(randomInt.ToString()); using (CookiesRepository cookiesRep = new CookiesRepository()) { Cooky existingCookie = cookiesRep.GetList().FirstOrDefault(x => x.UserId == user.UserId); if (existingCookie != null) { if (cookiesRep.Delete(existingCookie.Id) == false) return null; } Cooky newCookie = new Cooky() { UserId = user.UserId, HashValue = hashValue }; if (cookiesRep.Create(newCookie) == false) return null; } //var ticket = new FormsAuthenticationTicket(1, user.FullName, DateTime.Now, DateTime.Now.AddDays(7), true, user.GetCookieString(hashValue)); //var encrypted = FormsAuthentication.Encrypt(ticket).ToString(); var cookie = new HttpCookie(LOGIN_COOKIE_NAME, user.GetCookieString(hashValue)); return cookie; }
public ActionResult DeleteConfirmed(int id) { if (Authorized(RoleType.SystemManager)) { User user; using (CookiesRepository cookieRep = new CookiesRepository()) using (UsersRepository userRep = new UsersRepository(CurrentUser.CompanyId)) { user = userRep.GetEntity(id); if (user == null) { return Error(Loc.Dic.error_user_not_found); } if (user.Id == CurrentUser.UserId) { return Error(Loc.Dic.error_user_cannot_delete_self); } if (user.CompanyId != CurrentUser.CompanyId || user.Roles == (int)RoleType.SuperAdmin) { return Error(Loc.Dic.error_no_permission); } user.IsActive = false; userRep.Update(user); Cooky expiredCookie = cookieRep.GetList().SingleOrDefault(x => x.UserId == user.Id); if (expiredCookie != null) { cookieRep.Delete(expiredCookie.Id); } } return RedirectToAction("Index"); } else { return Error(Loc.Dic.error_no_permission); } }
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); }