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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:04:30+00:00 2026-05-21T00:04:30+00:00

I currently have a project that I seem to have ran into an issue

  • 0

I currently have a project that I seem to have ran into an issue regarding Roles and thought I would get some opinions on how to best handle the problem.

The system will require editable, flexible roles that control not only the access of specific areas, but also the use of system functions (Adding Users, Editing Users, Viewing Reports etc.)

The system currently allows users to have multiple roles, each of those roles has explicitly defined areas of access/actions, for example:

  • Role A can access areas 1,2,3 and can Add Users.
  • Role B can access areas 1,5,7 and can Modify Users.
  • Role C can access areas 4,6 and only View Users.

so a User could be in Roles A and C, and thus access : 1,2,3,4 and 6, and could Add and View Users.

My first solution was to create a dictionary that would store all of the possible areas of access/access options into a Dictionary like so:

Dictionary<string,bool>

then when it is instantiated it pulls all of the properties from the database and then iterates through the roles to determine if they are accessible.

All of that currently works just fine – however the project is quite Javascript/jQuery intensive so many of these options are called by client-side functions. I am trying to avoid having to wrap all of these client side functions with:

<%if(AccessDictionary[key])
     //Enable or Disable Action
<%}%>

So basically, I am wondering about the following things:

  1. After a user logs in, what is the best way to store this Dictionary? Statically? In the Session?
  2. What would be the best method of storage such that the Dictionary will be easily accessed in the View? (As I currently see no way around wrapping my client-side functions)

Any advice or ideas would be greatly appreciated!

  • 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-21T00:04:31+00:00Added an answer on May 21, 2026 at 12:04 am

    I would store this information in the user data part of the authentication cookie. So when a user logs in:

    public ActionResult Login(string username, string password)
    {
        // TODO: validate username/password couple and 
        // if they are valid get the roles for the user
    
        var roles = "RoleA|RoleC";
        var ticket = new FormsAuthenticationTicket(
            1, 
            username,
            DateTime.Now, 
            DateTime.Now.AddMilliseconds(FormsAuthentication.Timeout.TotalMilliseconds), 
            false, 
            roles
        );
        var encryptedTicket = FormsAuthentication.Encrypt(ticket);
        var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
        {
            // IIRC this property is only available in .NET 4.0,
            // so you might need a constant here to match the domain property
            // in the <forms> tag of the web.config
            Domain = FormsAuthentication.CookieDomain,
            HttpOnly = true,
            Secure = FormsAuthentication.RequireSSL,
        };
        Response.AppendCookie(authCookie);
        return RedirectToAction("SomeSecureAction");
    }
    

    Then I would write a custom authroize attribute which will take care of reading and parsing the authentication ticket and store a generic user in the HttpContext.User property with its corresponding roles:

    public class MyAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext.User.Identity.IsAuthenticated)
            {
                var authCookie = httpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
                if (authCookie != null)
                {
                    var ticket = FormsAuthentication.Decrypt(authCookie.Value);
                    var roles = ticket.UserData.Split('|');
                    var identity = new GenericIdentity(ticket.Name);
                    httpContext.User = new GenericPrincipal(identity, roles);
                }
            }
            return base.AuthorizeCore(httpContext);
        }
    }
    

    Next you could decorate your controllers/actions with this attribute to handle authorization:

    // Only users that have RoleA or RoleB can access this action
    // Note that this works only with OR => that's how the base
    // authorize attribute is implemented. If you need to handle AND
    // you will need to completely short-circuit the base method call
    // in your custom authroize attribute and simply handle this
    // case manually
    [MyAuthorize(Roles = "RoleA,RoleB")]
    public ActionResult Foo()
    {
        ...
    }
    

    In order to check whether a user is in a given role simply:

    bool isInRole = User.IsInRole("RoleC");
    

    Armed with this information you can now start thinking of how to organize your view models. In those view models I would include boolean properties such as CanEdit, CanViewReport, … which will be populated by the controller.

    Now if you need this mapping in each action and views things might get repetitive and boring. This is where global custom action filters come into play (they don’t really exist in ASP.NET MVC 2, only in ASP.NET MVC 3 so you might need a base controller decorated with this action filter which simulates more or less the same functionality). You simply define such global action filter which executes after each action and injects some common view model to the ViewData (holy …., can’t believe I am pronouncing those words) and thus make it available to all views in a transverse of the other actions manner.

    And finally in the view you would check those boolean value properties in order to include or not different areas of the site. As far as the javascript code is concerned if it is unobtrusively AJAXifying areas of the site then if those areas are not present in the DOM then this code won’t run. And if you needed more fine grained control you could always use HTML5 data-* attributes on your DOM elements to give hints to your external javascript functions on the authorizations of the user.

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

Sidebar

Related Questions

I have some C code in an iOS project that I would like to
Currently I have a simple maven project that is building a jar file and
I have a project that needs to be done using ASP.NET. Currently, I'm using
Hello Ruby/Rails/Merb developers! Im currently working on a web project that will have a
I have a project that currently compiles happily on my dev machine using VS
I have a project that's currently built with ant that pulls the latest trunk
I currently have a Rails website that has some Prototype scripting in it. My
Currently in my CI project I have a single controller that handles all things
I have been using TDD to drive the project that I am currently working
In my current project we have a large repository of content that was originally

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.