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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:37:11+00:00 2026-05-26T12:37:11+00:00

I have an MVC app and I wrote a custom roleprovider for it as

  • 0

I have an MVC app and I wrote a custom roleprovider for it as shown:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using VectorCheck.Models;

namespace VectorCheck.Security
{
    public class MyRoleProvider : RoleProvider
    {
        private VectorCheckRepository<User> _repository { get; set; }

        public MyRoleProvider()
        {
            _repository = new VectorCheckRepository<User>();
        }

        public MyRoleProvider(VectorCheckRepository<User> repository)
        {
            _repository = repository;
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string username)
        {
            var user = _repository.GetUser(username);

            return new string[] { user.Role.Name };
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            var user = _repository.GetUser(username);

            return string.Compare(user.Role.Name, roleName, true) == 0;
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}

This works really well with restricting access to controllers and actions using:

[Authorize(Roles = "Administrator")]

above the controller or action.

I also want restricted access to some things in the view though using:

HttpContext.Current.User.IsInRole("Administrator")

This method isn’t part of my roleprovider though so isn’t getting overridden.

Does anyone know how to do it for this method as well?

  • 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-26T12:37:12+00:00Added an answer on May 26, 2026 at 12:37 pm

    If you’ve hooked your RoleProvider as the role provider for the application in web.config, then this should work automatically; the framework will create a RolePrincipal for an authenticated user at the start of the request that will call the GetRolesForUser method on your role provider, passing the name from the IIdentity as the user name.

    The framework implementation of RolePrincipal‘s IsInRole(string role) method is something like this (I’ve added comments)

    public bool IsInRole(string role) 
    { 
        if (_Identity == null)
            throw new ProviderException(SR.GetString(SR.Role_Principal_not_fully_constructed)); 
    
        if (!_Identity.IsAuthenticated || role == null)
            return false;
        role = role.Trim(); 
        if (!IsRoleListCached) {
            _Roles.Clear(); 
    
            // here the RoleProvider is used to get the roles for the user
            // and are cached in a collection on the RolePrincipal so that
            // they are only fetched once per request
            string[] roles = Roles.Providers[_ProviderName].GetRolesForUser(Identity.Name); 
            foreach(string roleTemp in roles)
                if (_Roles[roleTemp] == null) 
                    _Roles.Add(roleTemp, String.Empty);
    
            _IsRoleListCached = true;
            _CachedListChanged = true; 
        }
        return _Roles[role] != null; 
    } 
    

    Set a breakpoint inside of your RoleProvider GetRolesForUser method to ensure that it is being called correctly and also inspect the IPrincipal (HttpContext.Current.User) to ensure that it is of type RolePrincipal for an authenticated user.

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

Sidebar

Related Questions

I have configured in following way that spring MVC app using Spring 3.1.1.RELEASE web.xml
Spring MVC web app: I have a stack trace w/o line numbers (shown at
I have an MVC app with custom membership and role providers. I am checking
I have a MVC app and using JQuery. I have anchor that I setup
I have C# MVC web app that has some textboxes that in IE9 you
I have an ASP.Net-MVC app using LinqToSql. I have a subcontracts table, a service_lines
I have an asp.net mvc app, using JQuery UI dialog. I'm trying to submit
I have an asp.net MVC app that i am working on and I wrote
I'm using LINQ to SQL for a .NET MVC app. In my db i
In my Asp.net MVC app, I have a custom validator class V and an

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.