layout: docs-default
使用X.509證書認(rèn)證客戶端
客戶端一般使用共享的密鑰來認(rèn)真(也就是客戶端secret),還有一個選擇就是用X.509
client 證書.
注冊客戶端
通過ISecretValidator接口可以完全控制 映射一個客戶端證書到對應(yīng)的客戶端密鑰
默認(rèn)實(shí)現(xiàn)是通過證書的指紋來映射合適的客戶端。
下面的代碼片段用來為客戶端注冊客戶憑據(jù):
var certClient = new Client
{
ClientName = "Client Credentials Flow Client with Client Certificate",
ClientId = "certclient",
ClientSecrets = new List<Secret>
{
new Secret
{
Value = "61B754C541BBCFC6A45A9E9EC5E47D8702B78C29",
Type = Constants.SecretTypes.X509CertificateThumbprint,
}
},
Flow = Flows.ClientCredentials,
AllowedScopes = new List<string>
{
"read",
"write"
},
}
配置主機(jī)
我們需要配置主機(jī)接受用戶端證書,對于IIS,我們需要配置一個Location節(jié)來讓令牌endpoint接受客戶端證書和SSL設(shè)置:
<location path="core/connect/token">
<system.webServer>
<security>
<access sslFlags="Ssl, SslNegotiateCert" />
</security>
</system.webServer>
</location>
備注 默認(rèn)情況下, SSL設(shè)置是被IIS鎖定的,需要在代理配置(delegation configuration)設(shè)置為可讀寫。
申請令牌
為了申請令牌,需要把客戶端證書提供給HTTP Client并把客戶端ID放在post body中。
下面是用IdentityModel OAuth2客戶端的例子:
async Task<TokenResponse> RequestTokenAsync()
{
var cert = new X509Certificate2("Client.pfx");
var handler = new WebRequestHandler();
handler.ClientCertificates.Add(cert);
var client = new OAuth2Client(
new Uri("https://identityserver.io/core/connect/token"),
"certclient",
handler);
return await client.RequestClientCredentialsAsync("read write");
}