public async Task <ActionResult> ResetPassword(ResetPasswordViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = await _userManager.FindByEmailAsync(model.Email); if (user == null) { ModelState.AddModelError("email", "Email không tồn tại"); } var provider = new DpapiDataProtectionProvider("YourAppName"); _userManager.UserTokenProvider = new DataProtectorTokenProvider <AppUser>(provider.Create("PasswordConfirmation")); string code = await _userManager.GeneratePasswordResetTokenAsync(user.Id); var result = await _userManager.ResetPasswordAsync(user.Id, code, model.Password); if (result.Succeeded) { return(RedirectToAction("ResetPasswordConfirmation", "Account")); } return(View()); }
public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(model.Email); if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id))) { // Don't reveal that the user does not exist or is not confirmed return(View("ForgotPasswordConfirmation")); } // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link var provider = new DpapiDataProtectionProvider("Absoc"); UserManager.UserTokenProvider = new DataProtectorTokenProvider <MyUser, int>(provider.Create("PasswordReset")); UserManager.EmailService = new EmailService(); string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); await UserManager.SendEmailAsync(user.Id, "Wachtwoord Reset", "Gelieve uw wachtwoord <a href=\"" + callbackUrl + "\">hier</a> te resetten"); return(RedirectToAction("ForgotPasswordConfirmation", "Account")); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IHttpActionResult> ConfirmEmail(string userId, string code) { if (userId == null || code == null) { ModelState.AddModelError("", "User Id and Code are required"); return(BadRequest(ModelState)); } var provider = new DpapiDataProtectionProvider("EmailConfirm"); var context = new ApplicationDbContext(); UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); UserManager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(provider.Create("EmailConfirmation")); IdentityResult result = await UserManager.ConfirmEmailAsync(userId, code); if (result.Succeeded) { var currentUser = await UserManager.FindByIdAsync(userId); var roleresult = await UserManager.AddToRoleAsync(userId, "Rescuer"); return(Ok()); } else { ModelState.AddModelError("", "Error on confirming email"); return(BadRequest(ModelState)); } }
public ApplicationUserManager(IUserStore <ApplicationUser> store) : base(store) { PasswordValidator = new PasswordValidator { RequireDigit = true, RequiredLength = 6, RequireLowercase = true, RequireNonLetterOrDigit = false, RequireUppercase = false }; UserValidator = new UserValidator <ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = true, RequireUniqueEmail = true }; EmailService = new EmailService(); DpapiDataProtectionProvider provider = new DpapiDataProtectionProvider("ASP.NET IDENTITY"); UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>( provider.Create("EmailConfirmation")) { TokenLifespan = TimeSpan.FromHours(1) }; }
public void InitUserManager() { _userManager.UserValidator = new UserValidator <ApplicationUser>(_userManager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; // Configure validation logic for passwords _userManager.PasswordValidator = new PasswordValidator { RequiredLength = 6, RequireNonLetterOrDigit = true, RequireDigit = true, RequireLowercase = true, RequireUppercase = true, }; // Configure user lockout defaults _userManager.UserLockoutEnabledByDefault = true; _userManager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); _userManager.MaxFailedAccessAttemptsBeforeLockout = 5; _userManager.EmailService = new EmailService(); var provider = new DpapiDataProtectionProvider("DotnetAcademy"); _userManager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(provider.Create("ASP.NET Identity")) { // Reset password link expires after 24 hours TokenLifespan = TimeSpan.FromHours(24) }; _userManager.EmailService = new EmailService(); }
public ApplicationUserManager(IUserStore <ApplicationUser> store) : base(store) { // Configure validation logic for usernames UserValidator = new UserValidator <ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; // Configure validation logic for passwords PasswordValidator = new PasswordValidator { RequiredLength = 6, RequireNonLetterOrDigit = true, RequireDigit = true, RequireLowercase = true, RequireUppercase = true, }; // Configure user lockout defaults UserLockoutEnabledByDefault = true; DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); MaxFailedAccessAttemptsBeforeLockout = 5; // Creat a token provider to generate the magic links var tokenProvider = new DpapiDataProtectionProvider("Piccolo"); // Set our new token provider as the default to be used // Declare what purposes we'll be using the token for UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(tokenProvider.Create("EmailConfirmation", "PasswordReset")); }
public ApplicationUserManager(IUserStore <ApplicationUser> store) : base(store) { var provider = new DpapiDataProtectionProvider("UserManagementAPI"); this.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(provider.Create("Confirmation")); this.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider <ApplicationUser> { Subject = "Security Code", BodyFormat = "Your security code is {0}" }); //this should be configurable this.UserValidator = new CustomUserValidator(this) { RequireUniqueEmail = true }; //this should be configurable this.PasswordValidator = new CustomPasswordValidator { RequiredLength = 8, RequireNonLetterOrDigit = false, RequireDigit = true, RequireLowercase = false, RequireUppercase = true, }; this.EmailService = new EmailServiceKW(); }
public async Task <ActionResult> ResetPassword(ResetPasswordViewModel model) { var provider = new DpapiDataProtectionProvider("VideoManager"); UserManager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>( provider.Create("EmailConfirmation")); if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(model.UserName); if (user == null) { ModelState.AddModelError("", "No user found."); return(View(model)); } IdentityResult result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password); if (result.Succeeded) { return(RedirectToAction("ResetPasswordConfirmation", "Account")); } else { AddErrors(result); return(View(model)); } } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> Register(UserModel model) { if (ModelState.IsValid) { var provider = new DpapiDataProtectionProvider("SampleAppName"); model.UserName = model.Email; // var user = new UserModel { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(model); if (result.Succeeded) { UserManager.UserTokenProvider = new DataProtectorTokenProvider <UserModel>( provider.Create("SampleTokenName")); // генерируем токен для подтверждения регистрации var code = await UserManager.GenerateEmailConfirmationTokenAsync(model.Id); // создаем ссылку для подтверждения var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = model.Id, code = code }, protocol: Request.Url.Scheme); // отправка письма await UserManager.SendEmailAsync(model.Id, "Подтверждение электронной почты", "Для завершения регистрации перейдите по ссылке:: <a href=\"" + callbackUrl + "\">завершить регистрацию</a>"); return(View("DisplayEmail")); } //AddErrors(result); } return(View(model)); }
public ApplicationUserManager(IUserBS store) : base(store) { // Configure validation logic for usernames UserValidator = new UserValidator(this) { AllowOnlyAlphanumericUserNames = true, RequireUniqueEmail = true }; // Configure validation logic for passwords PasswordValidator = new PassValidator { RequiredLength = 4, RequireNonLetterOrDigit = false, RequireDigit = true, RequireLowercase = false, RequireUppercase = false, }; EmailService = new EmailService(); // Configure user lockout defaults UserLockoutEnabledByDefault = true; DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); MaxFailedAccessAttemptsBeforeLockout = 5; var provider = new DpapiDataProtectionProvider("Neutrino"); UserTokenProvider = new DataProtectorTokenProvider <User, int>(provider.Create("ASP.NET Identity")); }
public ActionResult ResetPass(ResetModel model) { if (model.Password != model.ConfirmPassword) { ModelState.AddModelError("", "Passwords do not match!"); return(View()); } else { var provider = new DpapiDataProtectionProvider("MVC_Prices2"); userManager.UserTokenProvider = new DataProtectorTokenProvider <AppUser>( provider.Create("MVC_Prices2")); var result = userManager.ResetPasswordAsync(model.UserId, model.Code, model.Password); if (result.Result.Succeeded) { return(RedirectToAction("Login")); } else { string valMessage = ""; foreach (var el in result.Result.Errors) { valMessage += el + " "; } ModelState.AddModelError("", valMessage); return(View()); } } }
public UserManager(GameContesterDbContext context) { userManager = new UserManager <User>(new UserStore <User>(context)); var provider = new DpapiDataProtectionProvider("GameContester"); userManager.UserTokenProvider = new DataProtectorTokenProvider <User>(provider.Create("ResetPassword")); }
public ApplicationUserManager(IUserStore <ApplicationUser> store) : base(store) { UserValidator = new UserValidator <ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; PasswordValidator = new PasswordValidator { RequiredLength = IdentityHelper.MinimumPasswordLength, RequireNonLetterOrDigit = false, RequireDigit = false, RequireLowercase = false, RequireUppercase = false, }; UserLockoutEnabledByDefault = false; EmailService = new EmailService(); var provider = new DpapiDataProtectionProvider("Ingresso"); var dataProtector = provider.Create("ASP.NET Identity"); UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(dataProtector); }
private static DataProtectorTokenProvider <User, int> GetDataProtectionProvider() { // IdentityFactoryOptions<UserManager> options; var dataProtectionProvider = new DpapiDataProtectionProvider("IR"); return(new DataProtectorTokenProvider <User, int>(dataProtectionProvider.Create("IR Dpapi protection provider"))); }
public static void SetUserTokenProvider(this ApplicationUserManager userManager, params string[] userManagerPurposes) { IDataProtectionProvider dataProtectorProvider = new DpapiDataProtectionProvider("PoshBoutique"); IDataProtector dataProtector = dataProtectorProvider.Create(userManagerPurposes); userManager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(dataProtector); }
public void Protector(IDataProtectionProvider protector) { string[] purposes = new string[] { "", "" }; IDataProtector prot1 = protector.Create(purposes); DpapiDataProtectionProvider dpapi = new DpapiDataProtectionProvider(); IDataProtector prot2 = dpapi.Create(purposes); }
private void Configure(ApplicationUserManager manager) { var provider = new DpapiDataProtectionProvider("Sample"); manager.UserValidator = new UserValidator <ApplicationUser>(manager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; manager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(provider.Create("EmailConfirmation")); manager.PasswordValidator = new PasswordValidator { RequiredLength = 6, RequireNonLetterOrDigit = false, RequireDigit = true, RequireLowercase = true, RequireUppercase = true, }; manager.UserLockoutEnabledByDefault = true; manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); manager.MaxFailedAccessAttemptsBeforeLockout = 5; manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider <ApplicationUser> { Subject = "SecurityCode", BodyFormat = "Your security code is {0}" } ); manager.EmailService = new EmailService(); }
public void UpdateUser(string id, User user) { using (ApplicationDbContext context = new ApplicationDbContext()) { UserStore <ApplicationUser> store = new UserStore <ApplicationUser>(context); UserManager <ApplicationUser> UserManager = new UserManager <ApplicationUser>(store); var currentUser = UserManager.FindById(id); currentUser.FirstName = user.FirstName; currentUser.LastName = user.LastName; currentUser.Email = user.Email; currentUser.PhoneNumber = user.ContactNumber; UserManager.Update(currentUser); var currerntRole = UserManager.GetRoles(id).FirstOrDefault(); if (currerntRole != user.Role) { UserManager.RemoveFromRole(id, currerntRole); UserManager.AddToRole(id, user.Role); } if (!String.IsNullOrEmpty(user.Password)) { var provider = new DpapiDataProtectionProvider("MyVehicleTracker"); UserManager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(provider.Create("PasswordResetByAdmin")); var code = UserManager.GeneratePasswordResetToken(id); var result = UserManager.ResetPassword(id, code, user.Password); } } }
public void SetDefaultTokenProvider() { var provider = new DpapiDataProtectionProvider("Testing"); Database.UserManager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>( provider.Create("EmailConfirmation")); }
public AppUserManager(AppUserStore store) : base(store) { var provider = new DpapiDataProtectionProvider("SweetHome"); UserTokenProvider = new DataProtectorTokenProvider <AppUser, long>(provider.Create("EmailConfirmation")); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // 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 var provider = new DpapiDataProtectionProvider("TrolleyTracker"); UserManager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(provider.Create("EmailConfirmation")); 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")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public UserService(AppUnitOfWork db) { _db = db; var provider = new DpapiDataProtectionProvider("Sample"); _db.UserManager.UserTokenProvider = new DataProtectorTokenProvider <User>(provider.Create("EmailConfirmation")); }
//DONE: Change any settings related to your user password rules, etc. public static AppUserManager Create(IdentityFactoryOptions <AppUserManager> options, IOwinContext context) { var manager = new AppUserManager(new UserStore <AppUser>(context.Get <AppDbContext>())); // Configure validation logic for usernames manager.UserValidator = new UserValidator <AppUser>(manager) { //DONE: If you want to use email as the username, you need to make sure usernames can have @ //and that each person has a unique email AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; //NOTE: Stan added this var provider = new DpapiDataProtectionProvider("bevo"); manager.UserTokenProvider = new DataProtectorTokenProvider <AppUser, string>(provider.Create("UserToken")) as IUserTokenProvider <AppUser, string>; // DONE: Configure validation logic for passwords manager.PasswordValidator = new PasswordValidator { RequiredLength = 3, RequireNonLetterOrDigit = false, RequireDigit = false, RequireLowercase = false, RequireUppercase = false, }; return(manager); }
public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var user = await UserManager.FindByEmailAsync(model.Email); if (user == null) { ModelState.AddModelError("", "The user either does not exist."); return(View()); } var provider = new DpapiDataProtectionProvider("Fresh Fitness Foods"); UserManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider <ApplicationUser>(provider.Create("Password Reset")); // Send an email with this link string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); SendEmail sendEmail = new SendEmail(); sendEmail.SendForgotPasswordEmail(model.Email, user.FirstName, callbackUrl); return(RedirectToAction("ForgotPasswordConfirmation", "Account")); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> SendConfirmEmailAsync(MyUser user) { if (user.LastConfirmationMail == DateTime.Today) { return(View("ConfirmEmailFail")); } else { var provider = new DpapiDataProtectionProvider("Absoc"); UserManager.UserTokenProvider = new DataProtectorTokenProvider <MyUser, int>(provider.Create("EmailConfirmation")); UserManager.EmailService = new EmailService(); 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, "Bevestig uw e-mail adres", "Gelieve uw e-mail adres <a href=\"" + callbackUrl + "\">hier</a> te bevestigen"); Gebruiker gebruiker = gebruikerMng.GetGebruiker(user.Id); gebruiker.LastConfirmationMail = DateTime.Today; gebruikerMng.ChangeGebruiker(gebruiker); //return View("DisplayEmail"); ViewBag.User = user.Email; return(View("ConfirmEmailRedirect")); } }
public ApplicationUserManager(IUserStore <ApplicationUser> store) : base(store) { PasswordValidator = new PasswordValidator { RequiredLength = 6, RequireNonLetterOrDigit = false, RequireDigit = true, RequireLowercase = true, RequireUppercase = true, }; RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider <ApplicationUser> { MessageFormat = "Your security code is {0}" }); RegisterTwoFactorProvider("Email Code", new EmailTokenProvider <ApplicationUser> { Subject = "Security Code", BodyFormat = "Your security code is {0}" }); EmailService = new EmailService(); var provider = new DpapiDataProtectionProvider("Payment System"); this.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(provider.Create("PasswordReset")); // Configure user lockout defaults UserLockoutEnabledByDefault = true; DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); MaxFailedAccessAttemptsBeforeLockout = 5; }
public async Task <ActionResult> ResetPassword(ResetPasswordViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = await UserManager.FindByNameAsync(model.Email); if (user == null) { // Don't reveal that the user does not exist return(RedirectToAction("ResetPasswordConfirmation", "Account")); } var provider = new DpapiDataProtectionProvider("Absoc"); UserManager.UserTokenProvider = new DataProtectorTokenProvider <MyUser, int>(provider.Create("PasswordReset")); var result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password); if (result.Succeeded) { return(RedirectToAction("ResetPasswordConfirmation", "Account")); } AddErrors(result); return(View()); }
public static AppUserManager Create(IdentityFactoryOptions <AppUserManager> options, IOwinContext context) { GameShopContext db = context.Get <GameShopContext>(); AppUserManager manager = new AppUserManager(new UserStore <User>(db)); manager.PasswordValidator = new PasswordValidator() { RequiredLength = 6, RequireDigit = true, RequireLowercase = true, RequireUppercase = true }; manager.UserValidator = new UserValidator <User>(manager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; var provider = new DpapiDataProtectionProvider("SampleAppName"); manager.UserTokenProvider = new DataProtectorTokenProvider <User>(provider.Create("SampleTokenName")); manager.EmailService = new EmailService(); return(manager); }
public async Task <IHttpActionResult> ChangePassword(ChangePasswordViewModel model) { var provider = new DpapiDataProtectionProvider("ChangePassword"); var context = new ApplicationDbContext(); UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); UserManager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(provider.Create("ChangePassword")); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var findUser = await UserManager.FindByEmailAsync(model.Email); if (findUser == null) { return(BadRequest("Email not found")); } var userId = findUser.Id; var result = await UserManager.ChangePasswordAsync(userId, model.OldPassword, model.NewPassword); if (result.Succeeded) { return(Ok(model)); } return(BadRequest(ModelState)); }
public async Task <IHttpActionResult> ConfirmEmail(string code, string userId) { if ((userId == "") || (code == "")) { ModelState.AddModelError("", "Пользователь не зарегистрирован."); return(BadRequest(ModelState)); } var provider = new DpapiDataProtectionProvider("ApplicationName"); UserManager.UserTokenProvider = new DataProtectorTokenProvider <User>(provider.Create("ASP.NET Identity")); IdentityResult result = await UserManager.ConfirmEmailAsync(userId, code); if (result.Succeeded) { //User user = UserManager.FindById(userId); //user.EmailConfirmed = true; //await UserManager.UpdateAsync(user); return(Ok("пользователь активирован")); } else { ModelState.AddModelError("", "Неверный email."); } return(BadRequest(ModelState)); }