private void AddErrors(Microsoft.AspNet.Identity.IdentityResult result) { foreach (var error in result.Errors) { ModelState.AddModelError("", error); } }
public async Task <ActionResult> DeleteConfirmed(string id) { if (ModelState.IsValid) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ApplicationUser user = await UserManager.FindByIdAsync(id); if (user == null) { return(HttpNotFound()); } Microsoft.AspNet.Identity.IdentityResult result = await UserManager.DeleteAsync(user); if (!result.Succeeded) { ModelState.AddModelError("", result.Errors.First()); return(View()); } return(RedirectToAction("Index")); } return(View()); }
public async Task <ActionResult> Create(RegisterViewModel userViewModel, params string[] selectedRoles) { if (ModelState.IsValid) { ApplicationUser user = new ApplicationUser { UserName = userViewModel.Email, Email = userViewModel.Email }; Microsoft.AspNet.Identity.IdentityResult adminresult = await UserManager.CreateAsync(user, userViewModel.Password); //Add User to the selected Roles if (adminresult.Succeeded) { if (selectedRoles != null) { Microsoft.AspNet.Identity.IdentityResult result = await UserManager.AddToRolesAsync(user.Id, selectedRoles); if (!result.Succeeded) { ModelState.AddModelError("", result.Errors.First()); ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name"); return(View()); } } } else { ModelState.AddModelError("", adminresult.Errors.First()); ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name"); return(View()); } return(RedirectToAction("Index")); } ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name"); return(View()); }
private IHttpActionResult GetErrorResult(Microsoft.AspNet.Identity.IdentityResult result) { if (result == null) { return(InternalServerError()); } if (!result.Succeeded) { if (result.Errors != null) { foreach (string error in result.Errors) { ModelState.AddModelError("", error); } } if (ModelState.IsValid) { // No ModelState errors are available to send, so just return an empty BadRequest. return(BadRequest()); } return(BadRequest(ModelState)); } return(null); }
private void AddErrors(Microsoft.AspNet.Identity.IdentityResult result) { foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } }
public async System.Threading.Tasks.Task <ActionResult> Register(string username, string email, string password) { var userStore = new Microsoft.AspNet.Identity.EntityFramework.UserStore <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(); var manager = new Microsoft.AspNet.Identity.UserManager <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(userStore); var user = new Microsoft.AspNet.Identity.EntityFramework.IdentityUser() { UserName = username, Email = email, EmailConfirmed = false }; manager.UserTokenProvider = new Microsoft.AspNet.Identity.EmailTokenProvider <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(); Microsoft.AspNet.Identity.IdentityResult result = await manager.CreateAsync(user, password); if (result.Succeeded) { //I have some options: log them in, or I can send them an email to "Confirm" their account details.' //I don't have email set up this week, so we'll come back to that. string confirmationToken = await manager.GenerateEmailConfirmationTokenAsync(user.Id); string confirmationLink = Request.Url.GetLeftPart(UriPartial.Authority) + "/Account/Confirm/" + user.Id + "?token=" + confirmationToken; string apiKey = System.Configuration.ConfigurationManager.AppSettings["SendGrid.ApiKey"]; SendGrid.ISendGridClient client = new SendGrid.SendGridClient(apiKey); SendGrid.Helpers.Mail.EmailAddress from = new SendGrid.Helpers.Mail.EmailAddress("*****@*****.**", "Coding Cookware Administrator"); SendGrid.Helpers.Mail.EmailAddress to = new SendGrid.Helpers.Mail.EmailAddress(email); string subject = "Confirm your Coding Cookware Account"; string htmlContent = string.Format("<a href=\"{0}\">Confirm Your Account</a>", confirmationLink); string plainTextContent = confirmationLink; SendGrid.Helpers.Mail.SendGridMessage message = SendGrid.Helpers.Mail.MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent); SendGrid.Response response = await client.SendEmailAsync(message); TempData["EmailAddress"] = email; return(RedirectToAction("ConfirmationSent")); //Commenting this out: I'm not going to log the user in on registration anymore - I'm going to send them a confirmation email instead. //This authentication manager will create a cookie for the current user, and that cookie will be exchanged on each request until the user logs out //var authenticationManager = HttpContext.GetOwinContext().Authentication; //var userIdentity = await manager.CreateIdentityAsync(user, Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie); //authenticationManager.SignIn(new Microsoft.Owin.Security.AuthenticationProperties() { }, userIdentity); } else { ViewBag.Error = result.Errors; return(View()); } return(RedirectToAction("Index", "Home")); }
public async Task TestRegister() { Random r = new Random(13); UserViewModel userViewModel = new UserViewModel { UserName = "******" + r.Next().ToString(), Password = "******" }; Microsoft.AspNet.Identity.IdentityResult result = await _userService.RegisterAccountUser(userViewModel); Assert.IsTrue(result.Succeeded); }
public async Task <bool> Register(RegisterViewModel model) { ApplicationUser user = new ApplicationUser { UserName = model.Email, Email = model.Email }; Microsoft.AspNet.Identity.IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return(false); } await SignInManager.SignInAsync(user, false, false); return(true); }
//[AcceptVerbs("GET")] public async Task <IHttpActionResult> Register(UserModel userModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Microsoft.AspNet.Identity.IdentityResult result = await _repo.RegisterUser(userModel); IHttpActionResult errorResult = GetErrorResult(result); if (errorResult != null) { return(errorResult); } return(Ok()); }
public async Task <ActionResult> Edit([Bind(Include = "Email,Id")] EditUserViewModel editUser, params string[] selectedRole) { if (ModelState.IsValid) { ApplicationUser user = await UserManager.FindByIdAsync(editUser.Id); if (user == null) { return(HttpNotFound()); } user.UserName = editUser.Email; user.Email = editUser.Email; System.Collections.Generic.IList <string> userRoles = await UserManager.GetRolesAsync(user.Id); selectedRole = selectedRole ?? new string[] { }; Microsoft.AspNet.Identity.IdentityResult result = await UserManager.AddToRolesAsync(user.Id, selectedRole.Except(userRoles).ToArray <string>()); if (!result.Succeeded) { ModelState.AddModelError("", result.Errors.First()); return(View()); } result = await UserManager.RemoveFromRolesAsync(user.Id, userRoles.Except(selectedRole).ToArray <string>()); if (!result.Succeeded) { ModelState.AddModelError("", result.Errors.First()); return(View()); } return(RedirectToAction("Index")); } ModelState.AddModelError("", "Something failed."); return(View()); }