我将我的身份验证服务器的客户端存储在一个appsettings.json文件中:
"ClientSettings": [
{
"ClientId": "TestClient1",
"RedirectUris": [ "http://localhost:5002/signin-oidc" ],
"PostLogoutRedirectUris": [ "http://localhost:5002/signout-callback-oidc" ],
"AllowedGrantTypes": [ "hybrid" ],
"AllowedScopes": [ "openid","profile","email" ],
"RequireConsent": "false",
"Enabled": "true",
"ClientSecrets": [
{
"Description": "This is the client sceret description.",
"Value": "password123"
}
]
}
],
在我的ConfigureServices
方法中,我设置了依赖注入。
services.Configure<List<Client>>(config.GetSection("ClientSettings"));
我的IClientStore
通过依赖注入获取客户端列表。当然,我需要对客户端密钥进行哈希处理。为了保持这个问题的简单性,我编写了以下代码,假设我只有一个客户端和一个客户端密钥。
public ClientConfigFileStore(IOptions<List<Client>> options)
{
var jsonClient = options.Value[0];
Client client = new Client()
{
ClientId = jsonClient.ClientId,
RedirectUris = jsonClient.RedirectUris,
PostLogoutRedirectUris = jsonClient.PostLogoutRedirectUris,
AllowedGrantTypes = jsonClient.AllowedGrantTypes,
AllowedScopes = jsonClient.AllowedScopes,
RequireConsent = jsonClient.RequireConsent,
Enabled = jsonClient.Enabled,
};//Works
Client client = jsonClient;//Does not work
client.ClientSecrets = new List<Secret>()
{
new Secret(jsonClient.ClientSecrets.First().Value.Sha256())
};
clients = new List<Client>() { client };
}
我需要创建一个新的Client
实例,但是使用依赖注入创建的实例无法工作。在用户在身份服务器输入凭据后,重定向回客户端失败,出现错误Client secret validation failed for the client: TestClient1
为什么需要创建一个新实例?是否有更优雅的方式从appsettings.json
文件中加载客户端信息?