第38篇 net8接口调试方式
.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方式
https://img2024.cnblogs.com/blog/2212230/202410/2212230-20241020183821778-2007667467.png
调用内置接口调试方式
https://img2024.cnblogs.com/blog/2212230/202410/2212230-20241020183916564-2000763609.png
外部接口调用工具:
https://img2024.cnblogs.com/blog/2212230/202410/2212230-20241020184000290-870449821.png
HttpPost方式
外部接口调用工具:
哀求body内容
https://img2024.cnblogs.com/blog/2212230/202410/2212230-20241020184416938-582324905.png
返回结果
https://img2024.cnblogs.com/blog/2212230/202410/2212230-20241020184352887-1755146187.png
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]