public async Task <IActionResult> Login(UserModel model)
        {
            UserModel user = UserComponent.GetUser(model.UserName);

            if (UserComponent.CheckUser(user, model))
            {
                var tokenDescriptor = new SecurityTokenDescriptor
                {
                    Subject = new ClaimsIdentity(new Claim[]
                    {
                        new Claim("UserID", user.Id.ToString())
                    }),
                    Expires = DateTime.UtcNow.AddDays(1),
                };
                var tokenHandler  = new JwtSecurityTokenHandler();
                var securityToken = tokenHandler.CreateToken(tokenDescriptor);
                var token         = tokenHandler.WriteToken(securityToken);

                return(Ok(new { token }));
            }
            else
            {
                return(BadRequest(new { message = "Username or password is incorrect." }));
            }
        }
Example #2
0
 public UserModel GetUserProfile(string username)
 {
     return(UserComponent.GetUser(username));
 }