使用X.509證書認(rèn)證客戶端


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");
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容