.net core .NET Core Identity Server动态客户端应用程序注册和身份验证
我已经使用.NET Core和IdentityServer 4创建了一个Identity Server,我有一组API,对这些API的所有调用都必须经过身份验证,但这些API可能被第三方应用程序使用,因此客户端可以是动态的 到目前为止,我发现的示例是静态地将启动时的客户机设置为.net core .NET Core Identity Server动态客户端应用程序注册和身份验证,.net-core,asp.net-core-webapi,identityserver4,.net Core,Asp.net Core Webapi,Identityserver4,我已经使用.NET Core和IdentityServer 4创建了一个Identity Server,我有一组API,对这些API的所有调用都必须经过身份验证,但这些API可能被第三方应用程序使用,因此客户端可以是动态的 到目前为止,我发现的示例是静态地将启动时的客户机设置为 public void ConfigureServices(IServiceCollection services) { services.AddIdentityServer() .AddDeveloperS
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients());
}
public class Config
{
public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("resourceApi1", "API Application1")
new ApiResource("resourceApi2", "API Application2")
};
}
public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client
{
ClientId = "clientApp1",
// no interactive user, use the clientid/secret for authentication
AllowedGrantTypes = GrantTypes.ClientCredentials,
// secret for authentication
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "resourceApi1" }
}
};
}
}
public void配置服务(IServiceCollection服务)
{
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients());
}
公共类配置
{
公共静态IEnumerable GetApiResources()
{
返回新列表
{
新的ApiResource(“resourceApi1”、“API应用程序1”)
新的ApiResource(“resourceApi2”、“API应用程序2”)
};
}
公共静态IEnumerable GetClients()
{
返回新列表
{
新客户
{
ClientId=“clientApp1”,
//无交互用户,请使用clientid/secret进行身份验证
AllowedGrantTypes=GrantTypes.ClientCredentials,
//身份验证的秘密
客户秘密=
{
新密码(“Secret.Sha256())
},
AllowedScopes={“resourceApi1”}
}
};
}
}
在IdentityServer实现中是否有一种方法可以注册客户端应用程序并进行动态设置
例如,如果我有API
1.资源API1
2.资源API2
每个第三方API都应该能够注册,我们应该能够为每个API生成ClientID和Client secret,它们可以访问哪些资源,Identity Server可以验证ClientID和Client secret?可以选择使用客户端存储。默认情况下,identity server使用内存存储查找客户端:
services.AddIdentityServer()
.AddInMemoryClients(Clients)
您可以更改此设置并注册自己的客户端存储
services.AddIdentityServer()
.AddClientStore<MyClientStore>()
services.AddIdentityServer()
.AddClientStore()
并实施该服务
class MyClientStore : IClientStore
{
public Task<Client> FindClientByIdAsync(string clientId)
{
}
}
classmyclientstore:IClientStore
{
公共任务findclientbydasync(字符串clientId)
{
}
}
这将解决客户端的动态查找问题。要注册客户端及其管理,您需要实现自己的基础架构。首先配置您的基础架构,然后需要添加一些API来添加客户端和API资源 IdentityServer使用EntityFramework有自己的存储实现 您可以为此添加新的Api
[Route("api/[controller]")]
[ApiController]
[AllowAnonymous]
public class DynamicController : ControllerBase
{
private readonly ConfigurationDbContext context;
public DynamicController(ConfigurationDbContext context)
{
this.context = context;
}
[HttpPost]
public async Task<ActionResult> AddApiResource(ApiResource apiResource)
{
context.ApiResources.Add(apiResource);
await context.SaveChangesAsync();
return Ok();
}
[HttpPost]
public async Task<ActionResult> AddClient(Client client)
{
context.Clients.Add(client);
await context.SaveChangesAsync();
return Ok();
}
}
[路由(“api/[控制器]”)]
[ApiController]
[异名]
公共类动态控制器:ControllerBase
{
私有只读ConfigurationDbContext上下文;
公共DynamicController(ConfigurationDbContext上下文)
{
this.context=上下文;
}
[HttpPost]
公共异步任务AddAPIROURCE(APIROURCE APIROURCE)
{
context.ApiResources.Add(ApiResources);
wait context.saveChangesSync();
返回Ok();
}
[HttpPost]
公共异步任务AddClient(客户端)
{
context.Clients.Add(客户端);
wait context.saveChangesSync();
返回Ok();
}
}
谢谢你,我不知道这一点,是否可以更改其他虚拟资源?对于客户端应用程序,我们需要将访问权限限制为客户端存储区中的少数控制器(可能按范围),这可能吗?对于API资源,有.AddResourceStore()
方法。Client
类具有AllowedScopes
属性,它可以是客户端存储区的一部分。