RSF
RSF

Reputation: 598

Windsor Castle : Property Inject

I implemented a CustomMembershipProvider which is deriving from ExtendedMembershipProvider. IUserService is a dependency of CustomMembershipProvider which will be used to validate the given credentials. This will be configured in Web.Config;

 <membership defaultProvider="DefaultMembershipProvider">
  <providers>
    <add name="DefaultMembershipProvider" type="BorderExpress.AutoImport.Web.Security.CustomMembershipProvider"  connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
  </providers>
  </membership>

Injecting IUserService implementation via constructor not possible as CustomMembershipProvider require a parameterless constructor.

So thought of introducing Property injection. I made IUserService as a public property of CustomMembershipProvider.

 public class CustomMembershipProvider : ExtendedMembershipProvider
 {

    public IUserService UserService { get; set; }

    public CustomMembershipProvider()
    {
    }
    ...

    public override bool ValidateUser(string username, string password)
    {
        var user = UserService.GetUser(username);

        if (user != null && SaltedHash.Verify(user.Salt, user.Hash, password))
        {
            return true;
        }

        return false;            
    }
 }

I wrote a separate installer only for this registration

public class WindsorMembershipInstaller:IWindsorInstaller

{
    public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
    {

        container.Register(
           Component.For<CustomMembershipProvider>()
           );

    }
}

That didn't work and UserService always NULL at the time of calling the ValidateUser.

And I tried contributors;

public class RequireUserServiceProperties : IContributeComponentModelConstruction
{
    public void ProcessModel(Castle.MicroKernel.IKernel kernel, Castle.Core.ComponentModel model)
    {
        model.Properties
            .Where(p => p.Dependency.TargetItemType == typeof(IUserService))
            .All(p => p.Dependency.IsOptional = false);


    }
}

And register the contributor where I bootstrap the container.

_container = new WindsorContainer()
        .Install(FromAssembly.This());

        _container.Kernel.ComponentModelBuilder.AddContributor(new RequireUserServiceProperties());

Please do let me know how to inject the IUserService property of CustomMembershipProvider

Upvotes: 0

Views: 143

Answers (1)

RSF
RSF

Reputation: 598

I found an answer within the SO. @Mauricio Scheffer has done custom implementation to utilize windsor to inject dependencies to membership provider.

Original question How do I control MembershipProvider instance creation/lifetime?

@Mauricio Scheffer 's blog post

http://bugsquash.blogspot.com/2010/11/windsor-managed-membershipproviders.html

Upvotes: 1

Related Questions