public async Task <IActionResult> RefreshAccessTokenAsync([FromBody] SecretDto dto) { var userProfile = _jwtApp.GetExistenceToken(dto.Token); var user = new UserDto { UserName = userProfile.Name, Id = userProfile.UserId, RoleName = userProfile.RoleNames }; var jwt = await _jwtApp.RefreshAsync(dto.Token, user); var data = new JwtResponseDto { Access = jwt.Token, Type = "Bearer", Profile = new Profile { RoleNames = user.RoleName, UserId = user.Id, UserName = user.UserName, Auths = jwt.Success ? jwt.AuthTime : 0, Expires = jwt.Success ? jwt.ExpTime : 0 } }; return(new JsonResult( new { code = 20000, data })); }
public async Task <IActionResult> Login(LoginDto login) { var user = await userManager.FindByNameAsync(login.Username); if (user != null && await userManager.CheckPasswordAsync(user, login.Password)) { var authClaims = new List <Claim> { new Claim("userId", user.Id), }; var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:Secret"])); var token = new JwtSecurityToken( issuer: _configuration["JWT:ValidIssuer"], audience: _configuration["JWT:ValidAudience"], expires: DateTime.Now.AddHours(3), claims: authClaims, signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256) ); var response = new JwtResponseDto() { Payload = new JwtSecurityTokenHandler().WriteToken(token) }; return(Ok(response)); } return(Unauthorized()); }
public JsonResult LoginAsync([FromBody] SecretDto dto) { //Todo:获取用户信息 //var user = new UserDto //{ // Id = Guid.NewGuid(), // UserName = "******", // Role = Guid.Empty, // Email = "*****@*****.**", // Phone = "13912345678", //}; var user = _loginApp.GetUserInfo(dto.UserName, dto.Password); if (user == null) { var datanull = new JwtResponseDto { Access = "无权访问", Type = "Bearer", Profile = new Profile { UserName = dto.UserName, Auths = 0, Expires = 0 } }; return(new JsonResult ( new { code = 20000, data = datanull } )); } var jwt = _jwtApp.Create(user); var data = new JwtResponseDto { Access = jwt.Token, Type = "Bearer", Profile = new Profile { RoleNames = user.RoleName, UserId = user.Id, UserName = user.UserName, Auths = jwt.AuthTime, Expires = jwt.ExpTime } }; return(new JsonResult (new { code = 20000, data })); }