DigitalFactory/Admin.NET/Admin.NET.Core/OAuth/OAuthSetup.cs

48 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 大名科技(天津)有限公司版权所有 电话18020030720 QQ515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
namespace Admin.NET.Core;
public static class OAuthSetup
{
/// <summary>
/// 三方授权登录OAuth注册
/// </summary>
/// <param name="services"></param>
public static void AddOAuth(this IServiceCollection services)
{
var authOpt = App.GetConfig<OAuthOptions>("OAuth", true);
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.Cookie.SameSite = SameSiteMode.Lax;
})
.AddWeixin(options =>
{
options.ClientId = authOpt.Weixin?.ClientId;
options.ClientSecret = authOpt.Weixin?.ClientSecret;
})
.AddGitee(options =>
{
options.ClientId = authOpt.Gitee?.ClientId;
options.ClientSecret = authOpt.Gitee?.ClientSecret;
options.ClaimActions.MapJsonKey(OAuthClaim.GiteeAvatarUrl, "avatar_url");
});
}
public static void UseOAuth(this IApplicationBuilder app)
{
app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax });
}
}