Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将JwtBearer与AddMicrosoftIdentityWebAppAuthentication一起添加_C#_Asp.net Core_Asp.net Core 3.1_Microsoft Identity Platform - Fatal编程技术网

C# 如何将JwtBearer与AddMicrosoftIdentityWebAppAuthentication一起添加

C# 如何将JwtBearer与AddMicrosoftIdentityWebAppAuthentication一起添加,c#,asp.net-core,asp.net-core-3.1,microsoft-identity-platform,C#,Asp.net Core,Asp.net Core 3.1,Microsoft Identity Platform,我不确定自己是否完全理解了Microsoft.Identity.Web的更改,但我正在阅读一篇文章(其中描述了如何在启动时进行更改) services.AddAuthentication(AzureADDefaults.AuthenticationScheme) .AddAzureAD(options => Configuration.Bind("AzureAd", options)); 到 虽然这看起来很好,也很简单,但我还有一些工作要做,因为我现有的代

我不确定自己是否完全理解了Microsoft.Identity.Web的更改,但我正在阅读一篇文章(其中描述了如何在启动时进行更改)

 services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
     .AddAzureAD(options => Configuration.Bind("AzureAd", options));

虽然这看起来很好,也很简单,但我还有一些工作要做,因为我现有的代码中有以下代码片段

services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
   .AddAzureAD(options => this.configuration.Bind("AzureAd", options))
   .AddJwtBearer(options =>
   {
       //this code used to validate signing keys
       string stsDiscoveryEndpoint = "https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration";
       IConfigurationManager<OpenIdConnectConfiguration> configurationManager = new ConfigurationManager<OpenIdConnectConfiguration>(stsDiscoveryEndpoint, new OpenIdConnectConfigurationRetriever());
       OpenIdConnectConfiguration openIdConfig = configurationManager.GetConfigurationAsync(CancellationToken.None).GetAwaiter().GetResult();
       var tenantId = this.configuration["TenantId"];
       var validIssuer = $"https://sts.windows.net/{tenantId}/";

       options.TokenValidationParameters = new TokenValidationParameters()
       {
           ValidIssuer = validIssuer,
           ValidAudience = this.configuration["ClientId"],
           IssuerSigningKeys = openIdConfig.SigningKeys,
       };
  });
services.AddAuthentication(AzureAddFaults.AuthenticationScheme)
.AddAzureAD(options=>this.configuration.Bind(“AzureAd”,options))
.AddJwtBearer(选项=>
{
//此代码用于验证签名密钥
字符串stsDiscoveryEndpoint=”https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration";
IConfigurationManager configurationManager=new configurationManager(stsDiscoveryEndpoint,new OpenIdConnectConfigurationRetriever());
OpenIdConnectConfiguration openIdConfig=configurationManager.GetConfigurationAsync(CancellationToken.None).GetAwaiter().GetResult();
var tenantId=this.configuration[“tenantId”];
var validIssuer=$”https://sts.windows.net/{tenantId}/“;
options.TokenValidationParameters=新的TokenValidationParameters()
{
ValidIssuer=ValidIssuer,
Validudience=this.configuration[“ClientId”],
IssuerSigningKeys=openIdConfig.SigningKeys,
};
});
为了给你一点背景知识,我们有两种不同的应用程序

  • 用户登录并执行一些操作(用户将在此处获得Microsoft登录对话框,以便使用其凭据登录)
  • Microsoft Azure使用某些令牌调用我们的端点,我们需要验证该令牌
  • 您在上面看到的JWTValidation部分是针对第二项的,一旦我们收到一个令牌,我们将在不登录和UI工作流的情况下验证该令牌

    问题:
    上面的代码工作正常。这里唯一的问题是如果我们想使用
    Microsoft.Identity
    我们应该如何使用第二项(JWT),因为
    services.AddAuthentication().AddAzureAD
    返回
    IAuthenticationBuilder
    ,我们进一步使用它来添加
    AddJwtBearer
    ,而
    服务。AddMicrosoftIdentityWebAppAuthentication
    不返回
    IAuthenticationBuilder

    AddMicrosoftIdentityWebAppAuthentication
    实际上是执行以下操作:

    services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApp(…)
    
    因此,它将默认方案配置为OIDC方案,并运行
    AddMicrosoftIdentityWebApp
    来配置最终执行的操作

    现在,实际上可以在服务集合上多次调用。您只需小心不要错误地重新配置。无参数函数不能做到这一点,因此访问
    IAAuthenticationBuilder
    是进一步配置身份验证的好方法

    这意味着您可以像这样更改代码:

    // configure Microsoft Identity Web first
    // this also sets the default authentication to OIDC
    services.AddMicrosoftIdentityWebAppAuthentication(Configuration);
    
    // retrieve an authentication builder without changing the default
    services.AddAuthentication()
        // add JWT bearer now
       .AddJwtBearer(options =>
       {
           // …
       });
    

    AddMicrosoftIdentityWebAppAuthentication
    实际上是执行以下操作:

    services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApp(…)
    
    因此,它将默认方案配置为OIDC方案,并运行
    AddMicrosoftIdentityWebApp
    来配置最终执行的操作

    现在,实际上可以在服务集合上多次调用。您只需小心不要错误地重新配置。无参数函数不能做到这一点,因此访问
    IAAuthenticationBuilder
    是进一步配置身份验证的好方法

    这意味着您可以像这样更改代码:

    // configure Microsoft Identity Web first
    // this also sets the default authentication to OIDC
    services.AddMicrosoftIdentityWebAppAuthentication(Configuration);
    
    // retrieve an authentication builder without changing the default
    services.AddAuthentication()
        // add JWT bearer now
       .AddJwtBearer(options =>
       {
           // …
       });
    

    我喜欢你的解释,也许这也是正确的答案。然而,我想知道一个非常快速的替代方案,这是正确的理解,还是会改变目的。我们可以使用类似于services.AddAuthentication().AddJwtBearer().AddMicrosoftIdentityWebAppAuthentication(配置)的内容来代替你编写的代码换句话说,只需先在管道中添加JWTBeaer,然后添加MicrosoftIdentityWebAppAuthentication-这是否与您的示例相同?@BrijeshShah因为必须对服务集合调用
    AddMicrosoftIdentityWebAppAuthentication
    ,所以您不能在
    AddJwtBearer
    之后直接运行它。您必须调用再次打开
    services
    。但是,是的,您可以在不影响任何内容的情况下切换我的示例中的这两个语句。顺序只对配置有影响(以后的配置可能会覆盖以前的配置)。我喜欢你的解释,也许这也是正确的答案。但是,我想知道一个非常快速的替代方案,这是正确的理解还是会改变目的。我们可以使用类似于services.AddAuthentication().AddJwtBearer().AddMicrosoftIdentityWebAppAuthentication(配置)的内容来代替你编写的代码换句话说,只需先在管道中添加JWTBeaer,然后添加MicrosoftIdentityWebAppAuthentication-这是否与您的示例相同?@BrijeshShah因为必须对服务集合调用
    AddMicrosoftIdentityWebAppAuthentication
    ,所以您不能在
    AddJwtBearer
    之后直接运行它。您必须调用再次打开
    services
    。但是,是的,在我的示例中,您可以在不影响任何内容的情况下切换这两个语句。顺序只对配置有影响(以后的配置可能会覆盖以前的配置)。