Integrating MVC Forum with existing solution!

It has always been my plan to make sure MVC Forum could easily be integrated with existing solutions. One of the main tasks for an easy integration would be to let MVC Forum work with any ASP.NET membership provider. This does seem like a bit of a task, as the membership provider framework doesn't provider any event hooks, and MVC Forum needs to know when an user is created etc. So how should this be handled, without forcing anybody to do a lot of custom code to their existing solution (which might be closed source software etc.)?

My idea to solving this problem is to write a custom membership provider that could wrap any existing provider. All this wrapper should do, is call the actual membership provider and when needed, tell MVC Forum about the changes (new user created, existing user deleted, user updated etc.).

So let me give you an example of what to expect when we release MVC Forum next time. Your web.config file could look a bit like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<system.web>
		<membership defaultProvider="SqlProvider">
			<providers>
				<clear />
				<add 
				  name="SqlProvider" 
				  type="System.Web.Security.SqlMembershipProvider" 
				  connectionStringName="SqlServices"
				  applicationName="MyApplication"
				  ..... />
			</providers>
		</membership>
	</system.web>
</configuration>

And you really don't want to use another membership provider, but you really want to add MVC Forum to your web site. So with the next release of MVC Forum, your web.config file will look like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<system.web>
		<membership defaultProvider="ProviderWrapper">
			<providers>
				<clear />
				<add name="ProviderWrapper" type="mvcForum.Web.Providers.MembershipProviderWrapper"
				  WrappedProvider="SqlProvider" />
				<add 
				  name="SqlProvider" 
				  type="System.Web.Security.SqlMembershipProvider" 
				  connectionStringName="SqlServices"
				  applicationName="MyApplication"
				  ..... />
			</providers>
		</membership>
	</system.web>
</configuration>

Can you spot the magic? Probably not, because there's no magic! Whenever any methods are called on the default membership provider (the ProviderWrapper) the calls will be redirected to the wrapped provider (the SqlProvider). Let's look at some code:

public class MembershipProviderWrapper : MembershipProvider {
	private string wrappedProvider;

	public override void Initialize(string name, NameValueCollection config) {
		base.Initialize(name, config);
		if (String.IsNullOrWhiteSpace(config["WrappedProvider"]))
			throw new ArgumentNullException("WrappedProvider");
		this.wrappedProvider = config["WrappedProvider"];
	}
	protected MembershipProvider ActualProvider {
			get { return Membership.Providers[wrappedProvider]; }
	}
	public override Boolean ValidateUser(string username, string password) {
		// Let's call the wrapped provider!
		bool loggedOn = this.ActualProvider.ValidateUser(username, password);
		// Did we succeed?
		if (loggedOn) {
			// Let's get the membership user!
			MembershipUser user = this.GetUser(username, false);
			// And the matching forum user.
			ForumUser u = this.UserRepository.ReadOne(new ForumUserSpecifications.SpecificProviderUserKey(user.ProviderUserKey.ToString()));
			// Let's update the last known IP
			u.LastIP = HttpContext.Current.Request.UserHostAddress;
			// And the last visit datestamp.
			u.LastVisit = DateTime.UtcNow;
			// Commit the changes!
			this.UnitOfWork.Commit();
		}

		return loggedOn;
	}
}

We could probably live without the last known IP address and the last visit timestamp, but it would take a lot of (ugly) work-arounds to not know when a new user was created, or when an user is deleted etc.

If you disagree with the way we've solved this issue and you have all the right reasons, you can still make me change my mind, just give me some feedback!

Categories: MVC Forum - Alpha 4 - Membership Provider

Posted by Steen F. Tøttrup on 06 December 2011 07:38. There are 2 comments.

Comments

  1. 20 December 2011 07:42 by UcatastaCA:

    Hi, I did not know where to post my question. It is more of a technical question regarding the boardsoftware, so sorry if "http://newsuntold.dk/Blog/integrating-mvc-forum-with-existing-solution" is the false area. My name is Pedro and I am 18 years old. I've been reading on inscalemodelling.com for some days and now I made the decision to participate. ^^ Let's stop the smalltalk ... the question is if there is a chance to get brand-new articles via RSS feed? I love reading new posts of my favorite blogs with RSS Bandit so it would be nice to also be able to check what's happening on inscalemodelling.com in my RSS-reader. It would be awesome if you could send me the link to the feed. Thank you! Good-bye

  2. 20 December 2011 10:20 by Steen F. Tøttrup:

    Hello Pedro, the inscalemodelling site does have a RSS feed, and you can reach it on: http://inscalemodelling.com/news/rss It isn't really a secret, I just haven't put a lot of time into the project lately, so I have a bunch of new feature I just haven't announced yet! Hopefully I'll get around to doing more exciting things with the site in 2012.

Write new comment