.net提供了內建的介面除錯方式
1.新建.net core web api控制檯應用程式
2.封裝好jwt驗證機制
token令牌驗證機制
/// <summary>
/// 登入
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task<UserResponse> LoginAsync(UserInfoRequest request)
{
UserResponse userResponse = null;
UserInfoVo user = await _userRepository.GetUserInfoByUserCodeAsync(request.UserCode);
if (user == null)
{
throw new Exception("使用者名稱不存在");
}
if (user.Status == -1)
{
throw new Exception("賬戶被鎖定");
}
if (user.UserPassword != AesHelper.Encrypt(request.UserPassword))
{
throw new Exception("使用者名稱或密碼不正確");
}
//判斷redis裡是否已經存在當前患者
CurrentUser currentUser = await _userRepository.GetCurrentUserAsync(user.UserCode);
//如果已經存在,並且token沒有過期,則不用生成新的token,直接將redis裡的進行返回
if (currentUser != null && DateTime.Now < currentUser.ExpireTime)
{
return currentUser.Adapt<UserResponse>();
}
//如果不存在或者token過期,則生成新的token
SsoUser ssoUser = new SsoUser()
{
UserCode = user.UserCode,
UserName = user.UserName,
};
String token = await _jwtService.BuildToken(ssoUser);
await _userRepository.DelCurrentUserAsync(ssoUser.UserCode);
currentUser = new CurrentUser()
{
UserCode = ssoUser.UserCode,
UserName = user.UserName,
Token = token,
ExpireSeconds = _jwtConfig.ExpireSeconds,
ExpireTime = DateTime.Now.AddSeconds(_jwtConfig.ExpireSeconds)
};
await _userRepository.SetCurrentUserAsync(currentUser, _jwtConfig.ExpireSeconds);
userResponse = new UserResponse()
{
Token = token,
UserCode = user.UserCode,
UserName = user.UserName,
ExpireSeconds = _jwtConfig.ExpireSeconds,
};
return userResponse;
}
3.除錯
3.1 啟動應用程式,調傭介面
HttpGet方式
呼叫內建介面除錯方式
外部介面呼叫工具:
HttpPost方式
外部介面呼叫工具:
請求body內容
返回結果