无法在AuthorizeAttribute类上命中断点

时间:2017-05-09 15:34:34

标签: c# .net asp.net-web-api

任何人都可以告诉我为什么我无法在我创建的应用WebApi帖子请求的新类上设置断点吗?下面是我的新类的代码和使用classes属性

修饰的方法
namespace MercuryUserWeb.Core.Authorization {

public class ClientUserApi : System.Web.Http.AuthorizeAttribute
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        WebRequestState webRequestState = ContainerHome.Container.Resolve<WebRequestState>();
        ActionDescriptor actionDescriptor = filterContext.ActionDescriptor;
        if (actionDescriptor.IsDefined(typeof(StaffUserAuthorizeAttribute), true))
            return;

        if (!webRequestState.IsAuthenticated || webRequestState.ClientUser == null)
        {
            filterContext.Result = new HttpUnauthorizedResult();
            return;         // 401, always show log-in page
        }            
    }
  }  
}

控制器操作

namespace MercuryUserWeb.Areas.JApi.Controllers{
[ClientUserApi]
    public class ClientViewController: ApiController, IJApiController
    {           

    [System.Web.Http.HttpPost]
    [ClientUserApi]
    public ActionResult assignViews(ClientViewModel model)
    {
        string[] clientViewIdsArray = model.selectedViewsForClient.Split(',');
        IEnumerable<long> parsedClientViewIdArray = clientViewIdsArray.Select(long.Parse);
        List<long> clientViewsIdList = parsedClientViewIdArray.ToList();

        string[] volPoolIdArray = model.volPoolIds.Split(',');
        IEnumerable<long> parsedIdArray = volPoolIdArray.Select(long.Parse);
        List<long> volPoolIdList = parsedIdArray.ToList();

        var clientId = clientApiPrincipal.ClientUser.ClientId;

        clientViewService.assignViews(clientViewsIdList, volPoolIdList, clientId);

        return null;

    }

1 个答案:

答案 0 :(得分:0)

尝试使用override关键字。否则,您将在AuthorizeAttribute中隐藏OnAuthorization的虚拟实现。

public class ClientUserApi : System.Web.Http.AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        WebRequestState webRequestState = ContainerHome.Container.Resolve<WebRequestState>();
        ActionDescriptor actionDescriptor = filterContext.ActionDescriptor;
        if (actionDescriptor.IsDefined(typeof(StaffUserAuthorizeAttribute), true))
            return;

        if (!webRequestState.IsAuthenticated || webRequestState.ClientUser == null)
        {
            filterContext.Result = new HttpUnauthorizedResult();
            return;         // 401, always show log-in page
        }            
    }
}