第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方式


调用内置接口调试方式

外部接口调用工具Apifox:

HttpPost方式

外部接口调用工具Apifox:
请求body内容

返回结果

posted @ 2024-10-20 19:07  似梦亦非梦  阅读(183)  评论(0编辑  收藏  举报