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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:52:41+00:00 2026-06-06T13:52:41+00:00

I have an MVC app with custom membership and role providers. I am checking

  • 0

I have an MVC app with custom membership and role providers. I am checking whether a user is in a role like this:

public static bool CustomersVisible
{
    get
    {
        return
            HttpContext.Current.User != null &&
            HttpContext.Current.User.Identity.IsAuthenticated &&
            HttpContext.Current.User.IsInRole("Admin");
    }
}

The custom Role provider looks like this:

public class DRRoleProvider : RoleProvider
{
    private IUserProfileRepository _userProfileRepository;
    private IRoleRepository _roleRepository;

    public DRRoleProvider() {}

    public DRRoleProvider(IUserProfileRepository userProfileRepository, IRoleRepository roleRepository)
    {
        _userProfileRepository = userProfileRepository;
        _roleRepository = roleRepository;
    }

. . .

public override bool IsUserInRole(string username, string roleName)
{
    var user = _userProfileRepository.GetByUserName(username);
    var role = _roleRepository.GetByName(roleName);

    if (user != null)
    {
        return user.Roles != null && user.Roles.Contains(role);
    }

    return false;
}

Edited to add: Here is my NinjectControllerFactory:

public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel ninjectKernel;

    public NinjectControllerFactory()
    {
        ninjectKernel = new StandardKernel();
        AddBindings();
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        return controllerType == null
                   ? null
                   : (IController) ninjectKernel.Get(controllerType);
    }

    private void AddBindings()
    {
        ninjectKernel.Bind<ICustomerRepository>().To<CustomerRepository>();
        ninjectKernel.Bind<IUserProfileRepository>().To<UserProfileRepository>();
        ninjectKernel.Bind<IRoleRepository>().To<RoleRepository>();
        ninjectKernel.Bind<ISessionFactory>().ToProvider<SessionFactoryBuilder>().InSingletonScope();
        ninjectKernel.Bind<ISession>().ToMethod(CreateSession).InSingletonScope();

        ninjectKernel.Inject(Membership.Provider);
        ninjectKernel.Inject(Roles.Provider);
    }

    private ISession CreateSession(IContext context)
    {
        var session = context.Kernel.Get<ISessionFactory>().OpenSession();
        return session;
    }
}

When the IsUserInRole method is called, I get a null reference exception on the _userProfileRepository. How can I make sure the repositories get injected when called from HttpContext.Current.User.IsInRole? It works fine when called from my own code.

  • 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-06T13:52:43+00:00Added an answer on June 6, 2026 at 1:52 pm

    RoleProvider is not really dependency-injection-friendly. It requires that you have default constructor. Try using property injection instead.

    Since you are using Ninject, maybe these questions / answers could help:
    Ninject with MembershipProvider | RoleProvider
    Custom Role Provider using DI ninject throwing error

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

Sidebar

Related Questions

I am developing an ASP.NET MVC app with custom membership and role providers. My
I have this markup in an MVC app. <div id=ingredientlistdiv> <% Recipe recipe =
I have custom membership and role provider (C#) in my .net 4 (webform based)
I have an MVC 3 app I am trying to create some custom authentication
Lets say I have an ASP.Net MVC App and this app (UI) references a
I have an MVC app and I wrote a custom roleprovider for it as
I need some help implementing a custom role provider in a asp.net mvc app.
I have a Spring MVC web application with conroller like below : @Controller public
I have a custom MVC app, where in it used IndexController naming convention, which
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.