【问题标题】:asp.net MVC Custom Filters [RESTAuthorize] is ignoredasp.net MVC 自定义过滤器 [RESTAuthorize] 被忽略
【发布时间】:2016-05-22 07:27:55
【问题描述】:

[RESTAuthorization] 被忽略,而是跳转到代码以获取所有国家/地区而不检查 Rest Authorization 过滤器。

这是RESTAuthorization的代码

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using MyWebsite.Repository;

namespace MyWebsite.API.Attributes
{
    public class RESTAuthorizeAttribute : AuthorizeAttribute
    {
        private ISecurityRepository _repository;

        public RESTAuthorizeAttribute()
            : this(new SecurityRepository())
        {

        }

        public RESTAuthorizeAttribute(ISecurityRepository repository)
        {
            _repository = repository;
        }

        private const string _securityToken = "token";

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (Authorize(filterContext))
            {
                return;
            }

            HandleUnauthorizedRequest(filterContext);
        }

        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            base.HandleUnauthorizedRequest(filterContext);
        }

        private bool Authorize(AuthorizationContext actionContext)
        {
            try
            {
                HttpRequestBase request = actionContext.RequestContext.HttpContext.Request;
                string token = request.Params[_securityToken];
                string ip = _repository.GetIP(request);

                return _repository.IsTokenValid(token, ip, request.UserAgent);
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}

这是获取所有国家/地区的代码。 RestAuthorize 被忽略

[RESTAuthorize]
[HttpGet]
public IEnumerable<dtoCountry> GetAllCountry()
{
    try
    {
        return _repository.GetAllCountry().ToList();
    }
    catch (UnauthorizedAccessException)
    {
        throw new HttpResponseException(HttpStatusCode.Unauthorized);
    }
    catch (Exception)
    {
        throw new HttpResponseException(HttpStatusCode.InternalServerError);
    }
}

【问题讨论】:

    标签: c# asp.net-web-api asp.net-web-api2 asp.net-mvc-custom-filter


    【解决方案1】:

    假设你实现了System.Web.Http.AuthorizeAttribute,则需要实现方法:

    protected override bool IsAuthorized(HttpActionContext actionContext)
    {
    
    }
    

    我认为调用OnAuthorization 是没有必要的(但如果需要,您可以保留它),因此您的代码示例如下所示:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    using System.Web.Http.Controllers;
    
    namespace MyWebsite.API.Attributes
    {
        public class RESTAuthorizeAttribute : AuthorizeAttribute
        {
            private ISecurityRepository _repository;
    
            public RESTAuthorizeAttribute()
                : this(new SecurityRepository())
            {
    
            }
    
            public RESTAuthorizeAttribute(ISecurityRepository repository)
            {
                _repository = repository;
            }
    
            private const string _securityToken = "token";
    
            // This function actually decides whether this request will be accepted or not
            protected override bool IsAuthorized(HttpActionContext actionContext)
            {
                //TODO Return true or false, whether you need to accept this request or not
            }
        }
    }
    

    【讨论】:

    • HttpRequestContext 不包含 HttpContext 的定义
    • @NiangMoore 你能提供你正在使用的命名空间吗?
    • 使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.Mvc;使用 MyWebsite.Repository;
    • 看来您正在实现System.Web.Mvc.AuthorizeAttribute,对于ASP.NET WebAPI,System.Web.Http.AuthorizeAttribute 更合适。删除using System.Web.Mvc; 并添加using System.Web.Http; using System.Web.Http.Controllers;。
    • 是的,我已经这样做了,但仍然相同的 HttpRequestContext 不包含 HttpContext 的定义
    【解决方案2】:
     public class Authorizetest: System.Web.Http.AuthorizeAttribute
    {
        private const string _securityToken = "token"; 
        public override void OnAuthorization(HttpActionContext actionContext)
        {
    
           if(Authorize(actionContext))
            {
                return;
            }
            HandleUnauthorizedRequest(actionContext);  
        }
    
        protected override void HandleUnauthorizedRequest(HttpActionContextactionContext)
        {
            base.HandleUnauthorizedRequest(actionContext);
        }
    
        private bool Authorize(HttpActionContext actionContext)
        {         
            try
            {                           
                var context = new HttpContextWrapper(HttpContext.Current);
                HttpRequestBase request = context.Request;              
                string token = request.Params[_securityToken];
                bool xyz = ValidatingTokens.IsTokenValid(token, 
                CommonManager.GetIP(request), request.UserAgent);
                return xyz;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-01
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      相关资源
      最近更新 更多