Ejemplo n.º 1
0
        public async Task <IActionResult> Login([FromBody] Models.LoginRequest login)
        {
            try
            {
                var user = await _userBL.CheckLogin(login);

                user.Role = await _roleBL.GetById(user.RoleId);

                var tokenDescriptor = new SecurityTokenDescriptor
                {
                    Subject = new ClaimsIdentity(new Claim[]
                    {
                        new Claim("UserID", user.UserId.ToString()),
                        new Claim("roles", user.Role.Name)
                    }),
                    Expires            = DateTime.UtcNow.AddMinutes(30),
                    SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(AppSettings.JWT_Secret)), SecurityAlgorithms.HmacSha256Signature)
                };
                var tokenHandler  = new JwtSecurityTokenHandler();
                var securityToken = tokenHandler.CreateToken(tokenDescriptor);
                var token         = tokenHandler.WriteToken(securityToken);
                return(Ok(new { token }));
            }
            catch (Exception e)
            {
                return(BadRequest(new { message = e.Message.ToString() }));
            }
        }
Ejemplo n.º 2
0
        public Task <string> CheckLogin(Models.LoginRequest loginInfo)
        {
            var user = _userRepos.GetAllIncluding(u => u.Role, u => u.Premises, u => u.Premises.PremisesType).Where(u => u.Username == loginInfo.Username).SingleOrDefault();

            if (user != null)
            {
                var isCorrectPassword = PasswordHasher.CheckHashedPassword(new Models.HashPassword()
                {
                    HashedPassword = user.Password,
                    Password       = loginInfo.Password,
                    Salt           = user.Salt
                });
                if (isCorrectPassword)
                {
                    if (!user.IsActive)
                    {
                        throw new DeActivedUsernameException(msg: MessageConstant.DEACTIVED_USER);
                    }
                    if (user.RoleId != RoleDataConstant.ADMIN_ID && user.RoleId != RoleDataConstant.VETERINARY_ID)
                    {
                        if (!user.Premises.IsActive)
                        {
                            throw new DeActivedPremisesException(msg: MessageConstant.DEACTIVED_PREMISES);
                        }
                    }
                    var roles = new List <string>
                    {
                        user.Role.Name
                    };
                    string premesisId = null;


                    ClaimsIdentity subject = new ClaimsIdentity();
                    subject.AddClaim(new Claim("userID", user.UserId.ToString()));
                    if (user.Premises != null)
                    {
                        roles.Add(user.Premises.PremisesType.Name);
                        premesisId = user.Premises.PremisesId.ToString();
                        subject.AddClaim(new Claim("premisesID", premesisId));
                    }
                    foreach (var role in roles)
                    {
                        subject.AddClaim(new Claim(ClaimTypes.Role, role));
                    }

                    var tokenDescriptor = new SecurityTokenDescriptor
                    {
                        Subject            = subject,
                        Expires            = DateTime.UtcNow.AddMinutes(30),
                        SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_appSettings.JWT_Secret)), SecurityAlgorithms.HmacSha256Signature)
                    };
                    var tokenHandler  = new JwtSecurityTokenHandler();
                    var securityToken = tokenHandler.CreateToken(tokenDescriptor);
                    var token         = tokenHandler.WriteToken(securityToken);
                    return(Task.FromResult(token));
                }
            }
            throw new InvalidUsernameOrPasswordException(msg: MessageConstant.WRONG_PASS_OR_USERNAME);
        }
Ejemplo n.º 3
0
        public async Task <User> CheckLogin(Models.LoginRequest loginInfo)
        {
            var user = await this._userRepos.FindByUsername(loginInfo.Username);

            if (user != null)
            {
                var isCorrectPassword = PasswordHasher.CheckHashedPassword(new Models.HashPassword()
                {
                    HashedPassword = user.Password,
                    Password       = loginInfo.Password,
                    Salt           = user.Salt
                });
                if (isCorrectPassword)
                {
                    return(user);
                }
            }
            throw new InvalidUsernameOrPasswordException("Wrong Username or Password");
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Login([FromBody] Models.LoginRequest login)
        {
            try
            {
                var token = await _userBL.CheckLogin(login);

                Entities.User user = null;
                if (token != null)
                {
                    user = await _userBL.FindByName(login.Username);
                }
                var loginReponse = new Models.UserLoginReponse()
                {
                    User  = _mapper.Map <Models.UserData>(user),
                    Token = token
                };
                return(Ok(new { Data = loginReponse }));
            }
            catch (Exception e)
            {
                return(BadRequest(new { message = e.Message, error = e.StackTrace }));
            }
        }