【问题标题】:How to use Azure AppRoles in Blazor Server with Azure Active Directory如何将 Blazor 服务器中的 Azure AppRoles 与 Azure Active Directory 结合使用
【发布时间】:2021-12-21 12:46:56
【问题描述】:

我有一个正在运行的 .NET 5 Web-API,它带有一个生产环境中的 Blazor 服务器客户端。我想使用应用角色从个人用户帐户切换到 Azure AD,以对我的控制器中的特定用户进行身份验证。我找到了很多关于 Webassembly 的信息,但没有找到关于 Blazor Server 的信息。

是否有人为带有 Blazor 服务器客户端并集成 Azure 应用角色的 .NET 5/6 Web-Api 提供有效的解决方案?

应用程序已经在 Azure 门户中注册等等,我只需要知道如何将应用程序角色特定的东西传递给我的 API,这样我的控制器就可以使用 [Authorize("Admin")] 东西。我怀疑它也会使用不记名令牌。

编辑: 非常感谢您的阅读。所以我发现如果我在我的控制器中使用这样的东西,只使用 [Authorize] 属性而不使用任何角色:

 var identities = HttpContext.User.Identities.ToList();
            foreach (var item in identities)
            {
                if (item.RoleClaimType == "admin")
                {
                    // return or do something
                }
            }

它可以正常工作,但必须有一些更顺畅的解决方案,还是我这样做完全错误?当我查看 WASM 示例时,他们会使用令牌获取 AppRoles,而控制器可以简单地使用 [Authorize(Roles = "xyz")] 属性。我在这里想念什么? :/

顺便说一句,这就是我的 Program.cs 现在的样子:

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddMicrosoftIdentityWebApi(options =>
            {
                builder.Configuration.Bind("AzureAd", options);
                options.TokenValidationParameters.RoleClaimType =
                    "admin";
                options.TokenValidationParameters.RoleClaimType = "doku";
            },
            options => { builder.Configuration.Bind("AzureAd", options); });

谢谢你们

【问题讨论】:

    标签: azure-active-directory blazor roles blazor-server-side


    【解决方案1】:

    请检查给定的参考资料是否对您有用。

    SERVER API 应用可以授权用户使用安全组、AAD 管理员角色和应用角色的授权策略访问安全 API 端点

    • 在 SERVER 应用的 Program.cs 中,将声明指定为 roleclaim

    示例:

         builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApi(options =>
                {
                    Configuration.Bind("AzureAd", options);
                    options.TokenValidationParameters.RoleClaimType = 
                        "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
                },
                options => { Configuration.Bind("AzureAd", options); });
    • 然后你可以使用授权控制器上的管理员角色来访问

      [授权(角色=“管理员”)]

    • App roles 部分,您可以看到两者的配置 服务器和客户端。

    • 在门户的清单编辑器中编辑应用角色,然后给出 适当的 api 权限,公开范围并授予管理员权限 同意>见Add app roles and get them from a token。 过程逻辑必须包含 api 所需的那些范围。

    注意:客户端和客户端的 appRoles 清单属性 服务器 Azure 门户应用注册必须包含相同的配置 角色。

    请查看this 了解更多有关服务器和客户端应用程序指南的详细信息。

    其他参考资料:

    1. using-app-roles-with-AAD-blazor-server-client scenario | codemaze.com
    2. quickstart-configure-app-expose-web-apis
    3. quickstart-configure-app-access-web-apis

    【讨论】:

    • 感谢您的详细解答。我刚刚用更多细节更新了帖子。如果你能给我另一个提示,那就太好了:)
    • 天哪,我是白痴。现在我知道了。非常感谢:D
    猜你喜欢
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 2018-01-25
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    相关资源
    最近更新 更多