0

I am using multiple AuthorizeAttribute on my methods and it's giving me compilation error. This error comes only on build machine and somehow doesn't come on my local machine.

Rosyln Error CS0579: Duplicate 'AuthorizeAttribute' attribute

From below SO post, it seems okay to use multiple authorize attribute. I tried adding this on my custom authorize attribute -> [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] but it doesn't seem to work.

Multiple Authorization attributes on method

Is it possible to use multiple authorizeAttribute? If yes, what am I missing?

    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
    public class MyCustomAuthorizeAttribute : AuthorizeAttribute
        {
        }


 [MyCustomAuthorizeAttribute (permissions : new[]{"Permission1", "Permission2"})]
 [MyCustomAuthorizeAttribute (permissions : new[]:{"Permission3", "Permission4"})]    
 public string GetSomething(string someId)
        {
        }
user911
  • 1,269
  • 5
  • 20
  • 41
  • In the custom Authorize attributes that I've built, I don't add `[AttributeUsage]`. I'm not sure why that would cause an issue, but try taking it out. Also, the correct usage is `Authorize(Roles = "role1, role2")`. – howcheng Sep 09 '19 at 21:33
  • Is the colon `:` after `new[]` in the second attribute, `[MyCustomAuthorizeAttribute (permissions : new[]:{"Permission3", "Permission4"})]` meant to be there? – AlwaysLearning Sep 09 '19 at 21:37
  • 1
    I do not think you can have new operator in attributes. It has to be something that can be evaluated at compile time. – Optional Option Sep 09 '19 at 22:02

1 Answers1

0

Avoid any function calls in your attributes. Instead of

[MyCustomAuthorizeAttribute (permissions : new[]{"Permission1", "Permission2"})]

use code like this:

[MyCustomAuthorizeAttribute(permissions: "Permission1,Permission2")]

You can split the string in your attributes object.

Optional Option
  • 1,261
  • 8
  • 26