1. 首先说一个完整的,.net core、net framework、.net 3.1/6.0/8.0都支持的,我正在用的,最熟悉的,下面两个不同平台上的实现方式也可以参考参考
1.1 设置身份认证服务器(IdentityServer)
首先,你需要创建一个身份认证服务器,该服务器将负责用户身份验证、令牌发放和注销。
安装IdentityServer4
你可以通过NuGet包管理器安装IdentityServer4:
dotnet add package Duende.IdentityServer
配置IdentityServer
在身份认证服务器项目中配置IdentityServer,定义API资源和身份资源:
public class Config
{
public static IEnumerable<IdentityResource> IdentityResources =>
new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
};
public static IEnumerable<Client> Clients =>
new List<Client>
{
// 定义客户端应用程序
new Client
{
ClientId = "client_id",
ClientSecrets = {
new Secret("client_secret".Sha256()) },
AllowedGrantTypes = GrantTypes.Code,
RedirectUris = {
"https://client-app/callback" },
PostLogoutRedirectUris = {
"https://client-app/signout-callback" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile
}
}
};
}
public void ConfigureServices(IServiceCollection services)
{
var builder = services.AddIdentityServer(options =>
{
// 配置IdentityServer选项
})
.AddInMemoryIdentityResources