【问题标题】:Can I use openid connect authentication and jwtbearer authentication scheme in one app?我可以在一个应用程序中使用 openid connect 身份验证和 jwtbearer 身份验证方案吗?
【发布时间】:2021-03-04 06:32:17
【问题描述】:

我有一个 ASP.NET Core Web 应用程序,其中前端内置在 react 中,而对于后端,我有 Web API,两者都放在一个 Web 项目中。 对于身份验证,我使用了 Azure AD open Id 连接身份验证方案。

现在我有一个要求,我需要使用客户端凭据流向外部系统公开一些 API。所以我不确定如何在当前的 Web 应用程序中实现这一点,因为它已经具有开放 id 连接身份验证方案。

有什么想法吗? 非常感谢

【问题讨论】:

  • 这个示例对您有帮助吗?
  • 在某种程度上,他们在 startup.cs 中明确使用了“services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)”方案,但在我的情况下,Web 应用程序的 startup.cs 有“services.AddAuthentication (OpenIdConnectDefaults.AuthenticationScheme)" 方案已经存在,我可以添加另一个身份验证方案吗?
  • 恐怕这行不通。
  • 您不能同时使用 oidc 和客户端凭证流。

标签: asp.net-core oauth-2.0 azure-active-directory


【解决方案1】:

您需要在 Azure 门户中创建两个应用程序。目前你已经有了一个ASP.NET Core Web应用,代表前端应用,所以需要再创建一个代表Web API的应用,也就是后端-结束应用程序。

首先需要暴露受Azure保护的后端应用的api,可以按照如下流程进行配置:

Azure 门户>应用注册>公开 API>添加范围>添加客户端应用程序

然后您需要创建后端应用的appRole,然后将该角色作为应用权限授予客户端应用。

接下来,转到客户端应用程序>API权限>添加权限>我的 API>您的 api 应用程序。

最后,您需要在没有用户登录的情况下使用client credential flow 获取访问令牌:

解析令牌:

最后可以将token传递给api应用,api应用会通过解析token对客户端应用进行认证。

Similar samples.

【讨论】:

  • 如果我的回答对您有帮助,您可以接受它作为答案(单击答案旁边的复选标记,将其从灰色切换为已填充。)。这对其他社区成员可能是有益的。谢谢。
【解决方案2】:

我有同样的情况并以这种方式解决了它:

Startup.cs

string[] scopes = new[]
            {
                "openid",
                "profile",
                "offline_access",
                "email",
                "scope_one",
                "scope_two",
            };

                services.AddAuthentication(options =>
            {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    options.Authority = "https://my-own-indentity.com";
                    options.Audience = "Any_Audience";
                    options.RequireHttpsMetadata = false;
                    options.SaveToken = true;
                })  .AddOpenIdConnect("oidc", options =>
                {
                    options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.Authority = "https://Authority.com";
                    options.ClientId = "*****";
                    options.ClientSecret = "******";
                    options.CallbackPath = "/callback";
                    options.ResponseType = "code";
                    options.ResponseType = OidcConstants.ResponseTypes.Code;
                    options.RequireHttpsMetadata = false;
                    options.SaveTokens = true;

                    options.Scope.Clear();
                    options.GetClaimsFromUserInfoEndpoint = true;

                    foreach (string scope in scopes)
                    {

                        options.Scope.Add(scope);
                    }
                });

由控制器的“AddJwtBearer”认证:

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Route("[controller]")]
[ApiController]
public class ProjectController : ControllerBase
{ 
   //your code
}

由控制器的“AddOpenIdConnect”进行身份验证::

[Authorize(AuthenticationSchemes = "oidc")]
[ApiController]
[Route("api/[controller]")]
public class SomethingWithOpenIdController : Controller
{
 //your code
}

如果您找到了其他解决方案,请随时分享,否则我希望这对您有所帮助。

【讨论】:

    【解决方案3】:

    在 Startup.cs 中

    // Add services to the container.
    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, (options) =>
     {
         options.TokenValidationParameters.ValidateIssuer = true;
         options.MetadataAddress = "metadataaddress";
         options.TokenValidationParameters.ValidAudience = "portal.com";
         // or any specific option used for your application
     });
    services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
                    .AddMicrosoftIdentityWebApp(
                options =>
                {
                    builder.Configuration.Bind("AzureAd", options);
                    // or any specific option used for your application
                });
    

    在控制器中:

    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + "," + OpenIdConnectDefaults.AuthenticationScheme)]
        [ApiController]
        [Route("[controller]")]
        public class SomeController : ControllerBase
    

    这对我有用:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-14
      • 2016-06-28
      • 2021-03-16
      • 2021-09-07
      • 1970-01-01
      • 2018-12-23
      • 2021-04-28
      • 2014-07-21
      相关资源
      最近更新 更多