ASP.NET MVC 3 Ninject Custom Membership and Role Provider
Asked Answered
F

2

7

Hi i got some problems enabling custom Membership and Role Provider, i got following error:

An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

System.InvalidOperationException: Default Membership Provider could not be found.

It's pointing to my mvc3.ninject kernel.Inject(Membership.Provider)

My MVC3.Ninject in app_start:

private static void RegisterServices(IKernel kernel)
{
    kernel.Inject(Membership.Provider);
    kernel.Inject(Roles.Provider);
    kernel.Bind<IUnitOfWork>().To<EFContext>().InRequestScope();
    kernel.Bind<IUzytkownicyRepository>().To<UzytkownicyRepository>().InRequestScope();            
}  

My RoleProvider:

public class DziennikRoleProvider : RoleProvider
{
    [Inject]
    private IUzytkownicyRepository repository { get; set;}

My MembershipProvider:

public class DziennikMembershipProvider : MembershipProvider
{
    [Inject]
    private IUzytkownicyRepository repository { get; set; }

My WebConfig file:

 <membership defaultProvider="DziennikMembershipProvider">
  <providers>
    <clear/>
    <add name="DziennikMembershipProvider" type="Dziennik_MVC.Helpers.DziennikMembershipProvider, Dziennik_MVC" connectionStringName="EFDbContext"
         applicationName="/" />
  </providers>
</membership>

<profile>
  <providers>
    <clear/>
  </providers>
</profile>

<roleManager defaultProvider="DziennikRoleProvider" enabled="true" cacheRolesInCookie="true">
  <providers>
    <clear />
    <add name="DziennikRoleProvider" type="Dziennik_MVC.Helpers.DziennikMembershipProvider, Dziennik_MVC" connectionStringName="EFDbContext" />
  </providers>
</roleManager>
Feverwort answered 11/2, 2012 at 15:50 Comment(4)
#8249045 #6520220 #8409609Arthralgia
#2964111 ... and many moreArthralgia
yeah, i saw all of them, but still mine application cant startFeverwort
I was helped by this version of the question: #14412779Keener
Q
8

You have to setup the bindings before you inject the Providers.

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IUnitOfWork>().To<EFContext>().InRequestScope();
    kernel.Bind<IUzytkownicyRepository>().To<UzytkownicyRepository>().InRequestScope();

    kernel.Inject(Membership.Provider);
    kernel.Inject(Roles.Provider);
}
Queensland answered 16/2, 2012 at 16:11 Comment(0)
C
1

I had the same problem. My solution was to change the repository method to be Public. Ninject cannot inject into a private property.

Coverlet answered 7/5, 2012 at 7:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.