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 ApplicationUser {
                    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.UpdateExternalAuthenticationTokensAsync(info);

                        user.DiscordUser = await discordService.GetUserProfileAsync(userManager, user);

                        user.DiscordConnections = await discordService.GetUserConnectionsAsync(userManager, user);

                        user.UserName = user.DiscordUser.Username;
                        await userManager.UpdateAsync(user);

                        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());
        }
示例#2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var user = await userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{userManager.GetUserId(User)}'."));
            }

            //var tmp = await httpService.GetUserProfileAsync(userManager, user);
            //System.Diagnostics.Debug.WriteLine(tmp.Id);
            //var tmp2 = await httpService.GetUserConnectionsAsync(userManager, user);
            //System.Diagnostics.Debug.WriteLine(tmp2.FirstOrDefault().Id);

            user.DiscordUser = await httpService.GetUserProfileAsync(userManager, user);

            user.DiscordConnections = await httpService.GetUserConnectionsAsync(userManager, user);

            await userManager.UpdateAsync(user);

            var email = await userManager.GetEmailAsync(user);

            if (Input.Email != email)
            {
                var setEmailResult = await userManager.SetEmailAsync(user, Input.Email);

                if (!setEmailResult.Succeeded)
                {
                    var userId = await userManager.GetUserIdAsync(user);

                    throw new InvalidOperationException($"Unexpected error occurred setting email for user with ID '{userId}'.");
                }
            }

            await signInManager.RefreshSignInAsync(user);

            StatusMessage = "Your profile has been updated";
            return(RedirectToPage());
        }