public async Task ExternalLogin() { var info = await this.signInManager.GetExternalLoginInfoAsync(); string email = info.Principal.Claims.First(x => x.Type.Contains("emailaddress")).Value; var user = new EventureUser { UserName = email, Email = email }; if (!(this.dbService.DbContext.Users.Any(x => x.UserName == user.UserName))) { var createUserResult = await userManager.CreateAsync(user); if (createUserResult.Succeeded) { createUserResult = await userManager.AddLoginAsync(user, info); if (createUserResult.Succeeded) { await signInManager.SignInAsync(user, isPersistent : false); } } } var result = await this.signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, false, true); }
public async Task <IActionResult> Register(RegisterInputModel model) { if (!ModelState.IsValid) { return(this.View(model)); } var user = new EventureUser { UserName = model.Username, FirstName = model.Firstname, Email = model.Email, LastName = model.Email, UniqueCitizenNumber = model.UniqueCitizenNumber }; await this.SignInManager.UserManager.CreateAsync(user, model.Password); var result = await this.SignInManager.PasswordSignInAsync(model.Username, model.Password, true, false); if (result == SignInResult.Success) { return(RedirectToAction("Index", "Home")); } return(this.View()); }
public async Task <IActionResult> Register(RegisterViewModel model) { if (this.User.Identity.Name != null) { await signInManager.SignOutAsync(); } var isUsernameExists = userManager.Users.FirstOrDefault(u => u.UserName == model.Username); if (isUsernameExists != null || model.Password != model.ConfirmPassword) { return(RedirectToAction("Register", "Users", model)); } //if (isUsernameExists != null && model.Password == model.ConfirmPassword) //{ // return RedirectToAction("Register", "Users", model); //} var user = new EventureUser() { UserName = model.Username, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, UCN = model.UCN }; await userManager.CreateAsync(user, model.Password); await userManager.AddToRoleAsync(user, "User"); return(RedirectToAction("Login", "Index")); }
public async Task <IActionResult> Login(LoginUserModel model) { if (ModelState.IsValid) { EventureUser user = await userManager.FindByNameAsync(model.Username); if (user != null) { await signInManager.SignOutAsync(); Microsoft.AspNetCore.Identity.SignInResult result = await signInManager.PasswordSignInAsync(user, model.Password, true, false); if (result.Succeeded) { return(RedirectToAction("Index", "Home")); } //if (result.Succeeded && User.IsInRole("Admin")) //{ // return RedirectToAction("IndexAdmin", "Home"); //} //if (result.Succeeded && User.IsInRole("User")) //{ // return RedirectToAction("IndexUser", "Home"); //} } } return(RedirectToAction("Login", "Users")); }
public int AddOrder(int ticketsCount, ClaimsPrincipal principal, string eventName) { Event currentEvent = this.eventsService.GetEvent(eventName); if (currentEvent != null) { EventureUser user = this.accountService.GetUser(principal); Order order = new Order { Customer = user, CustomerId = user.Id, OrderedOn = DateTime.UtcNow, Event = currentEvent, EventId = currentEvent.Id, TicketsCount = ticketsCount }; this.dbService.DbContext.Orders.Add(order); return(this.dbService.DbContext.SaveChanges()); } else { return(0); } }
private static void CreateAdminAndUser(UserManager <EventureUser> userManager, RoleManager <IdentityRole> roleManager) { Task.Run(async() => { var adminName = "Admin"; var userName = "******"; var adminRoleExists = await roleManager.RoleExistsAsync(adminName); var userRoleExists = await roleManager.RoleExistsAsync(userName); if (!adminRoleExists) { await roleManager.CreateAsync(new IdentityRole { Name = adminName }); var adminUser = await userManager.FindByNameAsync(adminName); if (adminUser == null) { adminUser = new EventureUser { UserName = adminName, Email = "*****@*****.**" }; await userManager.CreateAsync(adminUser, "test"); await userManager.AddToRoleAsync(adminUser, adminName); } } if (!userRoleExists) { await roleManager.CreateAsync(new IdentityRole { Name = userName }); var userUser = await userManager.FindByNameAsync(userName); if (userUser == null) { userUser = new EventureUser { UserName = userName, Email = "*****@*****.**" }; await userManager.CreateAsync(userUser, "test"); await userManager.AddToRoleAsync(userUser, userName); } } }).Wait(); }
public bool Register(EventureUser model, string password) { var result = OnRegisterPostAsync(model, password).Result; if (result.Succeeded) { return(true); } else { return(false); } }
public bool Login(EventureUser model, string password) { var result = this.OnLoginPostAsync(model, password).Result; if (result.Succeeded) { return(true); } else { return(false); } }
public async Task <bool> EmailExists(string email) { EventureUser user = await this.userManager.FindByEmailAsync(email); if (user == null) { return(false); } else { return(true); } }
public async Task <SignInResult> OnLoginPostAsync(EventureUser model, string password) { var user = this.dbService.DbContext.Users.FirstOrDefault(u => u.UserName == model.UserName); if (user == null) { return(SignInResult.Failed); } // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true var result = await signInManager.PasswordSignInAsync(user, password, false, lockoutOnFailure : true); return(result); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new EventureUser { UserName = Input.Username, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName, UCN = Input.UCN, }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { if (this._dbContext.Users.Count() == 1) { await this._userManager.AddToRoleAsync(user, "Admin"); } else { await this._userManager.AddToRoleAsync(user, "User"); } _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new EventureUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("RegisterConfirmation", new { email = Input.Email })); } else { await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public IActionResult Register(RegisterViewModel model) { var user = new EventureUser { Email = model.Email, UserName = model.Username }; var result = this.signIn.UserManager.CreateAsync(user, model.Password).Result; if (result.Succeeded) { this.signIn.SignInAsync(user, false).Wait(); return(this.RedirectToAction("Index", "Home")); } return(View()); }
private async Task LoadSharedKeyAndQrCodeUriAsync(EventureUser user) { // Load the authenticator key & QR code URI to display on the form var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); if (string.IsNullOrEmpty(unformattedKey)) { await _userManager.ResetAuthenticatorKeyAsync(user); unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); } SharedKey = FormatKey(unformattedKey); var email = await _userManager.GetEmailAsync(user); AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey); }
public IActionResult ExternalLogin() { var info = this.signInManager.GetExternalLoginInfoAsync().GetAwaiter().GetResult(); var email = info.Principal.FindFirstValue(ClaimTypes.Name); var user = this.userManager.FindByEmailAsync(email).Result; if (user == null) { user = new EventureUser { UserName = email, Email = email }; var result = this.userManager.CreateAsync(user).Result; } this.signInManager.SignInAsync(user, false).GetAwaiter().GetResult(); return(this.RedirectToAction("Index", "Home")); }
public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information during confirmation."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (ModelState.IsValid) { var user = new EventureUser { UserName = Input.Email, Email = Input.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("User created an account using {Name} provider.", info.LoginProvider); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return(Page()); }
private async Task SeedRoles( UserManager <EventureUser> userManager, RoleManager <IdentityRole> roleManager) { await roleManager.CreateAsync(new IdentityRole { Name = "admin" }); var user = new EventureUser { UserName = "******", FirstName = "Admin", LastName = "Admin", Email = "*****@*****.**", UniqueCitizenNumber = "1111111" }; await userManager.CreateAsync(user, "admin"); await userManager.AddToRoleAsync(user, "admin"); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new EventureUser { UserName = Input.Username, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName, UCN = Input.UCN }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { if (_userManager.Users.Count() == 1) { await _userManager.AddToRoleAsync(user, "Admin"); } else { await _userManager.AddToRoleAsync(user, "User"); } await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <IActionResult> Register(RegisterViewModel model) { if (!this.ModelState.IsValid) { return(this.View(model)); } var isUsernameExists = userManager.Users.FirstOrDefault(u => u.UserName == model.UserName); if (isUsernameExists != null) { return(this.View(model)); } var user = new EventureUser() { UserName = model.UserName, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, UCN = model.UCN }; var result = this.signInManager.UserManager.CreateAsync(user, model.Password).Result; var roleResult = this.signInManager.UserManager.AddToRoleAsync(user, Constants.ROLE_USER).Result; if (!result.Succeeded || !roleResult.Succeeded) { return(this.View()); } // With almost the same result: //await userManager.CreateAsync(user, model.Password); //await userManager.AddToRoleAsync(user, Constants.ROLE_USER); return(RedirectToAction("Login", "Users")); }
public EventureUser GetUser(ClaimsPrincipal principal) { EventureUser user = this.userManager.GetUserAsync(principal).GetAwaiter().GetResult(); return(user); }
public async Task <Microsoft.AspNetCore.Identity.IdentityResult> OnRegisterPostAsync(EventureUser model, string password) { var user = this.mapper.Map <EventureUser>(model); var result = await userManager.CreateAsync(user, password); if (result.Succeeded) { if (this.dbService.DbContext.Users.Count() == 1) { this.userManager.AddToRoleAsync(model, "Admin").GetAwaiter().GetResult(); } else { this.userManager.AddToRoleAsync(model, "User").GetAwaiter().GetResult(); } await signInManager.SignInAsync(user, isPersistent : false); return(result); } else { return(Microsoft.AspNetCore.Identity.IdentityResult.Failed()); } }