【问题标题】:How do I use the Blazor Clientside AuthorizeView control policy feature with Azure Ad?如何将 Blazor 客户端 AuthorizeView 控制策略功能与 Azure Ad 一起使用?
【发布时间】:2020-12-16 15:56:15
【问题描述】:

我正在使用此链接中的 Microsoft 示例 https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/standalone-with-azure-active-directory?view=aspnetcore-3.1

我的 Program.cs 文件如下所示

    public class Program
    {
        public static async Task Main(string[] args)
        {
            var builder = WebAssemblyHostBuilder.CreateDefault(args);
            builder.RootComponents.Add<App>("#app");

            builder.Services.AddScoped(sp => 
             new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

            builder.Services.AddAuthorizationCore(config =>
            {
                config.AddPolicy("BackOfficeEditor", policy =>
                    policy.RequireClaim("groups", "4dd6726b-3949-4b8a-a8e0-9e5eaa65e358"));
            });


            builder.Services.AddMsalAuthentication(options =>
            {
                builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
            });

            await builder.Build().RunAsync();
        }
    }

我的 index.razor 文件看起来像这样

@page "/"
<AuthorizeView Policy="BackOfficeEditor">
    <p>You can only see this if you satisfy the 
    <b style="color:magenta;font-size:larger">
    BackOfficeEditor</b> policy.</p>
     </AuthorizeView>

    <AuthorizeView>
        <NotAuthorized>
            <br />
            You are not logged in
            <br />
        </NotAuthorized>
        <Authorized>
            <br />
            Hello, @context.User.Identity.Name! <br />
            <h2>here's the list of your claims:</h2>
            <ul>
                @foreach (var claim in context.User.Claims)
                {
                    <li><b>@claim.Type</b>: @claim.Value</li>
                }
            </ul>
        </Authorized>
    </AuthorizeView>

您可以在声明中看到用户 (minonOne) 在“组”部分拥有正确的声明,但它不显示我为该组中的用户添加的自定义消息。

我在这个配置中遗漏了什么?

【问题讨论】:

  • 请检查Github Doc,看看你是否配置正确。

标签: c# azure-active-directory blazor-webassembly .net-5 claims-authentication


【解决方案1】:
猜你喜欢
  • 2021-03-06
  • 2020-07-05
  • 1970-01-01
  • 2021-05-20
  • 2017-07-27
  • 2018-02-08
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多