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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:14:17+00:00 2026-06-06T12:14:17+00:00

In my ASP.NET MVC application, I’m trying to figure out whether the user has

  • 0

In my ASP.NET MVC application, I’m trying to figure out whether the user has access to a particular controller, restricted by the authorize data annotation as follows

[Authorize(Roles = "user")]

I’m attempting to override OnAuthorization in order to check:-

  • If the request is authenticated (which works great)
  • If the user is authorised to access the requested view (which doesn’t work)

My user roles are stored in a SessionManager object I’ve created – SessionManager.ActiveUser.Roles

Here’s what I have in the form of pseudo-code but if anybody could help me get this right, I’d really appreciate it.

public class HomeBaseController : Controller
{
    protected override void OnAuthorization(AuthorizationContext context)
    {
        if (context.HttpContext.User.Identity.IsAuthenticated)
        {
            // these values combined are our roleName 

            bool isAuthorised = context.HttpContext.User.IsInRole(context.RequestContext.HttpContext.User.Identity.); 


            if (!context.HttpContext.User.IsInRole(---the roles associated with the requested controller action (e.g. user)---))
            {
                var url = new UrlHelper(context.RequestContext);
                var logonUrl = url.Action("LogOn", "SSO", new { reason = "youAreAuthorisedButNotAllowedToViewThisPage" });
                context.Result = new RedirectResult(logonUrl);

                return;
            } 
        }
    }
  • 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-06T12:14:20+00:00Added an answer on June 6, 2026 at 12:14 pm

    As far as overriding OnAuthorization according to ProASP.NET MVC3 Book they do not recommend overriding it since the default implementation of this method securely handles content cached using OutputCache Filter.

    If you are looking for Custom Authentication (using Forms Auth) and Authorization (Using Role provider logic then below is how I secured my application.

    EDIT: The following logic uses in-built forms authentication and roles manager. Once user is authenticated and authorized the User Identity can be used to check both the authentication (User.Identity.IsAuthenticated) and the roles User.IsInRole(“admin”)

    In Web.Config:

    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="15" slidingExpiration="true" enableCrossAppRedirects="false" protection="All" />
    </authentication>
    <roleManager enabled="true" defaultProvider="MyRolesProvider" cacheRolesInCookie="true" cookieProtection="All">
      <providers>
        <clear />
        <add name="MyRolesProvider" type="MyApp.Library.CustomRolesProvider" />
      </providers>
    </roleManager>
    

    For Role Authorization Extend RoleProvider and override methods as required.

    public class CustomRolesProvider : RoleProvider
    {
        public override string[] GetRolesForUser(string username)
        {
           // You need to return string of Roles Here which should match your role names which you plan to use.
           //Some Logic to fetch roles after checking if User is Authenticated...    
    
            return new string[] { "admin" , "editor" };
        }
    
        //Rest all of them I have kept not implemented as I did not need them...
    
    
    }
    

    In Your controller Now you can use this:

     [Authorize(Roles="Admin")]
        public class AdminController : Controller
        {
        ....
    
        }
    

    For Authentication I have implemented my custom Authentication Check but I still use Forms Authentication:

    //This one calls by Custom Authentication to validate username/password
    public ActionResult LogOn(LogOnViewModel model, string returnUrl)
    {
        if(Authenticate("test","test"))
        {
         .......
        }
    }
    
    public bool Authenticate(string username, string password)
    {
       //Authentication Logic and Set the cookie if correct else false.
       //..... your logic....
       //.....
    
       FormsAuthentication.SetAuthCookie(username, false);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing an asp.net mvc application having a Data Access Layer (DAL). Having
I am creating an ASP.NET MVC application that has postcode lookup functionality. I capture
A controller in my ASP.NET MVC application pre-populates form data displayed by my view
I have an ASP.NET MVC application, with some RESTful services that I'm trying to
In an ASP.NET MVC application (which has very little chance of having its view
My ASP.NET MVC application has pages with attachments and these attachments can be of
I have a an ASP.NET MVC application that will need to access file resources
I have an ASP.NET MVC 2 application with a custom StructureMap controller factory to
Where in my ASP.NET MVC application should I define my AutoMapper mappings? Mapper.CreateMap<User, UserViewModel>();
I am developing an ASP.NET MVC application that has two kind of pages: (1)

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.