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 8128365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:51:12+00:00 2026-06-06T07:51:12+00:00

In my asp.net mvc3 application, I have a custom Authorization Attribute as seen below.

  • 0

In my asp.net mvc3 application, I have a custom Authorization Attribute as seen below.

public class CustomAuthorize : AuthorizeAttribute
{
    public IAccountRepository AccountRepository { get; set; }

    public CustomAuthorize()
    {
        this.AccountRepository = new UserModel();
    }

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        base.AuthorizeCore(httpContext);
        return AccountRepository.isEnabled(HttpContext.Current.User.Identity.Name);
    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
    {
        base.HandleUnauthorizedRequest(filterContext);
    }
}

I have the [CustomAuthorize] tag on my controller actions, and the AuthorizeCore method works fine – it performs the logic I want it to (making sure the account is actually enabled), and then returning as such.

However, the overridden HandleUnauthorizedRequest method, which as I understand it should allow me to control the behaviour of an unauthorized request, is not running at all. I put a breakpoint there, I put code in there, I access my application unauthorized, and the code never runs.

What am I missing?

EDIT: I did some more research and found a few other people who had this problem, but no solution unfortunately.

EDIT2: Sample code

[CustomAuthorize]
public class UserController: Controller
{
    public UserController() 
    {
        //do stuff here
    }
}

EDIT 3: @Fabio

Here’s what I’m trying to do. I have a login page (forms auth) that works fine – it calls my custom login, and then calls my AuthorizeCore override. My application uses a large amount of ajax calls, and my eventual goal is for whenever a user is using the application, and the administrator disables them, making an ajax call after being disabled (though still being logged in) should log them out. However, in order to do this, i want to return a custom response if the user is making an ajax call, and for that, I need to ovverride HandleUnauthorizedRequest. But my Authorize Core (and by extension HandleUnauthorizedRequest) are being ignored if the user is logged in (despite the fact that I have customauthorize tags on all of my controller actions that the ajax is calling).

In short: I want to authorize the user on every request, not just the login request (which seems to be what the membership provider is doing right now)

  • 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-06-06T07:51:13+00:00Added an answer on June 6, 2026 at 7:51 am

    I ended up changing my approach a fair bit. I implemented individual permissions checking, and then that caused AuthorizeCore to be called every time (and not be cached, which I guess was what was happening before).

    Interestingly enough, putting a breakpoint on the HandleUnauthorizedRequest override still doesn’t break, but putting it inside the method will. Strange, and threw me off for a bit, but I’ve solved it now.

    Code if anyone is interested:

    public class CustomAuthorize : AuthorizeAttribute
    {
        public string Permissions { get; set; }
    
        private IAccountRepository AccountRepository { get; set; }        
    
        private string[] permArray { get; set; }
    
        private string reqStatus { get; set; }
    
        public CustomAuthorize()
        {
            this.AccountRepository = new UserModel();
        }
    
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            base.AuthorizeCore(httpContext);
    
            if (Permissions != null) {
                permArray = Permissions.Trim().Split(' ');
    
                if (AccountRepository.isEnabled(httpContext.User.Identity.Name)) {
                    this.reqStatus = "permission";
                    return AccountRepository.hasPermissions(permArray);                     
                } else {
                    return false;
                }
            } else {
                return AccountRepository.isEnabled(httpContext.User.Identity.Name);
            }
        }
    
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
        {
            if (this.reqStatus == "permission") {
                filterContext.Result = new RedirectResult(MvcApplication.eM.cause("no_permission", "redirect"));
            } else {
                base.HandleUnauthorizedRequest(filterContext);
            }
        }
    }
    

    And then I decorated the controller with this:

    [CustomAuthorize(Permissions="test_perm")]

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

Sidebar

Related Questions

I am creating an ASP.NET MVC3 application using NHIBERNATE. I have a base class
I have created a custom attribute in my asp.net MVC application using the following
I have a ASP.NET MVC3 application that handles time-consuming processes (copying a large file
This is about asp.net mvc3 web application. We have used Object cache to store
My application is in Asp.Net MVC3 coded in C#.Net. I have Views which contains
I have an employee class in model for ASP.NET MVC3. There is a field
Here's the issue at hand: I have developed an ASP.NET MVC3 application using Razor.
I have been working on asp.net mvc3 e-commerce application based on NopCommerce. Recently we
Looking for a custom authorization solution for a asp.net mvc 3 application with sql
I am designing an ASP.NET MVC3 application, and I would like to have a

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.