Note that there are some explanatory texts on larger screens.

plurals
  1. PORepository Doesn't Get Injected from HttpContext.Current.User.IsInRole
    primarykey
    data
    text
    <p>I have an MVC app with custom membership and role providers. I am checking whether a user is in a role like this:</p> <pre><code>public static bool CustomersVisible { get { return HttpContext.Current.User != null &amp;&amp; HttpContext.Current.User.Identity.IsAuthenticated &amp;&amp; HttpContext.Current.User.IsInRole("Admin"); } } </code></pre> <p>The custom Role provider looks like this:</p> <pre><code>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 &amp;&amp; user.Roles.Contains(role); } return false; } </code></pre> <p>Edited to add: Here is my NinjectControllerFactory:</p> <pre><code>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&lt;ICustomerRepository&gt;().To&lt;CustomerRepository&gt;(); ninjectKernel.Bind&lt;IUserProfileRepository&gt;().To&lt;UserProfileRepository&gt;(); ninjectKernel.Bind&lt;IRoleRepository&gt;().To&lt;RoleRepository&gt;(); ninjectKernel.Bind&lt;ISessionFactory&gt;().ToProvider&lt;SessionFactoryBuilder&gt;().InSingletonScope(); ninjectKernel.Bind&lt;ISession&gt;().ToMethod(CreateSession).InSingletonScope(); ninjectKernel.Inject(Membership.Provider); ninjectKernel.Inject(Roles.Provider); } private ISession CreateSession(IContext context) { var session = context.Kernel.Get&lt;ISessionFactory&gt;().OpenSession(); return session; } } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload