Bypass Authorize Attribute in .Net Core for Release Version

前端 未结 6 1950
梦如初夏
梦如初夏 2020-12-07 02:20

Is there a way to \"bypass\" authorization in asp.net core? I noticed that the Authorize attribute no longer has a AuthorizeCore method with which you could use to make dec

6条回答
  •  一生所求
    2020-12-07 02:55

    You can define your own handler that disables authorization:

    public class DisableAuthorizationHandler : AuthorizationHandler
        where TRequirement : IAuthorizationRequirement
    {
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, TRequirement requirement)
        {
            context.Succeed(requirement);
    
            return Task.CompletedTask;
        }
    }
    

    and then register it:

        public void ConfigureServices(IServiceCollection services)
        {
        //...
    #if DEBUG
        services.AddTransient>();
    #endif
        //...
        }
    

提交回复
热议问题