为ADFS和.Net内核设置OAuth2 JWT令牌

为ADFS和.Net内核设置OAuth2 JWT令牌,jwt,.net-core,oauth2,adfs3.0,Jwt,.net Core,Oauth2,Adfs3.0,有人能解释一下.Net Core中OAuth2 JWT令牌的生成和验证吗?首先,您需要使用客户端id和重定向URL设置ADFS,然后从ADFS服务器获取JWT令牌。看到这个帖子了吗 之后,如果您使用.NETCore和JWT承载令牌,则需要 使用以下powershell命令导出ADFS签名证书: $certRefs=Get-AdfsCertificate -CertificateType Token-Signing $certBytes=$certRefs[0].Certificate.Expor

有人能解释一下.Net Core中OAuth2 JWT令牌的生成和验证吗?

首先,您需要使用客户端id和重定向URL设置ADFS,然后从ADFS服务器获取JWT令牌。看到这个帖子了吗

之后,如果您使用.NETCore和JWT承载令牌,则需要 使用以下powershell命令导出ADFS签名证书:

$certRefs=Get-AdfsCertificate -CertificateType Token-Signing
$certBytes=$certRefs[0].Certificate.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert)
[System.IO.File]::WriteAllBytes("c:\foo.cer", $certBytes)
然后,在.NETCore应用程序启动时,您需要使用packageMicrosoft.AspNetCore.Authentication.JwtBearer并查看本文

启动类中的代码:

var signingKey = new X509SecurityKey(
    new System.Security.Cryptography.X509Certificates.X509Certificate2(
        "YOUR-PATH/foo.cer"));

var tokenValidationParameters = new TokenValidationParameters
{
    // The signing key must match!
    ValidateIssuerSigningKey = true,
    IssuerSigningKey = signingKey,

    // Validate the JWT Issuer (iss) claim
    ValidateIssuer = true,
    ValidIssuer = "http://YOUR-ADFS/adfs/services/trust",

    // Validate the JWT Audience (aud) claim
    ValidateAudience = true,
    ValidAudience = "https://YOUR-AUDIENCE/",

    // Validate the token expiry
    ValidateLifetime = true,

    // If you want to allow a certain amount of clock drift, set that here:
    ClockSkew = TimeSpan.Zero
};

app.UseJwtBearerAuthentication(new JwtBearerOptions
{
    AutomaticAuthenticate = true,
    AutomaticChallenge = true,
    TokenValidationParameters = tokenValidationParameters
});

首先,您需要使用客户端id和重定向URL设置ADFS,然后从ADFS服务器获取JWT令牌。看到这个帖子了吗

之后,如果您使用.NETCore和JWT承载令牌,则需要 使用以下powershell命令导出ADFS签名证书:

$certRefs=Get-AdfsCertificate -CertificateType Token-Signing
$certBytes=$certRefs[0].Certificate.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert)
[System.IO.File]::WriteAllBytes("c:\foo.cer", $certBytes)
然后,在.NETCore应用程序启动时,您需要使用packageMicrosoft.AspNetCore.Authentication.JwtBearer并查看本文

启动类中的代码:

var signingKey = new X509SecurityKey(
    new System.Security.Cryptography.X509Certificates.X509Certificate2(
        "YOUR-PATH/foo.cer"));

var tokenValidationParameters = new TokenValidationParameters
{
    // The signing key must match!
    ValidateIssuerSigningKey = true,
    IssuerSigningKey = signingKey,

    // Validate the JWT Issuer (iss) claim
    ValidateIssuer = true,
    ValidIssuer = "http://YOUR-ADFS/adfs/services/trust",

    // Validate the JWT Audience (aud) claim
    ValidateAudience = true,
    ValidAudience = "https://YOUR-AUDIENCE/",

    // Validate the token expiry
    ValidateLifetime = true,

    // If you want to allow a certain amount of clock drift, set that here:
    ClockSkew = TimeSpan.Zero
};

app.UseJwtBearerAuthentication(new JwtBearerOptions
{
    AutomaticAuthenticate = true,
    AutomaticChallenge = true,
    TokenValidationParameters = tokenValidationParameters
});

请检查下面的链接帮助您,但程序相同

OAuth2授权提供程序1.0.0nuget软件包具有验证给定jwt令牌的方法(ValidateToken),但它具有证书依赖性(提供程序)

在本地计算机受信任的根目录下安装证书,该根目录是您的adfs证书


Nuget软件包将根据SubjectKeyIdentifier识别已安装的证书。

请检查下面的链接帮助您,但过程相同

OAuth2授权提供程序1.0.0nuget软件包具有验证给定jwt令牌的方法(ValidateToken),但它具有证书依赖性(提供程序)

在本地计算机受信任的根目录下安装证书,该根目录是您的adfs证书

Nuget软件包将基于SubjectKeyIdentifier标识已安装的证书