我创建了简单的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。
我该怎么办?