public async Task <IHttpActionResult> ForgotPassword(ForgotPasswordBindingModel model) { if (this.ModelState.IsValid) { var user = await this.UserManager.FindByEmailAsync(model.Email); if (user == null || !(await this.UserManager.IsEmailConfirmedAsync(user.Id))) { this.ModelState.AddModelError("", "The user either does not exist or is not confirmed."); return(this.BadRequest(this.ModelState)); } var code = await this.UserManager.GeneratePasswordResetTokenAsync(user.Id); var callbackUrl = this.Url.Content("~/account/resetpassword?email=") + HttpUtility.UrlEncode(model.Email) + "&code=" + HttpUtility.UrlEncode(code); var notification = new AccountNotificationModel { Url = callbackUrl, DisplayName = user.UserName }; var body = ViewRenderer.RenderView("~/Views/Mailer/PasswordReset.cshtml", notification); await this.UserManager.SendEmailAsync(user.Id, "DurandalAuth reset password", body); return(this.Ok()); } // If we got this far, something failed return(this.BadRequest(this.ModelState)); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var user = new UserProfile { UserName = model.UserName, Email = model.Email, EmailConfirmed = false }; var identityResult = await this.UserManager.CreateAsync(user, model.Password); var createResult = this.GetErrorResult(identityResult); if (createResult != null) { return(createResult); } var justCreatedUser = await this.UserManager.FindByNameAsync(model.UserName); /*var profile = new Profile * { * UserId = justCreatedUser.Id * };*/ var roleResult = await this.UserManager.AddToRoleAsync(justCreatedUser.Id, "User"); var addRoleResult = this.GetErrorResult(roleResult); if (addRoleResult != null) { return(addRoleResult); } var code = await this.UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = this.Url.Link("ConfirmEmail", new { userId = user.Id, code }); var notification = new AccountNotificationModel { Code = code, Url = callbackUrl, UserId = justCreatedUser.Id, Email = justCreatedUser.Email, DisplayName = justCreatedUser.UserName }; var body = ViewRenderer.RenderView("~/Views/Mailer/NewAccount.cshtml", notification); await this.UserManager.SendEmailAsync(user.Id, "Home Service account confirmation", body); //return this.Created(this.Request.RequestUri + justCreatedUser.Id, profile); return(this.Ok(justCreatedUser.Id)); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } UserProfile user = new UserProfile { UserName = model.UserName, Email = model.Email, EmailConfirmed = false }; IdentityResult identityResult = await UserManager.CreateAsync(user, model.Password); IHttpActionResult createResult = GetErrorResult(identityResult); if (createResult != null) { return(createResult); } UserProfile justCreatedUser = await UserManager.FindByNameAsync(model.UserName); IdentityResult roleResult = await UserManager.AddToRoleAsync(justCreatedUser.Id, "User"); IHttpActionResult addRoleResult = GetErrorResult(roleResult); if (addRoleResult != null) { return(addRoleResult); } string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Link("ConfirmEmail", new { userId = user.Id, code = code }); var notification = new AccountNotificationModel { Code = code, Url = callbackUrl, UserId = justCreatedUser.Id, Email = justCreatedUser.Email, DisplayName = justCreatedUser.UserName }; string body = ViewRenderer.RenderView("~/Views/Mailer/NewAccount.cshtml", notification); await UserManager.SendEmailAsync(user.Id, "DurandalAuth account confirmation", body); return(Ok()); }
public async Task <IHttpActionResult> ResendConfirmationEmail() { UserProfile user = await UserManager.FindByIdAsync(User.Identity.GetUserId()); string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Link("ConfirmEmail", new { userId = user.Id, code = code }); var notification = new AccountNotificationModel { Code = code, Url = callbackUrl, UserId = user.Id, Email = user.Email, DisplayName = user.UserName }; string body = ViewRenderer.RenderView("~/Views/Mailer/NewAccount.cshtml", notification); await UserManager.SendEmailAsync(user.Id, "DurandalAuth account confirmation", body); return(Ok()); }