public async Task <ActionResult <ResponseData> > Login(LoginInfo loginInfo) { string jwtStr = string.Empty; var user = await _userInfoService.CheckUserPassword(loginInfo.userName, loginInfo.password); if (user != null) { //如果是基于用户的授权策略,这里要添加用户;如果是基于角色的授权策略,这里要添加角色 var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, user.UserName), new Claim(ClaimTypes.PrimarySid, user.Id.ToString()), new Claim(ClaimTypes.Role, await _userInfoService.GetUserPermission(user)), new Claim(ClaimTypes.Expiration, DateTime.Now.AddSeconds(_audienceConfiguration.Expiration).ToString()) }; //用户标识 var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme); identity.AddClaims(claims); var token = JWTHelper.BuildJwtToken(claims.ToArray(), _audienceConfiguration); //_redisCacheManager.Set(user.UserName, user, TimeSpan.FromMinutes(10)); //_redisCacheManager.Get<UserInfo>(user.UserName); _responseData.Success = true; _responseData.Data = token; } else { _responseData.Success = false; _responseData.Message = _stringLocalizer["ErrorMsg"]; } return(_responseData); }
public ActionResult <ResponseData> Login(LoginInfo loginInfo) { string jwtStr = string.Empty; var user = _userInfoAppService.CheckUserPassword(loginInfo.userName, loginInfo.password); if (user != null) { var userRoles = user.Roles?.Select(x => x.RoleId); //如果是基于用户的授权策略,这里要添加用户;如果是基于角色的授权策略,这里要添加角色 var claims = new List <Claim> { new Claim(ClaimTypes.Name, user.UserName), new Claim(ClaimTypes.PrimarySid, user.Id.ToString()), new Claim(ClaimTypes.Expiration, DateTime.Now.AddSeconds(_audienceConfiguration.Expiration).ToString()) }; //用户标识 var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme); identity.AddClaims(claims); var token = JWTHelper.BuildJwtToken(claims.ToArray(), _audienceConfiguration); _responseData.Success = true; _responseData.Data = token; } else { _responseData.Success = false; _responseData.Message = _stringLocalizer["ErrorMsg"]; } return(_responseData); }