public async Task <IActionResult> Register([FromBody] RegisterRequest model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (model.Password != model.ConfirmPassword)
            {
                return(BadRequest());
            }

            int openUserNumber = await _userManager.GetOpenDisplayNumber(model.DisplayName);

            var user   = new FantasyCriticUser(Guid.NewGuid(), model.DisplayName, openUserNumber, model.EmailAddress, model.EmailAddress, false, "", "", _clock.GetCurrentInstant());
            var result = await _userManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return(BadRequest(result.Errors));
            }

            _logger.LogInformation("User created a new account with password.");

            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            string baseURL = $"{Request.Scheme}://{Request.Host.Value}";
            await _emailSender.SendConfirmationEmail(user, code, baseURL);

            return(Created("", user.UserID.ToString()));
        }
Example #2
0
    public async Task <IActionResult> OnPostAsync(string returnUrl = null)
    {
        returnUrl ??= Url.Content("~/");

        var externalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

        GoogleLogin    = externalLogins.SingleOrDefault(x => x.Name == "Google");
        MicrosoftLogin = externalLogins.SingleOrDefault(x => x.Name == "Microsoft");
        TwitchLogin    = externalLogins.SingleOrDefault(x => x.Name == "Twitch");
        DiscordLogin   = externalLogins.SingleOrDefault(x => x.Name == "Discord");

        if (ModelState.IsValid)
        {
            var user = new FantasyCriticUser {
                Id = Guid.NewGuid(), UserName = Input.DisplayName, Email = Input.Email
            };
            var result = await _userManager.CreateAsync(user, Input.Password);

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

                var fullUser = await _userManager.FindByIdAsync(user.Id.ToString());

                var confirmLink = await LinkBuilder.GetConfirmEmailLink(_userManager, fullUser, Request);

                await _emailSendingService.SendConfirmationEmail(fullUser, confirmLink);

                if (_userManager.Options.SignIn.RequireConfirmedAccount)
                {
                    return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }));
                }
                else
                {
                    await _signInManager.SignInAsync(fullUser, 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());
    }
    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 FantasyCriticUser {
                Id = Guid.NewGuid(), UserName = Input.DisplayName, Email = Input.Email
            };

            var result = await _userManager.CreateAsync(user);

            if (result.Succeeded)
            {
                var fullUser = await _userManager.FindByIdAsync(user.Id.ToString());

                result = await _userManager.AddLoginAsync(fullUser, info);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                    var confirmLink = await LinkBuilder.GetConfirmEmailLink(_userManager, fullUser, Request);

                    await _emailSendingService.SendConfirmationEmail(fullUser, confirmLink);

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

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

        ProviderDisplayName = info.ProviderDisplayName;
        ReturnUrl           = returnUrl;
        return(Page());
    }