C# Identityserver4与azurewebsites.net域-如何获取ssl指纹

C# Identityserver4与azurewebsites.net域-如何获取ssl指纹,c#,azure-web-app-service,identityserver4,C#,Azure Web App Service,Identityserver4,我想在azurewebsites.net https上托管identity server 4,但我不确定如何找到我的someapp.azurewebsites.net子域的ssl证书,以便将其用作指纹?甚至可以在azurewebsites.net子域上获取您的Web应用程序上运行的ssl证书吗?使用自签名证书与IdentityServer 4进行令牌签名的一种方法是将证书与应用程序一起存储在“wwwroot”文件夹下 您可以使用OpenSSL生成自签名证书 openssl req -x509 -

我想在azurewebsites.net https上托管identity server 4,但我不确定如何找到我的someapp.azurewebsites.net子域的ssl证书,以便将其用作指纹?甚至可以在azurewebsites.net子域上获取您的Web应用程序上运行的ssl证书吗?

使用自签名证书与IdentityServer 4进行令牌签名的一种方法是将证书与应用程序一起存储在“wwwroot”文件夹下

您可以使用OpenSSL生成自签名证书

openssl req -x509 -newkey rsa:4096 -sha256 -nodes -keyout example.key -out example.crt -subj "/CN=example.com" -days 3650
然后将证书加载到“Startup.cs”类/文件中

public void ConfigureServices(IServiceCollection services)
{
        .....other code .....

        var fileName = Path.Combine(env.WebRootPath, "YOUR_FileName" );            

        if (!File.Exists(fileName))
        {
            throw new FileNotFoundException("Signing Certificate is missing!");
        }

        var cert = new X509Certificate2(fileName, "Your_PassPhrase" );

        services.AddIdentityServer().AddSigningCredential(cert)

        ...other code.....
}

使用自签名证书。由于Identity Server仅在内部使用证书,因此证书是否由受信任的CA签名并不重要,您不会将其呈现给其他人。我将签名证书作为序列化机密存储在Azure密钥库中。密钥库非常便宜,但您必须缓存结果,它并不适用于高流量

请记住在网站、功能应用程序或任何正在检索值的其他程序上启用托管服务标识,并将其添加到具有机密读取权限的密钥Vault SAS列表中

Powershell:创建签名证书 Powershell:将证书上载到Azure密钥库 线程安全密钥库缓存
公共类KeyVaultCache
{
private KeyVaultClient _KeyVaultClient=null;
公钥保险库客户端密钥保险库客户端
{
得到
{
如果(_KeyVaultClient为null)
{
var provider=新AzureServiceTokenProvider();
_KeyVaultClient=newkeyvaultclient(newkeyvaultclient.AuthenticationCallback(provider.KeyVaultTokenCallback));
}
返回KeyVaultClient;
}
}
私有ConcurrentDictionary SecretsCache=新的ConcurrentDictionary(StringComparer.OrdinalIgnoreCase);
公共异步任务GetCachedSecret(字符串secretName)
{
如果(!SecretsCache.ContainsKey(secretName))
{
var secretBundle=await KeyVaultClient.GetSecretAsync($“{AzureUris.KeyVaultSecrets}{secretName}”).ConfigureAwait(false);
SecretsCache.TryAdd(secretName,secretBundle.Value);
}
返回SecretsCache.ContainsKey(secretName)?SecretsCache[secretName]:string.Empty;
}
}
检索反序列化证书
public async Task TokenValidationCertificate()=>PfxStringToCert(wait cache.GetCachedSecret(“x509令牌验证”);
公共异步任务令牌签名证书()=>PfxStringToCert(wait cache.GetCachedSecret(“x509令牌签名”);
专用X509Certificate2 PfxStringToCert(字符串pfx)
{
var bytes=Convert.FromBase64String(pfx);
var coll=新的X509Certificate2Collection();
coll.Import(字节,空,X509keystrageFlags.Exportable);
返回coll[0];
}

您想使用证书进行签名吗?是的,用于identity server,但我没有找到获取itI的方法,但是如何获取azurewebsites.net证书?因此,您想在Azure Web应用程序上使用SSL证书来验证您的应用程序/域名?我想使用来自Azure的默认证书进行identity server认证。为azurewebsites.net域制作的证书。我认为您不能,因为我们没有访问该证书的权限。您最好创建一个自签名证书,并按照我的建议使用它。该域与令牌签名和验证证书无关。该证书仅在IS4内部使用。SSL证书可以重复使用(但不能按您要求的方式访问),但从技术上讲,签名证书的设置会有所不同。我所有的证书和设置(如连接字符串)都在密钥库中,每月只需几美分。。。
[CmdletBinding()]
param(
    [Parameter(Mandatory=$true)][string]$password = "",
    [Parameter(Mandatory=$true)][string]$rootDomain = ""
)

$cwd = Convert-Path .
$sCerFile = "$cwd\token_signing.cer"
$sPfxFile = "$cwd\token_signing.pfx"
$vCerFile = "$cwd\token_validation.cer"
$vPfxFile = "$cwd\token_validation.pfx"

# abort if files exist
if((Test-Path($sPfxFile)) -or (Test-Path($sCerFile)) -or (Test-Path($vPfxFile)) -or (Test-Path($vCerFile)))
{
    Write-Warning "Failed, token_signing or token_validation files already exist in current directory."
    Exit
}

function Get-NewCert ([string]$name)
{
    New-SelfSignedCertificate `
        -Subject $rootDomain `
        -DnsName $rootDomain `
        -FriendlyName $name `
        -NotBefore (Get-Date) `
        -NotAfter (Get-Date).AddYears(10) `
        -CertStoreLocation "cert:CurrentUser\My" `
        -KeyAlgorithm RSA `
        -KeyLength 4096 `
        -HashAlgorithm SHA256 `
        -KeyUsage DigitalSignature, KeyEncipherment, DataEncipherment `
        -Type Custom,DocumentEncryptionCert `
        -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.1")
}

$securePass = ConvertTo-SecureString -String $password -Force -AsPlainText

# token signing certificate
$cert = Get-NewCert("IdentityServer Token Signing Credentials")
$store = 'Cert:\CurrentUser\My\' + ($cert.ThumbPrint)  
Export-PfxCertificate -Cert $store -FilePath $sPfxFile -Password $securePass
Export-Certificate -Cert $store -FilePath $sCerFile
Write-Host "Token-signing thumbprint: " $cert.Thumbprint

# token validation certificate
$cert =  Get-NewCert("IdentityServer Token Validation Credentials")
$store = 'Cert:\CurrentUser\My\' + ($cert.ThumbPrint)  
Export-PfxCertificate -Cert $store -FilePath $vPfxFile -Password $securePass
Export-Certificate -Cert $store -FilePath $vCerFile
Write-Host "Token-validation thumbprint: " $cert.Thumbprint
[CmdletBinding()]
param(
    [Parameter(Mandatory=$true)][string]$password = "",
    [Parameter(Mandatory=$true)][string]$pfxFilename = "",
    [Parameter(Mandatory=$true)][string]$keyVaultName = "",
    [Parameter(Mandatory=$true)][string]$secretName = ""
)

$cwd = Convert-Path .
$pfxFile = "$cwd\$pfxFilename.pfx"

# abort when file not found
if(!(Test-Path($pfxFile)))
{
    Write-Warning "Failed, $pfxFilename.pfx not found $cwd"
    Exit
}

# force Azure login, if needed
function CheckLogin
{
    $needLogin = $true
    Try 
    {
        $content = Get-AzureRmContext
        if ($content) 
        {
            $needLogin = ([string]::IsNullOrEmpty($content.Account))
        } 
    } 
    Catch 
    {
        if ($_ -like "*Login-AzureRmAccount to login*") 
        {
            $needLogin = $true
        } 
        else 
        {
            throw
        }
    }

    if ($needLogin)
    {
        Login-AzureRmAccount
    }
}

CheckLogin

# load the PFX
$flag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
$coll = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection 
$coll.Import($pfxFile, $password, $flag)

# export to byte array
$type = [System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12
$bytes = $coll.Export($type)

# base64 encode
$base64 = [System.Convert]::ToBase64String($bytes)
$value = ConvertTo-SecureString -String $base64 -AsPlainText –Force

# send it to Azure KeyVault
$type = 'application/x-pkcs12'
Set-AzureKeyVaultSecret -VaultName $keyVaultName -Name $secretName -SecretValue $value -ContentType $type
public class KeyVaultCache
{
    private KeyVaultClient _KeyVaultClient = null;
    public KeyVaultClient KeyVaultClient
    {
        get
        {
            if(_KeyVaultClient is null)
            {
                var provider = new AzureServiceTokenProvider();
                _KeyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(provider.KeyVaultTokenCallback));
            }
            return _KeyVaultClient;
        }
    }

    private ConcurrentDictionary<string, string> SecretsCache = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
    public async Task<string> GetCachedSecret(string secretName)
    {
        if(!SecretsCache.ContainsKey(secretName))
        {
            var secretBundle = await KeyVaultClient.GetSecretAsync($"{AzureUris.KeyVaultSecrets}{secretName}").ConfigureAwait(false);
            SecretsCache.TryAdd(secretName, secretBundle.Value);
        }
        return SecretsCache.ContainsKey(secretName) ? SecretsCache[secretName] : string.Empty;
    }
}
public async Task<X509Certificate2> TokenValidationCertificate() => PfxStringToCert(await cache.GetCachedSecret("x509-token-validation"));
public async Task<X509Certificate2> TokenSigningCertificate() => PfxStringToCert(await cache.GetCachedSecret("x509-token-signing"));

private X509Certificate2 PfxStringToCert(string pfx)
{
    var bytes = Convert.FromBase64String(pfx);
    var coll = new X509Certificate2Collection();
    coll.Import(bytes, null, X509KeyStorageFlags.Exportable);
    return coll[0];
}