HTML   发布时间:2022-04-14  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了.net – 默认情况下如何拒绝Web API项目的授权大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
除非明确允许授权,否则是否可以拒绝项目中每个ASP.NET Web API控制器的授权(即使对于经过身份验证的用户)

我正在寻找类似的东西:

WebApiConfig

config.Filters.Add(new DenyAuthorizationAttribute());   // ??

ExampleController.cs

public class ExampleController : ApiController
{
    [Authorize(Roles = "Admins")]
    public String GetHello_OnlyAdmins()
    {
        // only admins can call this
    }

    [AllowAnonymous]
    public void PostSomething_Everybody()
    {
        // ...
    }

    public void deleteSomething_NoOne()        
    {
        // nobody can call this - we want to force the progrAMMer to be specific about authorized roles
    }

}

解决方法

我通过向httpConfiguration.Filters添加custuR_225_11845@“default deny”授权过滤器来解决这个问题:
public class DefaultDenyAuthorizationFilter : AuthorizeAttribute
{
    public override void OnAuthorization(httpActionContext actionContext)
    {
        if ( null == actionContext )
            throw new ArgumentNullException("actionContext");

        if ( actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() ||
             actionContext.ActionDescriptor.GetCustomAttributes<AuthorizeAttribute>().Any() ||
             actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() ||
             actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AuthorizeAttribute>().Any() )
            return;

        base.HandleUnauthorizedrequest(actionContext);
    }
}

大佬总结

以上是大佬教程为你收集整理的.net – 默认情况下如何拒绝Web API项目的授权全部内容,希望文章能够帮你解决.net – 默认情况下如何拒绝Web API项目的授权所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。