授权处理程序

时间:2018-05-07 15:02:03

标签: c# authentication asp.net-core

我创建了简单的AuthorizationHandler,如下所示:

public class IdentityHandler<TRole> : AuthorizationHandler<IdentityRequirement<TRole>>, ITokenValidator
        where TRole : Role
{
    protected AuthorizationHandlerContext AuthContext { get; private set; }              

    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PcIdentityRequirement<TRole> requirement)
    {
        AuthContext = context;
        //some checking
        //if no reuqiremnets then call ThrowUnathorizedException()
        context.Succeed(requirement);
        return Task.CompletedTask;
    }

    public virtual Task ThrowUnathorizedException(string message)
    {
        AuthContext.Fail();
        return Task.CompletedTask;
    }
}

和要求如下:

public class IdentityRequirement<TRole> : IAuthorizationRequirement
        where TRole : Role
{
    public TRole Role { get; }

    public PcIdentityRequirement()
    {
        Role = Activator.CreateInstance<TRole>();
    }
}

我还将处理程序添加到Startupfile

AddAuthorization(options => options.AddPolicy(name, policy => policy.Requirements.Add(requirement)));

成功满足要求后,一切正常。但是,如果我未在context.Succeed (req)中设置HandleRequirementAsync,则会返回500并发送消息:

  

未指定authenticationScheme,但没有   找到DefaultChallengeScheme。

我该怎么办?

0 个答案:

没有答案