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

  • SEARCH
  • Home
  • 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 585853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:02:45+00:00 2026-05-13T15:02:45+00:00

I am trying to add authorization to my controllers and it’s not working… I

  • 0

I am trying to add authorization to my controllers and it’s not working…

I am not sure where to look in my program, but adding the

[Authorize] 

filter in my controller is not working, let alone anything like

[Authorize(Roles = "Manager")]

I have been able to get this working in the default application that is provided when creating a new MVC project (i.e., I am able to make the “about” tab redirect to the login screen if I’m not logged in), so I assume I have mucked things up along the way as I’ve built my app. Does anyone know where I should be looking to fix this? I have users and they have roles; I’m using the ASP.net schema that is auto-created; I’ve examined my web.config file up and down and although I’m pretty new to this, nothing seems to be out of place. I have no clue why my authorization filters aren’t working.?.

  • 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-13T15:02:46+00:00Added an answer on May 13, 2026 at 3:02 pm

    I wrote a custom attribute to solve this problem. You can attribute your controller methods as follows:

    [RequiresRole(Role="Admin")]
    public ActionResult Index()
    {
        int i = 5 + 5;
    
        return View();
    }
    

    The code for the attribute is as follows….

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Security;
    
    namespace Web.Controllers
    {
        public class RequiresRoleAttribute : ActionFilterAttribute
        {
            public string Role { get; set; }
    
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                if (string.IsNullOrEmpty(Role))
                {
                    throw new InvalidOperationException("No role specified.");
                }
    
                string redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsolutePath;
                string redirectUrl = string.Format("?returnUrl={0}", redirectOnSuccess);
                string loginUrl = FormsAuthentication.LoginUrl + redirectUrl;
    
                if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
                {
                    filterContext.HttpContext.Response.Redirect(loginUrl, true);
                }
                else
                {
                    bool isAuthorised = filterContext.HttpContext.User.IsInRole(this.Role);
                    if (!isAuthorised)
                    {                        
                        filterContext.HttpContext.Response.Redirect(loginUrl, true);
                    }                
                }  
            }      
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to add a class object into a List using reflection, but when invoking
Im trying to add a directory foo to my repo, but there are some
I am trying to implement a custom authorization attribute on my Web API controllers,
I'm trying to add basic authorization to my request header. The code below compiles
I'm trying to add simple Authentication and Authorization to an ASP.NET MVC application. I'm
I am trying add value to a mutlidimensional array but I am slightly confused
I'm following Ryan Bates' declarative authorization railscast. I'm trying to add functionality for author
I am working on an application that someone else wrote, trying to add some
Trying to add a 'box' to a form at design time, I looked up
Trying to add email notification to my app in the cleanest way possible. When

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.