private async Task LoadSharedKeyAndQrCodeUriAsync(LMSAppUser 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 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 LMSAppUser { 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()); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (Input.Role == "Student") { if (_userService.AnyStudent(Input.Code, Input.FacultyName)) { ModelState.AddModelError(string.Empty, "Student with this UniId and Faculty already registered."); return(Page()); } } else { if (Input.Code != EducatorPassword) { ModelState.AddModelError(string.Empty, "Your educator code is wrong."); return(Page()); } } if (ModelState.IsValid) { var user = new LMSAppUser { UserName = Input.Username, Email = Input.Email, FirstName = Input.FirstName, FamilyName = Input.FamilyName }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { if (Input.Role == "Student") { await this._userManager.AddToRoleAsync(user, "Student"); string groupId; if (this._groupService.AnyGroup(Input.GroupNumber, Input.Major)) { groupId = this._groupService.GetAll() .Where(g => g.Major == Input.Major && g.Number == Input.GroupNumber) .First().Id; } else { groupId = await this._groupService.CreateAsync(new GroupViewModel() { Major = Input.Major, Number = Input.GroupNumber }); } await this._userService.CreateAsync(new StudentBindingModel() { UserId = user.Id, StudentUniId = Input.Code, FacultyName = Input.FacultyName, Major = Input.Major, GroupId = groupId }); } else { await this._userManager.AddToRoleAsync(user, "Educator"); await this._educatorService.CreateAsync(new EducatorBindingModel() { UserId = user.Id, FacultyName = Input.FacultyName }); } _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 CreateRolesAsync() { var adminRole = "Admin"; var roleNames = new String[] { adminRole, "Educator", "Student" }; foreach (var roleName in roleNames) { var role = await rolesManager.RoleExistsAsync(roleName); if (!role) { var result = await rolesManager.CreateAsync(new IdentityRole { Name = roleName }); } } // administrator var user = new LMSAppUser { UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, FirstName = "Test1", FamilyName = "Test2" }; var i = await userManager.FindByEmailAsync(user.Email); if (i == null) { var adminUser = await userManager.CreateAsync(user, "123456"); if (adminUser.Succeeded) { await userManager.AddToRoleAsync(user, adminRole); } } //educator var user2 = new LMSAppUser { UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, FirstName = "Edu1", FamilyName = "Edu2" }; var j = await userManager.FindByEmailAsync(user2.Email); if (j == null) { var eduUser = await userManager.CreateAsync(user2, "123456"); if (eduUser.Succeeded) { await userManager.AddToRoleAsync(user2, "Educator"); } } //student var user3 = new LMSAppUser { UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, FirstName = "Stu1", FamilyName = "Stu2" }; var k = await userManager.FindByEmailAsync(user3.Email); if (k == null) { var eduUser = await userManager.CreateAsync(user3, "123456"); if (eduUser.Succeeded) { await userManager.AddToRoleAsync(user3, "Student"); } } }