find a location for property in a new city

Thursday, 18 November 2010

Static Membership helper class causing problems with MembershipProvider

I was having a problem where after a user changes their password using my custom MembershipProvider it was being updated in the database but didn't seem to be propagating to my application. So if they tried to log in they would be expected to provide their old password even though there was a new password in the database.

It is almost as if Membership didn't particularly care what has changed in the database and it kept on just doing its own thing.

Almost as if Membership is a static class... oh right it is

I thought the Membership was acting as if it was a singleton or something and never going through its constructor which sets a new UserService() to the UserService property (the service which ultimately gets the users' data). Maybe it is something to do with the fact that MEMBERSHIP IS A STATIC CLASS! Of course.

Membership was using my MembershipProvider but only ever instantiating it once due to its static nature so the UserService was keeping the old version even if the data had been edited. My fix was to make sure that the MembershipService property returned a new MembershipService() each time it is called.

So for context I changed this code in MyMembershipProvider:
public MyMembershipProvider()
{
    UserService = new UserService();
}

public UserService UserService { get; set; }

To this:
public MyMembershipProvider() {}

public UserService UserService
{
    get { return new UserService(); }
}

Follow britishdev on Twitter

1 comment: