Asp.net web api 自定义授权-Web Api

Asp.net web api 自定义授权-Web Api,asp.net-web-api,asp.net-web-api2,asp.net-identity,Asp.net Web Api,Asp.net Web Api2,Asp.net Identity,我有一个web api,需要为其实现自定义身份验证和授权。授权应按如下所示的资源和操作进行定义: [Authorize("users","view")] public async Task<IHttpActionResult> GetAsync() { } [授权(“用户”、“查看”)] 公共异步任务GetAsync() { } 是否有任何方法可以使用自定义授权筛选器并实现此功能 此外,web api由客户端证书保护,调用方由在请求头中传递的密钥标识。使用自定义身份验证筛选器对密

我有一个web api,需要为其实现自定义身份验证和授权。授权应按如下所示的资源和操作进行定义:

[Authorize("users","view")]
public async Task<IHttpActionResult> GetAsync()
{
}
[授权(“用户”、“查看”)]
公共异步任务GetAsync()
{
}
是否有任何方法可以使用自定义授权筛选器并实现此功能

此外,web api由客户端证书保护,调用方由在请求头中传递的密钥标识。使用自定义身份验证筛选器对密钥进行身份验证

雷加拉德


John

是的,您可以创建自定义授权,并设置需要授权的控制器或方法

示例如下所示

public class CustomAuthorize : System.Web.Http.AuthorizeAttribute
    {
        private string Resource { get; set; }
        private string Action { get; set; }
        public CustomAuthorize(string resource, string action)
        {
            Resource = resource;
            Action = action;
        }
        public override void OnAuthorization(
               System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            base.OnAuthorization(actionContext);
            //Check your post authorization logic using Resource and Action
            //Your logic here to return authorize or unauthorized response 
        }
    }
在这里,您可以执行自定义授权逻辑,示例控制器如下所示

public class DoThisController : ApiController
{
    [CustomAuthorize("users","view")]// for specific methods
    public string Get()
    {
        return "Sample Authorized";
    }
}

是的,您可以创建自定义授权,并设置需要授权的控制器或方法

示例如下所示

public class CustomAuthorize : System.Web.Http.AuthorizeAttribute
    {
        private string Resource { get; set; }
        private string Action { get; set; }
        public CustomAuthorize(string resource, string action)
        {
            Resource = resource;
            Action = action;
        }
        public override void OnAuthorization(
               System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            base.OnAuthorization(actionContext);
            //Check your post authorization logic using Resource and Action
            //Your logic here to return authorize or unauthorized response 
        }
    }
在这里,您可以执行自定义授权逻辑,示例控制器如下所示

public class DoThisController : ApiController
{
    [CustomAuthorize("users","view")]// for specific methods
    public string Get()
    {
        return "Sample Authorized";
    }
}

我想你不明白我真正的问题是什么。我想编写可以接受两个参数的自定义授权属性—资源和操作。我应该能够通过提供用户试图访问的资源以及用户对该资源执行的操作来定义方法的授权。@SillyJohn请检查编辑后的答案是否符合您的特定目的。@SillyJohn请检查您的要求,当你投反对票时,请给出一个合理的理由,说明该解决方案对你没有用处。我已更改为特定的用途,这是否有帮助?为什么我出现错误?找不到类型或命名空间名称“CustomAuthorizeAttribute”(您是否缺少using指令或程序集引用?)…TestController.csI认为您不理解我真正的问题是什么。我想编写可以接受两个参数的自定义授权属性—资源和操作。我应该能够通过提供用户试图访问的资源以及用户对该资源执行的操作来定义方法的授权。@SillyJohn请检查编辑后的答案是否符合您的特定目的。@SillyJohn请检查您的要求,当你投反对票时,请给出一个合理的理由,说明该解决方案对你没有用处。我已更改为您的特定用途,这是否有帮助?为什么我出现错误?找不到类型或命名空间名称“CustomAuthorizeAttribute”(您是否缺少using指令或程序集引用?)…TestController.cs