How to get claim inside Asp.Net Core Razor View

前端 未结 3 587
名媛妹妹
名媛妹妹 2020-12-16 12:46

I did it in my rc1 project like:

User.Claims.ElementAt(#).Value

But after I switched to rtm it wouldn’t work anymore. When I debug the Razo

3条回答
  •  不知归路
    2020-12-16 13:21

    In Core 3.0, use view authorization

    Startup.cs

        services.AddAuthorization(options =>
        {
            options.AddPolicy("Claim_Name", x => x.RequireClaim("Claim_Name"));
        });
    

    Then inside the UI

        if ((AuthorizationService.AuthorizeAsync(User, "Claim_Name")).Result.Succeeded){
            //show ui element
        }
    

    View-based authorization in ASP.NET Core MVC

提交回复
热议问题