Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6550827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:15:48+00:00 2026-05-25T12:15:48+00:00

I have defined a custom Authorization Attribute and it is automaticly applied to all

  • 0

I have defined a custom Authorization Attribute and it is automaticly applied to all actions in the solution.
In its OnAuthorize method i use the IsDefined method to find if another attribute is defined but it seems that it always returns false.

Edit: the AuthorizeAttr attribute is set in the RegisterGlobalFilters function in Global.asax and the Anon attribute is marked directly above the actions that doesn´t need authorization.

Here is my code:

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

public class Role : Attribute
{
    public int Id;
    public Role(int id)
    {
        Id = id;
    }
}

public class AuthorizeAttr : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        if (!(filterContext.ActionDescriptor.IsDefined(typeof(Anon), false)) || !(filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(Anon), false)))
        {
            Procurement.User u = MvcApplication.GetCurrentUser(filterContext.HttpContext);
            if (u == null || !u.enabled)
                filterContext.Result = new RedirectResult("/Session/Login?msg=You must log in to use this site.&ReturnUrl=" + filterContext.RequestContext.HttpContext.Request.RawUrl);
            if (filterContext.ActionDescriptor.IsDefined(typeof(Role), false))
            {
                object[] criterias = filterContext.ActionDescriptor.GetCustomAttributes(typeof(Role), false);
                bool authorized = true;
                for (int x = 0; x < criterias.Length; x++)
                {
                    if (((Role)criterias[x]).Id > u.roleId)
                    {
                        authorized = false;
                        break;
                    }
                }

                if (!authorized)
                {
                    ContentResult C = new ContentResult();
                    C.Content = "<h1><b>The resource is unavailable!</b></h1>";
                    filterContext.Result = C;
                }
            }
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:15:49+00:00Added an answer on May 25, 2026 at 12:15 pm

    In boolean algebra the negation of

    isDefinedOnAction || isDefinedOnController
    

    is:

    !isDefinedOnAction && !isDefinedOnController
    

    So you probably want an && condition:

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        var isDefinedOnAction = filterContext.ActionDescriptor.IsDefined(typeof(Anon), false);
        var isDefinedOnController = filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(Anon), false);
        if (!isDefinedOnAction && !isDefinedOnController)
        {
            ... the Anon attribute is not present neither on an action nor on a controller
            => perform your authorization here
        }
    }
    

    or if you want ||:

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        var isDefinedOnAction = filterContext.ActionDescriptor.IsDefined(typeof(Anon), false);
        var isDefinedOnController = filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(Anon), false);
        if (isDefinedOnAction || isDefinedOnController)
        { 
            ... the attribute is present on either a controller or an action
            => do nothing here
        }
        else
        {
            ... perform your authorization here
        }
    }
    

    Obviously the first is by far more readable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined a custom html attribute data-something-something. In my view I use an
I have a WPF application where I have defined custom commands that use the
For my custom view I have also defined a custom attribute for keep id
I have defined a custom section in the App.config file and all the Configuration
I have defined a custom assert macro. This works fine for all other comparisons.
I have defined a custom edge and vertex type to use in an undirected
I have defined a few Custom Actions in my WIX setup, and these actions
I have defined a custom Sharepoint list for special attributes related to a software
I have defined a custom list template type for SharePoint . I install it
I have defined a custom IPrincipal and custom IIdentity based on a website that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.