0
votes

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)
        {
        }
1
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
Is the colon : after new[] in the second attribute, [MyCustomAuthorizeAttribute (permissions : new[]:{"Permission3", "Permission4"})] meant to be there? - AlwaysLearning
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

1 Answers

0
votes

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.