public static async Task InitializeAsync(UserManager <IdentityBuyer> userManager, RoleManager <IdentityRole> roleManager)
        {
            if (await roleManager.FindByNameAsync(RolesNames.Admin) == null)
            {
                await roleManager.CreateAsync(new IdentityRole(RolesNames.Admin));
            }

            if (await roleManager.FindByNameAsync(RolesNames.User) == null)
            {
                await roleManager.CreateAsync(new IdentityRole(RolesNames.User));
            }

            if (await userManager.FindByNameAsync(AdminEmail) == null)
            {
                IdentityBuyer admin = new IdentityBuyer {
                    Email = AdminEmail, UserName = AdminEmail, BuyerId = 1
                };
                IdentityResult result = await userManager.CreateAsync(admin, Password);

                if (result.Succeeded)
                {
                    await userManager.AddToRoleAsync(admin, RolesNames.Admin);
                }
            }
        }
Exemple #2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new IdentityBuyer {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                await _userManager.AddToRoleAsync(user, RolesNames.User);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email }));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(RedirectToAction("Create", "Buyers"));
                    }
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }