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

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