示例#1
0
		public async Task SendConfirmEmailMessage(DbUser user, CancellationToken cancellationToken)
		{
			var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

			code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));

			var messageModel = new ConfirmEmailMessageModel
			{
				CallbackUrl = _appUrlBuilder.Build($"{ClientRoutes.ConfirmEmail}/{user.Id}/{code}")
			};

			var message = await _templateRenderer.Render(MessageTemplateCode.EmailConfirmation, messageModel, cancellationToken);

			await _emailSender.Send(user.Email, message.Subject, message.Body, cancellationToken);
		}
示例#2
0
        public async Task <ChallengeResult> Handle(LinkLogin request, CancellationToken cancellationToken)
        {
            // Clear the existing external cookie to ensure a clean login process
            await _httpContextAccessor.HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

            // Request a redirect to the external login provider to link a login for the current user
            var redirectUrl = _appUrlBuilder.Build(ClientRoutes.LinkLogin);

            var properties = _signInManager.ConfigureExternalAuthenticationProperties(request.Provider, redirectUrl, _userManager.GetUserId(request.User));

            return(new ChallengeResult(request.Provider, properties));
        }
示例#3
0
        public Task <ChallengeResult> Handle(ExternalLogin request, CancellationToken cancellationToken)
        {
            // todo: make returnUrl constant
            var redirectUrl = _appUrlBuilder.Build(ClientRoutes.ExternalLogin,
                                                   new Dictionary <string, string> {
                { "returnUrl", request.ReturnUrl ?? "/" }
            });

            var properties = _signInManager.ConfigureExternalAuthenticationProperties(request.Provider, redirectUrl);

            var result = new ChallengeResult(request.Provider, properties);

            return(Task.FromResult(result));
        }
示例#4
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);
        }
示例#5
0
        public async Task <ApiResult> Handle(ForgotPassword request, CancellationToken cancellationToken)
        {
            var user = await _userManager.FindByEmailAsync(request.Email);

            if (user != null && await _userManager.IsEmailConfirmedAsync(user))
            {
                var code = await _userManager.GeneratePasswordResetTokenAsync(user);

                code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));

                var messageModel = new ConfirmEmailMessageModel
                {
                    CallbackUrl = _appUrlBuilder.Build($"{ClientRoutes.ResetPassword}/{code}")
                };

                var message = await _templateRenderer.Render(MessageTemplateCode.PasswordReset, messageModel, cancellationToken);

                await _emailSender.Send(user.Email, message.Subject, message.Body, cancellationToken);
            }

            return(new ApiResult {
                Message = "Please check your email to reset your password."
            });
        }
示例#6
0
        public async Task <ApiResult> Handle(SetupSystem request, CancellationToken cancellationToken)
        {
            var appOptions = _optionsMonitor.CurrentValue;

            if (appOptions.State == AppState.Initialized)
            {
                return(new ApiResult
                {
                    Success = false,
                    Message = await _localizer.Get("page.setup.initializedMessage", cancellationToken)
                });
            }

            using (var scope = _unitOfWorkFactory.Create())
            {
                _logger.LogInformation($"Creating default administrator {request.AdminEmail}", request.AdminEmail);

                var user = new User
                {
                    Name     = request.AdminEmail,
                    UserName = request.AdminEmail,
                    Password = request.AdminPassword,
                    Email    = request.AdminEmail
                };

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

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

                if (userResult.Success == false)
                {
                    return(userResult);
                }

                var signInResult = await _signInManager.PasswordSignIn(user.UserName, request.AdminPassword, true, false);

                if (signInResult.Success == false)
                {
                    return(signInResult);
                }

                var userUid = userResult.Uid;

                _logger.LogInformation($"Creating default company {request.CompanyName}", request.CompanyName);

                // todo: create company without mediator and company request
                var companyResult = await _mediator.Send(new CreateCompany
                {
                    UserUid = userUid,
                    Item    = new Company
                    {
                        Name       = request.CompanyName,
                        ConfigCode = CompanyConfigCode.Company                         // todo: register and use allowed company types
                    }
                }, cancellationToken);

                if (companyResult.Success == false)
                {
                    return(companyResult);
                }

                _logger.LogInformation("Updating application initialization options");

                await _settingsRepository.GetSettings <AppOptions>()
                .Set(x => x.State, AppState.Initialized)
                .Set(x => x.SuperUserId, userUid)
                .Update(cancellationToken);

                scope.Commit();

                return(new ApiResult {
                    RedirectUrl = _appUrlBuilder.Build(ClientRoutes.Dashboard)
                });
            }
        }