public ActionResult Login(LoginViewModel model, string returnUrl) { try { if (!ModelState.IsValid) { return(View(model)); } CEUserManager ceUserManager = new CEUserManager(); SHA1HashProvider sHA1HashProvider = new SHA1HashProvider(); User anActiveOrBlockedUser = ceUserManager.GetSigningUserByEmail(model.Email); if (anActiveOrBlockedUser != null && sHA1HashProvider.CheckHashSHA1(model.Password, anActiveOrBlockedUser.Password, 8)) { UserDTO userDTO = EntityDTOHelper.GetEntityDTO <User, UserDTO>(anActiveOrBlockedUser); AuthenticatedUserInfo authenticatedUserInfo = new AuthenticatedUserInfo(userDTO); ceUserManager.SignInUser(HttpContext, string.Format("{0}", authenticatedUserInfo.FullName), false); Session["loggeduser"] = authenticatedUserInfo; SessionManager.RegisterSessionActivity(loggedInAt: DateTime.Now); return(this.RedirectToLocal(returnUrl)); } ModelState.AddModelError(string.Empty, "Login attempt failed."); } catch (Exception e) { System.Diagnostics.Trace.WriteLine(e); } return(this.View(model)); }
internal async Task <IdentityResult> ChangePasswordAsync(int loggedUserId, string oldPassword, string newPassword) { bool returnSuccess = false; try { SHA1HashProvider sHA1HashProvider = new SHA1HashProvider(); using (CraveatsDbContext craveatsDbContext = new CraveatsDbContext()) { User thisUser = craveatsDbContext.User.FirstOrDefault(u => u.Id == loggedUserId); if (thisUser != null && sHA1HashProvider.CheckHashSHA1(oldPassword, thisUser.Password, 8)) { thisUser.Password = sHA1HashProvider.SecureSHA1(newPassword); thisUser.LastUpdated = DateTime.Now; await craveatsDbContext.SaveChangesAsync(); returnSuccess = true; } } } catch (Exception e) { Trace.WriteLine(e); } return(returnSuccess ? IdentityResult.Success : IdentityResult.Failed("Change password failed")); }
private async void btnCreateFile_Click(object sender, RoutedEventArgs e) { DirectoryInfo wot = new DirectoryInfo(txtGameFolder.Text); string output = txtOutputFile.Text; RootDirectoryEntity root = new RootDirectoryEntity(Helpers.GetGameVersion(txtGameFolder.Text)); HashProvider sha1 = new SHA1HashProvider(); btnCreateFile.IsEnabled = false; btnCreateFile.Content = "Creating..."; await Task.Run(() => GameDirectoryParser.Parse(wot, root, wot.FullName.Length, sha1, IgnoreList.FromEnumerable(File.ReadAllLines("ignored.txt")), null)); await Task.Run(() => new RootDirectoryEntityIO().Serialize(root, output)); btnCreateFile.Content = "Create"; btnCreateFile.IsEnabled = true; }
public void TextGetHashedText() { SHA1HashProvider sHA1HashProvider = new SHA1HashProvider(); //string clrText = "Test123$"; //string hashedText = sHA1HashProvider.SecureSHA1(clrText); //byte[] baLeft = new byte[4], // baRight = new byte[4]; //RandomNumberGenerator rngInstance = RandomNumberGenerator.Create(); //rngInstance.GetBytes(baLeft); //rngInstance.GetBytes(baRight); //string left = BitConverter.ToString(baLeft).Replace("-",""), // right = BitConverter.ToString(baRight).Replace("-", ""), // tempHashText = sHA1HashProvider.HashSHA1(clrText + left + right), // finalBlock = left+tempHashText+right; string org = "Test123$", hashedText = sHA1HashProvider.SecureSHA1(org); string rSS = sHA1HashProvider.GetRandomHexString(9); string rSE = new string(sHA1HashProvider.GetRandomHexString(9).Reverse().ToArray());// new string(rSS.Reverse().ToArray()); //byte[] baLeft = new byte[4], // baRight = new byte[4]; //RandomNumberGenerator rngInstance = RandomNumberGenerator.Create(); //rngInstance.GetBytes(baLeft); //rngInstance.GetBytes(baRight); //string rSS = System.BitConverter.ToString(baLeft).Replace("-", ""), // rSE = System.BitConverter.ToString(baRight).Replace("-", ""); string cTWPATH = sHA1HashProvider.HashSHA1(org + rSS + rSE), replica = rSS + cTWPATH + rSE ; Assert.AreSame(hashedText, replica); }
public ActionResult Register(RegisterViewModel model) { SessionManager.RegisterSessionActivity(); // Get all states again var roles = GetAllRoles(); // Set these states on the model. We need to do this because // only the selected value from the DropDownList is posted back, not the whole // list of states. model.Roles = GenUtil.GetSelectListItems(roles); // In case everything is fine - i.e. both "Name" and "State" are entered/selected, // redirect user to the "Done" page, and pass the user object along via Session if (ModelState.IsValid) { SHA1HashProvider sHA1HashProvider = new SHA1HashProvider(); if (!ceUserManager.IsRegistered(model.Email)) { string sha1HashText = sHA1HashProvider.SecureSHA1(model.Password.Trim()); int? newUserID = ceUserManager.RegisterNew(model.Email, sha1HashText, model.Role); if (newUserID.HasValue) { UserDTO userDTO = new UserDTO() { Id = DataSecurityTripleDES.GetEncryptedText(newUserID), FirstName = model.FirstName, Surname = model.Surname, UserStatus = (int?)UserStatusEnum.Active }; ceUserManager.SaveUserDetail(userDTO); StringBuilder sbSubject = new StringBuilder("Craveats new registrant notification"), sbEmailBody = new StringBuilder("<p>A new user with the following detail has been registered in the system. " + $"<br/><em>FirstName </em>: {model.FirstName}" + $"<br/><em>Surname </em>: {model.Surname}" + $"<br/><em>Email </em>: {model.Email}" + $"<br/><em>Registration Type </em>: {model.Role}" + "</p><p>Thank you.</p><p>Craveats</p>"); CommunicationServiceProvider.SendOutgoingNotification( new MailAddress( model.Email, string.Format("{0}{1}{2}", model.FirstName, " ", model?.Surname).Trim()), sbSubject.ToString(), sbEmailBody.ToString()); User result = ceUserManager.FindByCriteria(email: model.Email, userStatusEnums: new List <int> { (int)UserStatusEnum.Active, (int)UserStatusEnum.Blocked }); if (result != null) { userDTO = EntityDTOHelper.GetEntityDTO <User, UserDTO>(result); AuthenticatedUserInfo authenticatedUserInfo = new AuthenticatedUserInfo(userDTO); Session["loggeduser"] = authenticatedUserInfo; SessionManager.RegisterSessionActivity(userID: result.Id, loggedInAt: DateTime.Now); ceUserManager.SignInUser(HttpContext, string.Format("{0}", authenticatedUserInfo.FullName), false); return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError(string.Empty, "An error occurred in reading user data. Please review input and re-try."); } } else { ModelState.AddModelError(string.Empty, "An error occurred in registering new user. Please review input and re-try."); } } else { ModelState.AddModelError(string.Empty, "Email is registered and cannot be used to create another account."); } } // Something is not right - so render the registration page again, // keeping the data user has entered by supplying the model. return(View("Register", model)); }