在IdentityServer中,客户端机密和作用域机密之间有什么区别



有人能解释一下两者之间的区别吗?我了解客户的秘密,但范围秘密仍然不清楚。。。以及为什么范围秘密甚至需要存在?

虽然我发现文档在某些方面很有帮助,但我发现它对解释两者之间的区别没有帮助。

多亏了Scott Brady,我才得以回答我的问题。以下是我的发现。。。

客户机密

正如Scott Brady所说,当您的客户端应用程序调用令牌端点时,将使用客户端机密。您的客户端应用程序必须具有有效的客户端Id和客户端机密才能调用令牌端点。

范围机密

但是,如果您的资源服务器需要调用IdentityServer,该怎么办?当资源服务器调用自省端点时,就会发生这种情况。当您的应用程序使用引用令牌或使用JWT的服务器端验证时,就会发生这种情况。因此,假设经过身份验证的客户端调用资源服务器上受保护的端点。然后,资源服务器必须使用IdentityServer验证JWT承载令牌。但是,从资源服务器发送到IdentityServer的请求必须是受保护的调用。也就是说,资源服务器必须向IdentityServer提供JWT承载令牌,才能使用内省端点。资源服务器无法使用它试图验证的令牌,因为它属于客户端(受众不是资源服务器,而是客户端应用程序)。此外,资源服务器甚至不确定承载令牌是否有效。资源服务器使用有问题的承载令牌来验证验证所述承载令牌的请求是没有意义的。所以现在有一个问题。。。资源服务器如何向IdentityServer发送经过身份验证的请求?

这就是作用域机密的作用域。IdentityServer解决此问题的方法是允许您创建一个包含作用域秘密的作用域。此作用域已添加到资源服务器身份验证选项中。例如:

app.UseIdentityServerBearerTokenAuthentication(
new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "http://localhost:5000",
ClientId = "api", //The Scope name
ClientSecret = "api-secret", //This is the non hashed/encrypted Scope Secret
RequiredScopes = new[] { "api" } //Must add the Scope name here
});

通过要求此作用域,我们可以确保任何经过身份验证的客户端应用程序都将具有此作用域。然后在IdentityServer中,您可以添加这样的范围:

new Scope
{
Name = "api",
DisplayName = "Scope DisplayName",
Description = "This will grant you access to the API",
//The secret here must be Sha256-ed in order for the /introspection end point to work.
//If the API's IdentityServerBearerTokenAuthenticationOptions field is set as so ValidationMode = ValidationMode.ValidationEndpoint,
//then the API will call the /introspection end point to validate the token on each request (instead of ValidationModel.ValidationLocal.
//The ClientSecret must be the NON Sha256-ed string (for example Api = "api-secret" then scope secret must = "api-secret".Sha256())
//for the token to be validated. There must be a Scope that has the same name as the ClientId field in IdentityServerBearerTokenAuthenticationOptions.
//This is an API authenticates with IdentityServer
ScopeSecrets = new List<Secret>
{
new Secret("api-secret".Sha256())
},
Type = ScopeType.Resource
}

因此,当资源服务器调用内省端点时,它将简单地使用作用域机密作为客户端机密,使用作用域名称作为客户端Id。

客户端机密用于授权访问令牌端点。该端点使用客户端id和客户端机密,并允许您请求访问令牌。

作用域机密用于授权对内省端点的访问。此端点使用作用域id和作用域机密,因为只有包含在访问令牌中的作用域才允许对其进行内省

最新更新