简体   繁体   English

Blazor @attribute [Authorize] 标签不起作用

[英]Blazor @attribute [Authorize] tag is not working

I have a working .NET Core 3.0 MVC website, using AzureAD for authentication, this all works fine.我有一个工作 .NET Core 3.0 MVC 网站,使用 AzureAD 进行身份验证,这一切都很好。 I have started to migrate some of the front-end pages to Blazor (in same project) but cannot get authentication to work.我已经开始将一些前端页面迁移到 Blazor(在同一个项目中),但无法使身份验证工作。

I have added the @attribute [Authorize] tag to the top of Index.razor but I do not get redirected to Azure to login as I would do when adding it to a standard ASP.NET MVC Controller. I have added the @attribute [Authorize] tag to the top of Index.razor but I do not get redirected to Azure to login as I would do when adding it to a standard ASP.NET MVC Controller.

Startup.ConfigureServices启动.ConfigureServices

services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
    Configuration.GetSection("OpenIdConnect").Bind(options);
});
services.AddAuthorizationCore(options =>
{
    options.AddPolicy(Policies.AccessRole, Policies.IsAccessPolicy());
    options.AddPolicy(Policies.AdminRole, Policies.IsAdminPolicy());
});

Startup.Configure启动.配置

app.UseAuthentication();
app.UseAuthorization();

Index.razor索引.razor

@page "/"
@attribute [Authorize(Policy = Policies.AccessRole)]

Policies政策

public static class Policies
{
    public const string AccessRole = "Access";
    public const string AdminRole = "Admin";

    public static AuthorizationPolicy IsAccessPolicy()
    {
        return new AuthorizationPolicyBuilder().RequireAuthenticatedUser()
                                               .RequireRole(AccessRole)
                                               .Build();
    }

    public static AuthorizationPolicy IsAdminPolicy()
    {
        return new AuthorizationPolicyBuilder().RequireAuthenticatedUser()
                                               .RequireRole(AdminRole)
                                               .Build();
    }
}

If I navigate to an MVC page I get authenticated by AzureAD, if I then return to the Blazor page I can use the following successfuly如果我导航到 MVC 页面,我将通过 AzureAD 进行身份验证,如果我随后返回 Blazor 页面,我可以成功使用以下内容

<AuthorizeView Policy="@Policies.AccessRole">
    <p>Is in Access policy.</p>
</AuthorizeView>

<AuthorizeView Policy="@Policies.AdminRole">
    <p>Is in Admin policy.</p>
</AuthorizeView>

So to summarise, my Blazor page is not automatically issuing the auth challenge when using the [Authorize] attribute.总而言之,我的 Blazor 页面在使用 [Authorize] 属性时不会自动发出身份验证质询。

Does anyone know what I am doing wrong?有谁知道我做错了什么?

Update更新

It's as designed https://github.com/aspnet/AspNetCore/issues/13709按照设计https://github.com/aspnet/AspNetCore/issues/13709

As a workaround I have added a component to redirect to a login page作为一种解决方法,我添加了一个组件以重定向到登录页面

App.razor App.razor

<Router AppAssembly="@typeof(Program).Assembly">
    <Found Context="routeData">
        <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
            <NotAuthorized>
                <AuthChallenge></AuthChallenge>
            </NotAuthorized>
        </AuthorizeRouteView>
    </Found>
    <NotFound>
        <CascadingAuthenticationState>
            <LayoutView Layout="@typeof(MainLayout)">
                <p>Sorry, there's nothing at this address.</p>
            </LayoutView>
        </CascadingAuthenticationState>
    </NotFound>
</Router>

AuthCallenge.razor AuthCallenge.razor

@inject NavigationManager Navigation

@code {
    protected override void OnInitialized()
    {
        Navigation.NavigateTo("/Account/SignIn", true);
    }
}

Take a look at your App.razor file.查看您的 App.razor 文件。 Do you use RouteView or AuthorizeRouteView?你使用 RouteView 还是 AuthorizeRouteView?

You need to define an AuthorizeRouteView as described on the "ASP.NET Core Blazor authentication and authorization" page .您需要按照“ASP.NET Core Blazor 身份验证和授权”页面中的说明定义 AuthorizeRouteView。

<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
    <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
        <NotAuthorized>
            <h1>Sorry</h1>
            <p>You're not authorized to reach this page.</p>
            <p>You may need to log in as a different user.</p>
        </NotAuthorized>
        <Authorizing>
            <h1>Authentication in progress</h1>
            <p>Only visible while authentication is in progress.</p>
        </Authorizing>
    </AuthorizeRouteView>
</Found>
<NotFound>
    <CascadingAuthenticationState>
        <LayoutView Layout="@typeof(MainLayout)">
            <h1>Sorry</h1>
            <p>Sorry, there's nothing at this address.</p>
        </LayoutView>
    </CascadingAuthenticationState>
</NotFound>

Seems like the AuthorizeAttribute doesn't do all that much if that component is missing.如果缺少该组件,似乎 AuthorizeAttribute 并没有做那么多。

You must use CascadingAuthenticationState as root in the XML您必须在 XML 中以 root 身份使用 CascadingAuthenticationState

<CascadingAuthenticationState>
    <Router AppAssembly="@typeof(Program).Assembly">
        <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(EmptyLayout)">
                <NotAuthorized>
                    <RedirectLogin />
                </NotAuthorized>
            </AuthorizeRouteView>
        </Found>
        <NotFound>
            <LayoutView Layout="@typeof(MainLayout)">
                <h1>404 Error</h1>
                <p>Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
</CascadingAuthenticationState>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM