private async void AddAdminRole() { context.Roles.Add(new IdentityRole { Name = "Admin" }); context.SaveChanges(); var user = new User { UserName = "******", Email = "*****@*****.**" }; var result = userManager.CreateAsync(user, "password").Result; var adminUser = userManager.FindByNameAsync("Administrator").Result; await userManager.AddToRoleAsync(adminUser, "Admin"); }
protected override IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state) { this.UserProfile = this.Data.Users.All().Where(u => u.UserName == requestContext.HttpContext.User.Identity.Name).FirstOrDefault(); return base.BeginExecute(requestContext, callback, state); }
private async Task SignInAsync(User user, bool isPersistent) { this.AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie); this.AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(this.UserManager)); }
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return this.RedirectToAction("Index", "Manage"); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await this.AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return this.View("ExternalLoginFailure"); } var user = new User { UserName = model.Email, Email = model.Email }; var result = await this.UserManager.CreateAsync(user); if (result.Succeeded) { result = await this.UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await this.SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); return this.RedirectToLocal(returnUrl); } } this.AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return this.View(model); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new User { UserName = model.Email, Email = model.Email }; var result = await this.UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await this.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 // 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 this.RedirectToAction("Index", "Home"); } this.AddErrors(result); } // If we got this far, something failed, redisplay form return this.View(model); }
private void GenerateUsers() { for (int i = 0; i < 20; i++) { var randomName = random.GenerateRandomString(5, 15); var randomPassword = random.GenerateRandomString(10, 30); var randomEmail = random.GenerateRandomString(3, 6) + "@" + random.GenerateRandomString(2, 5) + ".bg"; var user = new User() { UserName = randomName, PasswordHash = randomPassword, Email = randomEmail, LanguageId = random.GenerateRandomNumber(1, 2), CountryId = random.GenerateRandomNumber(1, context.Countries.Count() - 1), CityId = random.GenerateRandomNumber(1, context.Cities.Count() - 1), }; context.Users.Add(user); } context.SaveChanges(); }
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new User { UserName = model.Email, Email = model.Email }; // TODO: CHANGE // user.Language = languages.GetLanguage("en").FirstOrDefault(); var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713 // Send an email with this link //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); //await _emailSender.SendEmailAsync(model.Email, "Confirm your account", // $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>"); await _signInManager.SignInAsync(user, isPersistent: false); _logger.LogInformation(3, "User created a new account with password."); return RedirectToLocal(returnUrl); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }
public async Task<IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null) { if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); } var user = new User { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent: false); _logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewData["ReturnUrl"] = returnUrl; return View(model); }