我们正在尝试将Authlete api与Identity Server4一起使用来创建和授权访问令牌,但我似乎无法弄清楚如何使用.NET Core进行设置?
IdentityServer4是用C#编写的软件。如果要从 C# 调用 Authlete 的 Web API,可以使用 authlete-csharp 库(作为 Authlete.Authlete NuGet 包提供)。authlete-csharp 库的 API 参考可在此处获得。
以下是授权服务器和OpenID提供程序以及使用authlete-csharp库的资源服务器的示例实现。
- csharp-oauth-server - 用C#编写的授权服务器和OpenID提供程序实现,支持OAuth 2.0和OpenID Connect。
- csharp-resource-server - 用 C# 编写的资源服务器实现,包括 UserInfo Endpoint 的实现,其规范在 OpenID Connect Core 1.0 中定义。
以下文章介绍了 csharp-oauth-server 和 csharp-resource-server。
- "OAuth 2.0 和 OpenID Connect 在 C# 中的实现(Authlete)">
基本上,如果您使用Authlete,则不必使用IdentityServer4。但是,如果您有充分的理由使用 IdentityServer4,Authlete API 的某些部分可能会满足您的目的。
例如,如果您想将Authlete用作访问令牌的生成器,Authlete的/api/auth/token/create
API可能会起作用。
// An instance of IAuthleteApi interface.
IAuthleteApi api = ......
// Prepare a request to /api/auth/token/create API.
var request = new TokenCreateRequest
{
GrantType = ......,
ClientId = ......,
Subject = ......,
Scopes = ......,
......
};
// Call /api/auth/token/create API.
TokenCreateResponse response = await api.TokenCreate(request);
// If the API call successfully generated an access token.
if (response.Action = TokenCreateAction.OK)
{
// The newly issued access token.
string accessToken = response.AccessToken;
}
如果要将 Authlete 用作客户端应用程序元数据的存储,/api/client/*
API(和"客户端 ID 别名"功能)可能有效。
// An instance of IAuthleteApi interface.
IAuthleteApi api = ......
// Prepare a request to /api/client/create API.
var request = new Client
{
ClientName = ......,
Developer = ......,
ClientType = ......,
RedirectUris = ......,
......
};
// Call /api/client/create API. Client ID and client secret
// are automatically generated and assigned by Authlete.
Client client = await api.CreateClient(request);
// You can update client information by calling
// /api/client/update/{clientId} API.
client = await api.UpdateClient(client);
Authlete管理多个服务。这里的服务是一个对应于一个授权服务器和OpenID提供者的实例。甚至服务本身也可以通过/api/service/*
API 进行管理。
// An instance of IAuthleteApi interface.
IAuthleteApi api = ......
// Prepare a request to /api/service/create API.
var request = new Service
{
ServiceName = ......,
Issuer = ......,
SupportedScopes = ......,
......
};
// Call /api/service/create API. A pair of API key and
// API secret to manage the service is automatically
// generated and assigned by Authlete.
Service service = await api.CreateService(request);
// You can update service information by calling
// /api/service/update/{serviceApiKey} API.
service = await api.UpdateService(service);
虽然服务和客户端应用程序可以通过Authlete API进行管理,但我建议您使用Web控制台(服务所有者控制台和开发人员控制台)来管理它们。
包括 IdentityServer4 在内的许多库都需要编程来配置授权服务器本身、注册客户端应用程序和设置数据库。我不想做这样的事情,最终决定开发一个SaaS(= API +永久存储),而不是一个库,以便将开发人员从负担中解放出来。这就是Authlete出生的原因。(我是Authlete,Inc.的联合创始人。