Пример #1
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new IdentityUser {
                    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, returnUrl = returnUrl },
                                               protocol: Request.Scheme);

                    var callbackUrl2 = Url.Page("/Account/ConfirmEmail", null, new { userId = user.Id, code }, Request.Scheme);


                    await((EmailSender)_emailSender).SendEmailAsync(Input.Email, "Please confirm your email",
                                                                    $"Please confirm your account by clicking the button below.", "Confirm email", callbackUrl);

                    //Affiliate links
                    var affiliate = HttpContext.Session.GetString(_configuration["AffiliateKey"]);

                    if (affiliate != "")
                    {
                        _affiliatesService.SignupWithAffiliateLink(affiliate, user);
                    }
                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }));
                    }
                    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());
        }
Пример #2
0
        public async Task <IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (remoteError != null)
            {
                ErrorMessage = $"Error from external provider: {remoteError}";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            // Sign in the user with this external login provider if the user already has a login.
            var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true);

            if (result.Succeeded)
            {
                _logger.LogInformation("{Name} logged in with {LoginProvider} provider.", info.Principal.Identity.Name, info.LoginProvider);
                return(LocalRedirect(returnUrl));
            }
            if (result.IsLockedOut)
            {
                return(RedirectToPage("./Lockout"));
            }
            else
            {
                // If the user does not have an account, then ask the user to create an account.
                ReturnUrl = returnUrl;
                var email = "";
                if (info.LoginProvider == "Google")
                {
                    email = info.Principal.FindFirst(ClaimTypes.Email).Value;
                }

                var user = new IdentityUser {
                    UserName = email, Email = email, EmailConfirmed = true
                };

                var uresult = await _userManager.CreateAsync(user);

                if (uresult.Succeeded)
                {
                    uresult = await _userManager.AddLoginAsync(user, info);

                    if (uresult.Succeeded)
                    {
                        var affiliate = HttpContext.Session.GetString(_configuration["AffiliateKey"]);
                        if (affiliate != "")
                        {
                            _affiliatesService.SignupWithAffiliateLink(affiliate, user);
                        }
                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);

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

                        return(LocalRedirect(returnUrl));
                    }
                }
                ErrorMessage = "Error loading external login information.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }
        }