首页 > 解决方案 > .NET Core WebApi + Angular 身份验证和授权通过 Microsoft Azure 中的应用程序

问题描述

我们在 Angular 上有 2 个 Web 应用程序(FE_1,FE_2),在 .NET Core 上有 3 个 API 应用程序(见图) 需要从一个站点一次登录并在两个站点之间工作而无需任何额外的授权过程 我的意思是,当我们登录到site1 并接收令牌 #1,我希望此令牌也可以与 API_2 一起使用,反之亦然,当我们登录到 site2 并接收令牌 #2 时,我希望使用此令牌与 API_1 一起使用

图片

所以我的问题是如何在 Azure 中正确配置应用程序并根据所描述的架构在内部配置它们?

标签: c#.netangularazure

解决方案


谢谢你曼尼什。发布您的建议作为帮助其他社区成员的答案。

从 BE 代码中提供的架构中,根据下图添加有效受众列表,使其为真

在此处输入图像描述

下面是示例代码,您可以在其中检查有效的受众代码。

services.AddAuthentication(cfg =>
{
    cfg.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    cfg.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(opt =>
{
    opt.Authority = "https://login.microsoftonline.com/common";
    opt.Audience = "api://A134d6c8-8078-2924-9e90-98cef862eb9a"; // Set this to the App ID URL for the web API, which you created when you registered the web API with Azure AD.

    opt.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuer = true, 
        ValidateAudience = true, 
        ValidAudiences = new List<String>
        {
            // you could add a list of valid audiences
            "A134d6c8-8078-2924-9e90-98cef862eb9a"
        }, 
        ValidIssuers = new List<string>
        {
            // Add tenant id after https://sts.windows.net/
            "https://sts.windows.net/{YourTenantId}"
        }
    };
    opt.Events = new JwtBearerEvents()
    {
        OnAuthenticationFailed = AuthenticationFailed
    };
});

有关完整信息,请查看SO


推荐阅读