Exemplo n.º 1
0
        public async Task <AuthResponse> ConfirmChangeEmail(ConfirmChangeEmailRequestModel model, string currentEmail)
        {
            AuthUser user = await this.userManager.FindByEmailAsync(currentEmail);

            if (user == null)
            {
                return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("User not found", AuthErrorCodes.UserDoesNotExist));
            }
            else
            {
                IdentityResult result = await this.userManager.ChangeEmailAsync(user, user.NewEmail, System.Net.WebUtility.UrlDecode(model.Token));

                if (result.Succeeded)
                {
                    user.NewEmail = string.Empty;

                    var updateResult = await this.userManager.UpdateAsync(user);

                    if (updateResult.Succeeded)
                    {
                        return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponse());
                    }
                    else
                    {
                        return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse(updateResult, AuthErrorCodes.UnableToUpdateUser));
                    }
                }
                else
                {
                    return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse(result, AuthErrorCodes.UnableToChangeEmail));
                }
            }
        }
Exemplo n.º 2
0
        public async Task <AuthResponse> ResetPassword(ResetPasswordRequestModel model)
        {
            AuthUser user = await this.userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("User not found", AuthErrorCodes.UserDoesNotExist));
            }
            else
            {
                string confirmationToken = await this.userManager.GeneratePasswordResetTokenAsync(user);

                string confirmationLink = $"{this.apiSettings.ExternalBaseUrl}/confirmpasswordreset/{user.Id}/{UrlEncoder.Default.Encode(confirmationToken)}";

                string command = "Click this link to reset your password";

                if (this.apiSettings.DebugSendAuthEmailsToClient)
                {
                    return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponseWithLink(command, confirmationLink));
                }
                else
                {
                    string email = this.FormatLink(command, confirmationLink);

                    await this.emailService.SendEmailAsync(model.Email, "Password Reset", email);

                    return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponse("Click the link sent to the provided email to reset your password"));
                }
            }
        }
Exemplo n.º 3
0
        public async Task <AuthResponse> Register(RegisterRequestModel model)
        {
            IdentityUser existingUser = await this.userManager.FindByEmailAsync(model.Email);

            if (existingUser == null)
            {
                AuthUser user = new AuthUser
                {
                    Email    = model.Email,
                    UserName = model.Email,
                    Id       = this.guidService.NewGuid().ToString()
                };

                IdentityResult result = await this.userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    IdentityResult addPasswordResult = await this.userManager.AddPasswordAsync(user, model.Password);

                    if (addPasswordResult.Succeeded)
                    {
                        string confirmationToken = await this.userManager.GenerateEmailConfirmationTokenAsync(user);

                        string confirmationLink = $"{this.apiSettings.ExternalBaseUrl}/confirmregistration/{user.Id}/{UrlEncoder.Default.Encode(confirmationToken)}";

                        string command = "Click this link to complete registration";

                        string email = this.FormatLink(command, confirmationLink);

                        await this.emailService.SendEmailAsync(model.Email, "Registration", email);

                        if (this.apiSettings.DebugSendAuthEmailsToClient)
                        {
                            return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponseWithLink(command, confirmationLink));
                        }
                        else
                        {
                            return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponse("Click the link sent to the provided email to complete registration"));
                        }
                    }
                    else
                    {
                        return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse(addPasswordResult, AuthErrorCodes.UnableToRegister));
                    }
                }
                else
                {
                    return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse(result, AuthErrorCodes.UnableToRegister));
                }
            }
            else
            {
                return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("User already exists", AuthErrorCodes.UserAlreadyExists));
            }
        }
Exemplo n.º 4
0
        public virtual async Task <ActionResponse> Delete(
            int id)
        {
            ActionResponse response = ValidationResponseFactory <object> .ActionResponse(await this.PostHistoryTypeModelValidator.ValidateDeleteAsync(id));

            if (response.Success)
            {
                await this.PostHistoryTypeRepository.Delete(id);

                await this.mediator.Publish(new PostHistoryTypeDeletedNotification(id));
            }

            return(response);
        }
Exemplo n.º 5
0
        public async Task <AuthResponse> ChangeEmail(ChangeEmailRequestModel model, string currentEmail)
        {
            AuthUser user = await this.userManager.FindByEmailAsync(currentEmail);

            if (user == null)
            {
                return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("User not found", AuthErrorCodes.UserDoesNotExist));
            }
            else
            {
                if (this.userManager.PasswordHasher.VerifyHashedPassword(user, user.PasswordHash, model.Password) == PasswordVerificationResult.Success)
                {
                    user.NewEmail = model.NewEmail;

                    IdentityResult updateResult = await this.userManager.UpdateAsync(user);

                    if (updateResult.Succeeded)
                    {
                        string confirmationToken = await this.userManager.GenerateChangeEmailTokenAsync(user, model.NewEmail);

                        string confirmationLink = $"{this.apiSettings.ExternalBaseUrl}/confirmchangeemail/{user.Id}/{UrlEncoder.Default.Encode(confirmationToken)}";

                        string command = "Click the link to change your email";

                        string email = this.FormatLink(command, confirmationLink);

                        await this.emailService.SendEmailAsync(model.NewEmail, "Change Email", email);

                        if (this.apiSettings.DebugSendAuthEmailsToClient)
                        {
                            return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponseWithLink(command, confirmationLink));
                        }
                        else
                        {
                            return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponse("Click the link sent to the provided email to complete changing your account email"));
                        }
                    }
                    else
                    {
                        return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("Unable to update user", AuthErrorCodes.UnableToUpdateUser));
                    }
                }
                else
                {
                    return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("Invalid email or password", AuthErrorCodes.InvalidUsernameOrPassword));
                }
            }
        }
Exemplo n.º 6
0
        public virtual async Task <CreateResponse <ApiPostHistoryTypeServerResponseModel> > Create(
            ApiPostHistoryTypeServerRequestModel model)
        {
            CreateResponse <ApiPostHistoryTypeServerResponseModel> response = ValidationResponseFactory <ApiPostHistoryTypeServerResponseModel> .CreateResponse(await this.PostHistoryTypeModelValidator.ValidateCreateAsync(model));

            if (response.Success)
            {
                PostHistoryType record = this.DalPostHistoryTypeMapper.MapModelToEntity(default(int), model);
                record = await this.PostHistoryTypeRepository.Create(record);

                response.SetRecord(this.DalPostHistoryTypeMapper.MapEntityToModel(record));
                await this.mediator.Publish(new PostHistoryTypeCreatedNotification(response.Record));
            }

            return(response);
        }
Exemplo n.º 7
0
        public async Task <AuthResponse> ConfirmPasswordReset(ConfirmPasswordResetRequestModel model)
        {
            AuthUser user = await this.userManager.FindByIdAsync(model.Id);

            if (user == null)
            {
                return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("User not found", AuthErrorCodes.UserDoesNotExist));
            }
            else
            {
                IdentityResult result = await this.userManager.ResetPasswordAsync(user, System.Net.WebUtility.UrlDecode(model.Token), model.NewPassword);

                if (result.Succeeded)
                {
                    return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponse());
                }
                else
                {
                    return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse(result, AuthErrorCodes.UnableToConfirmPasssordReset));
                }
            }
        }
Exemplo n.º 8
0
        public async Task <AuthResponse> ChangePassword(ChangePasswordRequestModel model, string email)
        {
            AuthUser user = await this.userManager.FindByEmailAsync(email);

            if (user == null)
            {
                return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("User not found", AuthErrorCodes.UserDoesNotExist));
            }
            else
            {
                IdentityResult result = await this.userManager.ChangePasswordAsync(user, model.CurrentPassword, model.NewPassword);

                if (result.Succeeded)
                {
                    return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponse());
                }
                else
                {
                    return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse(result, AuthErrorCodes.UnableToChangePassword));
                }
            }
        }
Exemplo n.º 9
0
        public virtual async Task <UpdateResponse <ApiPostHistoryTypeServerResponseModel> > Update(
            int id,
            ApiPostHistoryTypeServerRequestModel model)
        {
            var validationResult = await this.PostHistoryTypeModelValidator.ValidateUpdateAsync(id, model);

            if (validationResult.IsValid)
            {
                PostHistoryType record = this.DalPostHistoryTypeMapper.MapModelToEntity(id, model);
                await this.PostHistoryTypeRepository.Update(record);

                record = await this.PostHistoryTypeRepository.Get(id);

                ApiPostHistoryTypeServerResponseModel apiModel = this.DalPostHistoryTypeMapper.MapEntityToModel(record);
                await this.mediator.Publish(new PostHistoryTypeUpdatedNotification(apiModel));

                return(ValidationResponseFactory <ApiPostHistoryTypeServerResponseModel> .UpdateResponse(apiModel));
            }
            else
            {
                return(ValidationResponseFactory <ApiPostHistoryTypeServerResponseModel> .UpdateResponse(validationResult));
            }
        }
Exemplo n.º 10
0
        public async Task <AuthResponse> Login(LoginRequestModel model)
        {
            AuthUser user = await this.userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("Invalid email or password", AuthErrorCodes.InvalidUsernameOrPassword));
            }
            else
            {
                if (this.userManager.PasswordHasher.VerifyHashedPassword(user, user.PasswordHash, model.Password) == PasswordVerificationResult.Success)
                {
                    IList <Claim> claims = await this.userManager.GetClaimsAsync(user);

                    IList <string> roles = await this.userManager.GetRolesAsync(user);

                    foreach (string role in roles)
                    {
                        claims.Add(new Claim(ClaimTypes.Role, role));
                    }

                    string token = this.jwtService.GenerateBearerToken(
                        this.apiSettings.JwtSettings.SigningKey,
                        this.apiSettings.JwtSettings.Audience,
                        this.apiSettings.JwtSettings.Issuer,
                        user.Id,
                        user.Email,
                        claims);

                    return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponseWithToken(token));
                }
                else
                {
                    return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("Invalid email or password", AuthErrorCodes.InvalidUsernameOrPassword));
                }
            }
        }