コード例 #1
0
        public async Task <ApiResult> Handle(ExternalRegister request, CancellationToken cancellationToken)
        {
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                // return RedirectToPage("./Login", new { ReturnUrl = returnUrl });
                return(new ApiResult {
                    Success = false, Message = "Error loading external login information during confirmation."
                });
            }

            var user = new User
            {
                Name      = request.Email,
                UserName  = request.Email,
                FirstName = request.FirstName,
                LastName  = request.LastName,
                Email     = request.Email
            };

            var userRepository = _classifierRepositoryFactory.GetNamedOrDefaultService(ClassifierTypeCode.User);

            var result = await userRepository.Insert(user, cancellationToken);

            if (result.Success)
            {
                // todo: remove reload user
                var dbUser = await _userManager.FindByIdAsync(result.Uid.ToString());

                var identityResult = await _userManager.AddLoginAsync(dbUser, info);

                if (identityResult.Succeeded)
                {
                    await _signInManager.SignInAsync(dbUser, isPersistent : false);

                    _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);

                    await _emailConfirmationService.SendConfirmEmailMessage(dbUser, cancellationToken);

                    // todo: check redirect is local
                    return(new ApiResult {
                        RedirectUrl = request.ReturnUrl
                    });
                }
            }

            return(result);
        }
コード例 #2
0
        public async Task <ApiResult> Handle(SendEmailConfirmation request, CancellationToken cancellationToken)
        {
            var user = request.User != null
                                ? await _userManager.GetUserAsync(request.User)
                                : await _userManager.FindByEmailAsync(request.Email);

            if (user != null)
            {
                await _emailConfirmationService.SendConfirmEmailMessage(user, cancellationToken);
            }

            return(new ApiResult {
                Message = await _localizer.Get <SendEmailConfirmation.Resources>(x => x.Success, cancellationToken)
            });
        }
コード例 #3
0
        public async Task <ApiResult> Handle(Register request, CancellationToken cancellationToken)
        {
            var user = new User
            {
                Name      = request.Email,
                UserName  = request.Email,
                FirstName = request.FirstName,
                LastName  = request.LastName,
                Email     = request.Email,
                Password  = request.Password
            };

            var userRepository = _classifierRepositoryFactory.GetNamedOrDefaultService(ClassifierTypeCode.User);

            var result = await userRepository.Insert(user, cancellationToken);

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

                // todo: remove reload user
                var dbUser = await _userManager.FindByIdAsync(result.Uid.ToString());

                await _emailConfirmationService.SendConfirmEmailMessage(dbUser, cancellationToken);

                if (_userManager.Options.SignIn.RequireConfirmedAccount)
                {
                    var redirectUrl = _appUrlBuilder.Build(ClientRoutes.RegisterConfirmation,
                                                           new Dictionary <string, string> {
                        { "email", request.Email }
                    });

                    return(new ApiResult {
                        RedirectUrl = redirectUrl
                    });
                }

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

                return(new ApiResult {
                    RedirectUrl = request.ReturnUrl
                });
            }

            return(result);
        }