平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

22 lines
803 B

  1. using Furion.DataEncryption;
  2. using SimpleTool;
  3. namespace SafeCampus.Plugin.SignalR
  4. {
  5. /// <summary>
  6. /// 用户ID提供器
  7. /// </summary>
  8. public class UserIdProvider : IUserIdProvider
  9. {
  10. public string GetUserId(HubConnectionContext connection)
  11. {
  12. var token = connection.GetHttpContext().Request.Query["access_token"];//获取token
  13. var claims = JWTEncryption.ReadJwtToken(token)?.Claims;//解析token
  14. var userId = claims.FirstOrDefault(u => u.Type == ClaimConst.UserId)?.Value;//获取用户ID
  15. if (!string.IsNullOrEmpty(userId))//如果不为空
  16. return $"{userId}_{RandomHelper.CreateLetterAndNumber(5)}";//返回用户ID
  17. else
  18. return connection.ConnectionId;
  19. }
  20. }
  21. }