Пример #1
0
        private async Task CreateUserRoles(UserManager <ThesisDbUser> um, RoleManager <IdentityRole> rm)
        {
            ThesisDbUser user = await um.FindByNameAsync("*****@*****.**");

            if (user == null)
            {
                user = new ThesisDbUser()
                {
                    Email = "*****@*****.**", UserName = "******"
                };
                await um.CreateAsync(user, "_Samil123");
            }

            IdentityRole role = await rm.FindByNameAsync("Administrator");

            if (role == null)
            {
                role = new IdentityRole("Administrator");
                await rm.CreateAsync(role);
            }

            bool inrole = await um.IsInRoleAsync(user, "Administrator");

            if (!inrole)
            {
                await um.AddToRoleAsync(user, "Administrator");
            }

            return;
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new ThesisDbUser {
                    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);

                    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());
        }