コード例 #1
0
        private async Task LoadSharedKeyAndQrCodeUriAsync(CakeItUser 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);
        }
コード例 #2
0
        private void SeedAdmin(IServiceProvider provider)
        {
            var signInManager = provider.GetRequiredService <SignInManager <CakeItUser> >();
            var configuration = provider.GetRequiredService <IConfiguration>();

            // Seed User admin
            var email = configuration["AdminUser:Email"];

            var pass = configuration["AdminUser:Password"];

            var user = new CakeItUser
            {
                UserName = email,
                Email    = email
            };

            var result = signInManager.UserManager.CreateAsync(user, pass).Result;

            var role = signInManager.UserManager.AddToRoleAsync(user, "Admin").Result;
        }
コード例 #3
0
        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 CakeItUser {
                    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());
        }
コード例 #4
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new CakeItUser {
                    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.");

                    IdentityResult asigneRoleResult = await _userManager.AddToRoleAsync(user, "User");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    string content = CreateEmaiContent(Input.Email, callbackUrl);

                    SendEmailDetails details = new SendEmailDetails()
                    {
                        FromEmail = this._provider.GetService <IConfiguration>()["VerificationEmailDetails:FromEmail"],
                        FromName  = this._provider.GetService <IConfiguration>()["VerificationEmailDetails:FromName"],
                        ToEmail   = Input.Email,
                        ToName    = Input.Email,
                        Subject   = "Confirm your email.",
                        Content   = content
                    };

                    var sendEmailResult = await this._provider.GetService <ICustomEmilSender>().SendEmailAsync(details);

                    if (!sendEmailResult.Successful)
                    {
                        var mailErrors = sendEmailResult.Errors;

                        this._provider.GetService <IErrorService>().PassErrorParam(mailErrors);

                        return(RedirectToPage("/Error"));
                    }

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }

                var errors = this.ModelState.Values.SelectMany(p => p.Errors).Select(e => e.ErrorMessage).ToArray();

                this._provider.GetService <IErrorService>().PassErrorParam(errors);

                return(RedirectToPage("/Error"));
            }

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