平安校园
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

40 linhas
1.3 KiB

  1. using Microsoft.AspNetCore.Authentication.JwtBearer;
  2. namespace SafeCampus.Web.Core;
  3. /// <summary>
  4. /// 认证相关组件
  5. /// </summary>
  6. public sealed class AuthComponent : IServiceComponent
  7. {
  8. public void Load(IServiceCollection services, ComponentContext componentContext)
  9. {
  10. // JWT配置
  11. services.AddJwt<JwtHandler>(enableGlobalAuthorize: true, jwtBearerConfigure: options =>
  12. {
  13. //signalr jwt配置
  14. options.Events = new JwtBearerEvents
  15. {
  16. OnMessageReceived = context =>
  17. {
  18. var accessToken = context.Request.Query["access_token"];
  19. // If the request is for our hub...
  20. var path = context.HttpContext.Request.Path;
  21. if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/hubs"))
  22. {
  23. // Read the token out of the query string
  24. context.Token = accessToken;
  25. }
  26. return Task.CompletedTask;
  27. }
  28. };
  29. });
  30. // 允许跨域
  31. services.AddCorsAccessor();
  32. //注册自定义授权筛选器
  33. services.AddMvcFilter<MyAuthorizationFilter>();
  34. }
  35. }