Exemplo n.º 1
0
        public async Task <UserManagerResponse> LoginUserAsync(LoginModel model)
        {
            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                return(new UserManagerResponse {
                    Message = "There is no user with this Email Address",
                    IsSuccess = false
                });
            }

            var result = await _userManager.CheckPasswordAsync(user, model.Password);

            if (!result)
            {
                return new UserManagerResponse {
                           Message   = "Invalid Password",
                           IsSuccess = false
                }
            }
            ;

            var loginDetails = _dbContext.Users.FirstOrDefault(p => p.Email == model.Email);

            if (loginDetails.UserType != "User")
            {
                return(new UserManagerResponse {
                    Message = "Not an admin",
                    IsSuccess = false
                });
            }

            var claims = new[]
            {
                new Claim("Email", model.Email),
                new Claim(ClaimTypes.NameIdentifier, user.Id),
            };

            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JwtAuthentication:Key"]));

            var token = new JwtSecurityToken(
                issuer: _configuration["JwtAuthentication:Issuer"],
                audience: _configuration["JwtAuthentication:Audience"],
                claims: claims,
                expires: DateTime.Now.AddDays(30),
                signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
                );

            string tokenString = new JwtSecurityTokenHandler().WriteToken(token);

            return(new UserManagerResponse {
                Message = tokenString,
                IsSuccess = true,
                ExpirationDate = token.ValidTo,
                Data = _mapper.Map <UsersDto>(loginDetails)
            });
        }

        // public async Task<UserManagerResponse> ConfirmEmailAsync(string userId, string token)
        // {
        //     var user = await _userManager.FindByIdAsync(userId);
        //     if(user == null) {
        //         return new UserManagerResponse{
        //             Message = "User does not exist",
        //             IsSuccess = false
        //         };
        //     }

        //     var decodedToken = WebEncoders.Base64UrlDecode(token);
        //     var normalToken = Encoding.UTF8.GetString(decodedToken);

        //     var result = await _userManager.ConfirmEmailAsync(user, normalToken);

        //     if(result.Succeeded) {
        //         return new UserManagerResponse{
        //             Message = "Email Confirmed Successfully",
        //             IsSuccess = true
        //         };
        //     }

        //     return new UserManagerResponse{
        //         Message = "Email Is not confirmed",
        //         IsSuccess = false,
        //         Errors = result.Errors.Select(e => e.Description)
        //     };
        // }

        // public async Task<UserManagerResponse> ForgotPasswordAsync(string email)
        // {
        //     var user = await _userManager.FindByEmailAsync(email);
        //     if (user == null) {
        //         return new UserManagerResponse{
        //             IsSuccess = false,
        //             Message  = "No User associated with this email"
        //         };
        //     }

        //     var token = await _userManager.GeneratePasswordResetTokenAsync(user);
        //     var encodedToken = Encoding.UTF8.GetBytes(token);
        //     var validToken = WebEncoders.Base64UrlEncode(encodedToken);

        //     string url = $"{_configuration["AppUrl"]}/reset_password?email={email}&token={validToken}";

        //     await _mailService.SendEmailAsync(
        //                 email, "*****@*****.**",
        //                 "Reset Password",
        //                 $"<h1>Follow the instructions to reset password</h1><p>To reset your password <a href='{url}'>Click here</a></p>"
        //             );
        //     return new UserManagerResponse{
        //         IsSuccess = true,
        //         Message = "Reset password url has been sent to provided email"
        //     };
        // }

        // public async Task<UserManagerResponse> LoginUserAsync(LoginViewModel model)
        // {
        //     var user = await _userManager.FindByEmailAsync(model.Email);

        //     if (user == null) {
        //         return new UserManagerResponse {
        //             Message = "There is no user with this Email Address",
        //             IsSuccess = false
        //         };
        //     }

        //     var result = await _userManager.CheckPasswordAsync(user, model.Password);

        //     if(!result)
        //         return new UserManagerResponse {
        //             Message = "Invalid Password",
        //             IsSuccess = false
        //         };

        //     var claims = new[]
        //     {
        //         new Claim("Email", model.Email),
        //         new Claim(ClaimTypes.NameIdentifier, user.Id),

        //     };

        //     var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JwtAuthentication:Key"]));

        //     var token = new JwtSecurityToken(
        //         issuer: _configuration["JwtAuthentication:Issuer"],
        //         audience: _configuration["JwtAuthentication:Audience"],
        //         claims: claims,
        //         expires: DateTime.Now.AddDays(30),
        //         signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
        //     );

        //     string tokenString = new JwtSecurityTokenHandler().WriteToken(token);

        //     return new UserManagerResponse{
        //         Message = tokenString,
        //         IsSuccess = true,
        //         ExpirationDate = token.ValidTo
        //     };
        // }
        // public async Task<UserManagerResponse> ResetPasswordAsync(ResetPassword model)
        // {
        //      var user = await _userManager.FindByEmailAsync(model.EmailAddress);
        //     if (user == null) {
        //         return new UserManagerResponse{
        //             IsSuccess = false,
        //             Message  = "No User associated with this email"
        //         };
        //     }

        //     if (model.NewPassword != model.ConfirmPassword) return new UserManagerResponse{
        //             IsSuccess = false,
        //             Message  = "Password does not match Confirmation"
        //         };

        //     var decodedToken = WebEncoders.Base64UrlDecode(model.Token);
        //     var normalToken = Encoding.UTF8.GetString(decodedToken);

        //     var result = await _userManager.ResetPasswordAsync(user, normalToken, model.NewPassword);

        //     if(result.Succeeded) {
        //         return new UserManagerResponse{
        //             IsSuccess = true,
        //             Message = "PAssword has been reset"
        //         };
        //     }

        //     return new UserManagerResponse{
        //             IsSuccess = true,
        //             Message = "Soemthing went wrong",
        //             Errors = result.Errors.Select(e => e.Description)
        //         };
        // }
    }
Exemplo n.º 2
0
 public int Login(LoginModel model, bool isLoginAdmin = false)
 {
     throw new NotImplementedException();
 }