我正在尝试使用Identity Server 4来保护我的API。现在我已经看完了所有的文件
http://docs.identityserver.io/en/release/quickstarts/1_client_credentials.html
我已经建立了一些成功的演示。然而,有一件事我不明白。
例如,首先,我们需要在IS4上定义一个如下所示的客户端:
new Client
{
ClientId = "client",
// no interactive user, use the clientid/secret for authentication
AllowedGrantTypes = GrantTypes.ClientCredentials,
// secret for authentication
ClientSecrets =
{
new Secret("secret".Sha256())
},
// scopes that client has access to
AllowedScopes = { "api1" }
}
然后在API上,我们通过添加
IdentityServer4.AccessTokenValidation
打包并将配置添加到启动.cs
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters();
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ApiName = "api1";
});
最后我们加上
app.UseAuthentication();
现在,这些都可以在运行时运行,但是,我不明白的部分是API中定义的秘密在哪里。如您所见,客户端显然需要一个秘密,但是我没有在API的任何地方定义这个秘密。我也没有在IS4的任何地方定义任何关于我的API的东西,比如说你在保护API不受这个URI或者类似的东西的影响。