Dependency Injection with Custom Membership Provider
Asked Answered
A

4

11

I have an ASP.NET MVC web application that implements a custom membership provider. The custom membership provider takes a UserRepository to its constructor that provides an interface between the membership provider and NHibernate. The UserRepository is provided by the Ninject IoC container.

Obviously, however, this doesn't work when the provider is instantiated by .NET: the parameterless constructor does not have a UserRepository and cannot create one (the UserRepository requires an NHibernate session be passed to its constructor), which then means that the provider cannot access its data store. How can I resolve my object dependency?

It's probably worth noting that this is an existing application that has been retrofitted with Ninject. Previously I used parameterless constructors that were able to create their required dependencies in conjunction with the parametered constructors to assist unit testing.

Any thoughts, or have I built myself into a corner here?

Abeabeam answered 2/5, 2010 at 12:19 Comment(0)
O
5

You might want to keep the parameterless constructor, that initializes the needed repositories using Ninject. You might want to use the Common Service Locator, so you won't need to have neither a reference to Ninject nor it's container inside your custom provider. It seems Ninject doesn't have an official an adapter implementation for CSL, but writing one shouldn't be to hard (check the other implementations, like Windsor's), and I'm sure there's an unofficial implementation somewhere.

Olympian answered 2/5, 2010 at 14:18 Comment(4)
Ninject has had an official CSL adapter January 2010.Gearing
It is still missing from the CSL homepage (it's included in the NInject release instead)Olympian
"constructor, that initializes the needed repositories using Ninject" - watch out, if your repositories (or their dependencies) are in Request or Transient scope and any kind of disposable data context comes into play, you will end up accessing a disposed data context on all but the first request (because you don't control a Provider's lifetime and ASP.NET will reuse it across requests). I ended up with getting a repository instance from the locator each time I needed to call one when I came across this problem.Wrong
Comment from Fabio70a: This is the solution : #1552003Clothespress
L
5

Correct answer from Michael B. from: Dependency injection and ASP.Net Membership Providers

You can obtain your repository (or service) injection in this way:

public IRepository Repository { get { return DependencyResolver.Current.GetService(IRepository ); } }
Leverhulme answered 12/12, 2012 at 12:5 Comment(0)
G
4

Since the membership collection and the MemberShip.Provider instance are created before Ninject can instantiate them, you need to perform post creation activation on the object. If you mark your dependencies with [Inject] for your properties in your provider class, you can call kernel.Inject(MemberShip.Provider) - this will assign all dependencies to your properties.

Gearing answered 8/11, 2010 at 13:46 Comment(2)
Sounds good; I solved it with the CSL, but it might be worth re-visiting to remove the extra dependency.Abeabeam
Where would you call kernel.Inject...?Twinkle
G
-1

I had same problem, I solved it by passing required data using authentication ticket to identity object.

Then no objects need to be injected into membership providers.

In my authentication code I have

    [NonAction]
    private void SetAuthTicket(Member member, bool isPersistent)
    {
        HttpCookie cookie = Request.Cookies.Get(FormsAuthentication.FormsCookieName);

        FormsAuthentication.SetAuthCookie(member.Email, isPersistent);

        string userData = "|" + member.ID + "|" + member.Firstname + "|" + member.Lastname + member.Culture;

        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, member.Email, DateTime.UtcNow, DateTime.UtcNow.AddDays(7), isPersistent, userData);

        string encryptedTicket = FormsAuthentication.Encrypt(ticket);
        cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
        Response.Cookies.Add(cookie);
    }

And in Global.asax

    void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
    {
        IPrincipal user = HttpContext.Current.User;
        if (user.Identity.IsAuthenticated && user.Identity.AuthenticationType == "Forms")
        {
            FormsIdentity identity = user.Identity as FormsIdentity;

            MyIdentity ai = new MyIdentity(identity.Ticket);            
            MyPrincipal p = new MyPrincipal(ai);

            HttpContext.Current.User = p;
            System.Threading.Thread.CurrentPrincipal = p;

            if (!String.IsNullOrEmpty(ai.Culture))
            {
                CultureInfo ci = new CultureInfo(ai.Culture);
                System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
                System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
            }
        }
    }

And in Identity class I have MemberName, Firstname and Lastname properties that return parts of ticket string.

Ginsburg answered 2/5, 2010 at 12:24 Comment(3)
Please could you go into a little more detail? How would, for example, the CreateUser() override function in your scenario? Are you saying that in my scenario I would have to pass the repository object around in the authentication ticket?Abeabeam
Instead of passing repository pass user idGinsburg
what?? I agree with @alastairs, how do you create users and other stuff without a repository?Thevenot

© 2022 - 2024 — McMap. All rights reserved.