简体   繁体   English

授权属性重新定义不起作用

[英]Authorize Attribute redefinition does not work

I've been meddling a bit with Authorization for a while and found a few pages that helped me override the default methods. 我已经与授权进行了一段时间的接触,发现有几页可以帮助我覆盖默认方法。 However, it seems that I cannot get the attribute to work. 但是,似乎我无法使该属性正常工作。 I debug my project with Visual Studio 2013 and yet it still does not stop at the breakpoint where the Authorize method is redifined. 我使用Visual Studio 2013调试了我的项目,但它仍未在重新定义Authorize方法的断点处停止。 This wouldn't be an issue if the web services worked after placing the [Authorize] over the class or even individual methods. 如果在将[Authorize]放在类甚至单个方法上之后Web服务可以工作,这将不是问题。 I've tried them all yet it seems that everything it returns is 401 Unauthorized. 我已经尝试了所有方法,但似乎返回的所有内容都是401未经授权。 It doesn't even enter the desired Web service either. 它甚至也没有输入所需的Web服务。 Here is a few samples to explain my problem: 以下是一些样本来解释我的问题:

[Authorize]
[RoutePrefix("api/MyWS")]
public class MyWSController : ApiController
{
    private Test_UnitOfWork unitOfWork = new Test_UnitOfWork();

    [Route("Get"), HttpGet]
    public IEnumerable<MyWS> Get()
    {
        return unitOfWork.MyWSRepository.Get().OrderBy(s => s.Name);
    }

Redefinition of OnAuthorization method 重新定义OnAuthorization方法

public class TokenValidationAttribute : System.Web.Http.AuthorizeAttribute
{
    public override void OnAuthorization(HttpActionContext actionContext)
    { ...

Controller calling the web service 控制器调用Web服务

MyWSlist = client.GetSync<IList<MyWS>>("MyWS");

I'd like to know what I'm doing wrong here, seeing as whenever I try to debug my project, I can't seem to enter the rewriten code at all, it just returns the Unauthorized error. 我想知道我在这里做错了什么,因为每当我尝试调试项目时,我似乎根本无法输入重写的代码,它只会返回未授权的错误。 I've also tried to rewrite the other methods but no dice. 我也尝试重写其他方法,但没有骰子。 Regards 问候

I debug my project with Visual Studio 2013 and yet it still does not stop at the breakpoint where the Authorize method is redifined. 我使用Visual Studio 2013调试了我的项目,但它仍未在重新定义Authorize方法的断点处停止。

After overriding AuthorizeAttribute , you want to apply that to controller or action unless you configure as Global filter . 覆盖AuthorizeAttribute之后除非将其配置为Global filter,否则要将其应用于控制器或操作。

[TokenValidation]
public class MyWSController : ApiController
{
  ...
}

Or, am I missing something in your question? 或者,我在您的问题中遗漏了什么吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM