平安校园
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.

AuthComponent.cs 1.3 KiB

3 months ago
123456789101112131415161718192021222324252627282930313233343536373839
  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. }