public async Task <IActionResult> SendActivationEmail([FromBody] SendEmailActivationUserRequest request)
        {
            var result = await _usersService.ActivateAccountSendEmail(request);

            if (!result.Success)
            {
                return(BadRequest(result.Errors));
            }
            return(Ok());
        }
Пример #2
0
        public async Task <UserModelResult> ActivateAccountSendEmail(SendEmailActivationUserRequest request)
        {
            var user = await _db.Users.FirstAsync(u => u.Id == request.Id).ConfigureAwait(false);

            if (user != null && user.Active)
            {
                return(new UserModelResult("Account already activated!"));
            }

            var identity = await _userManager.Users.FirstAsync(u => u.Id == user.AspNetUserId).ConfigureAwait(false);

            var token = await _userManager.GenerateEmailConfirmationTokenAsync(identity);

            var template = await GetUserEmailAsync(identity, user, _templateData.ActivateAccountEmailFileName, "activate", token);

            await _emailClient.Send(identity.Email, "Activate account notification", template);

            return(new UserModelResult {
                Success = true
            });
        }
Пример #3
0
        public async Task <Response <UserModel> > ActivateAccountSendEmail(SendEmailActivationUserRequest request, ClaimsPrincipal principal)
        {
            var user = await _usersRepository.GetById(request.Id);

            if (user != null && user.Active)
            {
                return(Response.ForError("Account already activated."));
            }

            if (user.PartyId != principal.GetPartyId() && !principal.IsSchemeOwner())
            {
                return(Response.ForError("Missing permissions."));
            }

            var identity = await _userManager.Users.FirstAsync(u => u.Id == user.AspNetUserId);

            var token = await _userManager.GenerateEmailConfirmationTokenAsync(identity);

            var template = await GetUserEmailAsync(identity, user, _templateData.ActivateAccountEmailFileName, "activate", token);

            await _emailClient.Send(identity.Email, "Activate account notification", template);

            return(Response <UserModel> .ForSuccess());
        }
        public async Task <ActionResult> SendActivationEmail([FromBody] SendEmailActivationUserRequest request)
        {
            var response = await _identityService.ActivateAccountSendEmail(request, User);

            return(FromResponse(response));
        }
Пример #5
0
        public async Task <UserModelResult> Create(CreateUserRequest request)
        {
            var identity = new AspNetUser
            {
                Id       = Guid.NewGuid().ToString(),
                UserName = request.Username,
                Email    = request.Email,
            };

            var validateResult = ValidateRequest(request);

            if (!validateResult.Success)
            {
                return(validateResult);
            }

            var result = await _userManager.CreateAsync(identity).ConfigureAwait(false);

            if (!result.Succeeded)
            {
                return(new UserModelResult {
                    Success = false, Errors = result.Errors.Select(e => e.Description).ToArray()
                });
            }

            foreach (var role in request.Roles)
            {
                await _userManager.AddToRoleAsync(identity, role).ConfigureAwait(false);
            }

            var user = new User
            {
                AspNetUserId = identity.Id,
                Name         = request.Username,
                CreatedDate  = DateTime.Now,
                PartyId      = request.PartyId,
                PartyName    = request.PartyName
            };

            await _db.Users.AddAsync(user).ConfigureAwait(false);

            await _db.SaveChangesAsync().ConfigureAwait(false);

            var emailRequest = new SendEmailActivationUserRequest {
                Id = user.Id
            };
            var sendEmail = await ActivateAccountSendEmail(emailRequest);

            if (sendEmail.Success)
            {
                return(new UserModelResult
                {
                    Success = true,
                    Model = UserModel.Create(identity, user, request.Roles)
                });
            }

            try
            {
                _db.Users.Remove(user);
                await _db.SaveChangesAsync().ConfigureAwait(false);

                await _userManager.DeleteAsync(identity).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.LogError("unable to delete invalid account: ", ex);
                return(new UserModelResult("unable to create account"));
            }

            return(new UserModelResult {
                Success = false, Errors = result.Errors.Select(e => e.Description).ToArray()
            });
        }
Пример #6
0
        public Task <Response <UserModel> > ActivateAccountSendEmail(SendEmailActivationUserRequest request, ClaimsPrincipal principal)
        {
            Response <UserModel> response = Response.ForError("");

            return(Task.FromResult(response));
        }