public static IdentityResult CreateUser(User user, string password) { IdentityResult result = new IdentityResult(); string hashedPassword = Crypto.HashPassword(password); Random rand = new Random(); user.ID = rand.Next(); user.PasswordHash = hashedPassword; Users.Add(user); Write(); result.Success = true; return result; }
private void SignIn(User user, bool rememberMe = false) { Session["IsLoggedIn"] = true; Session["CurrentUser"] = user; }
public ActionResult Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new User() { UserName = model.UserName }; IdentityResult result = UserManager.CreateUser(user, model.Password); if (result.Success) { //TODO //vv I think here is where we add the system Role, and all new should be 'normal' user, we need to find a place to change this role in the near future //ref: http://stackoverflow.com/questions/19689183/add-user-to-role-asp-net-identity //var currentUser = UserManager.FindByName(user.UserName); //CheckAspRoles(); //var userRoleResult = UserManager.AddToRole(currentUser.Id, "Normal"); SignIn(user); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return RedirectToAction("Index", "Home"); } else { //AddErrors(result); } } // If we got this far, something failed, redisplay form return View(model); }