HandleRequirementAsync not being called in Auth handler in Blazor app

时光总嘲笑我的痴心妄想 提交于 2020-05-28 07:33:45

问题


Im trying to come to grips with using a custom auth handler in a serverside Blazor app. I have a breakpoint in my handler, but its not getting hit. Whats missing?

Requirement

public class ValidUserRequirement : IAuthorizationRequirement
{
    public bool MustBeValid { get; }

    public ValidUserRequirement(bool IsValid)
    {
        MustBeValid = IsValid;
    }
}

Policies

public static class Policies
{

    public const string IsValidUser = "IsValidUser";


    public static AuthorizationPolicy IsValidUserPolicy()
    {
        return new AuthorizationPolicyBuilder().RequireAuthenticatedUser()                                                  
                                               .Build();
    }
}

Handler

public class MyAuthHandler : AuthorizationHandler<ValidUserRequirement>
{

    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, ValidUserRequirement requirement)
    {    
        // will do checks against Db here
        return Task.CompletedTask;
    }
}

Excerpt from startup.cs

// Our services
services.AddAuthorization(config =>
{
    config.AddPolicy("IsValidUser", policy => policy.AddRequirements(new ValidUserRequirement(true)));
});
services.AddSingleton<IAuthorizationHandler, MyAuthHandler>();
services.AddScoped<AuthenticationStateProvider, ServerAuthenticationStateProvider>();   

auth attribute on blazor page

@attribute [Authorize(Policy = Policies.IsValidUser)]   

app.razor

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

[ update ]
Ok, I created a new project, server-side blazor, using windows auth and pulled in this same code. I could successfully hit a breakpoint in the auth handler, so something is off in the project im working on.

来源:https://stackoverflow.com/questions/61963205/handlerequirementasync-not-being-called-in-auth-handler-in-blazor-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!