OAuthSetup.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. using Microsoft.AspNetCore.Authentication;
  7. using Microsoft.AspNetCore.Authentication.Cookies;
  8. using Microsoft.AspNetCore.Authentication.JwtBearer;
  9. using Microsoft.AspNetCore.Builder;
  10. namespace Admin.NET.Core;
  11. public static class OAuthSetup
  12. {
  13. /// <summary>
  14. /// 三方授权登录OAuth注册
  15. /// </summary>
  16. /// <param name="services"></param>
  17. public static void AddOAuth(this IServiceCollection services)
  18. {
  19. var authOpt = App.GetConfig<OAuthOptions>("OAuth", true);
  20. services.AddAuthentication(options =>
  21. {
  22. options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  23. options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  24. })
  25. .AddCookie(options =>
  26. {
  27. options.Cookie.SameSite = SameSiteMode.Lax;
  28. })
  29. .AddWeixin(options =>
  30. {
  31. options.ClientId = authOpt.Weixin?.ClientId;
  32. options.ClientSecret = authOpt.Weixin?.ClientSecret;
  33. })
  34. .AddGitee(options =>
  35. {
  36. options.ClientId = authOpt.Gitee?.ClientId;
  37. options.ClientSecret = authOpt.Gitee?.ClientSecret;
  38. options.ClaimActions.MapJsonKey(OAuthClaim.GiteeAvatarUrl, "avatar_url");
  39. });
  40. }
  41. public static void UseOAuth(this IApplicationBuilder app)
  42. {
  43. app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax });
  44. }
  45. }