Using the Microsoft Access Providers for Membership, Roles and Profile under ASP.NET 4

Back in August 2006 I wrote an article about replacing the built-in Application Services providers (such as the Membership and Roles providers) with a new provider targeting a Microsoft Access database, instead of a SQL Server database. This solution has worked well for many people who didn't have access to SQL Server on their production machine, or couldn't (or didn't want to) spend a lot of money for an extended hosting package that includes SQL Server.

I wrote the article using Visual Studio 2005 and the Microsoft .NET Framework 2.0 which was the bomb at that time. And while you can continue to use the concepts explained in the article in web sites that target ASP.NET 4 today without any modifications (you can easily consume .NET 2.0 DLLs in an ASP.NET 4 web site), you may run into some issues when you also want to compile the Access Providers assembly against .NET 4.

In this article I'll show you how to make the Access Providers work under an ASP.NET 4.0 web site.

To fix or not to fix? That's the question

If you want to use the Access Providers assembly in an ASP.NET 4 web site, you have at least two options to do it:

  • Don't do or fix anything. Simply compile the Access Providers against the .NET Framework 2.0 (using Visual Studio 2005 or later) and use the resulting DLL in your ASP.NET 4 web site. This is probably the easiest solution to the problem (it also means that if you're upgrading a web site that uses the Access Providers, you don't have to do anything at all. The Access Providers DLL remains in .NET 2, but can successfully be used in an ASP.NET 4 web site).

  • Recompile the Access Providers code against the .NET 4 framework. In order for this to work you need to make some changes to the project.

In the remainder of this article, I'll show you how to make the Access Providers work with an ASP.NET 4 web site.

Keep using the .NET 2 version of the DLL

With this solution, you either reuse an existing .NET 2 version of the Access Providers assembly, or you create a new one targeting this version of the framework. To create a new version of the assembly, you need a copy of Visual Studio 2005, 2008 or 2010 (the Express editions of 2008 and 2010 would work equally well as they enable you to compile class libraries). Then follow the steps explained in the section "Opening, Compiling and Tweaking the Project" of the original article. This gives you a C# Class Library which outputs an assembly (a DLL file) called SampleAccessProviders.dll. To make sure the assembly targets .NET 2.0, right click the Class Library project in the Solution Explorer, choose Properties and switch to the Application category. In the Target Framework drop down, make sure that .NET Framework 2.0 is selected.

You can now follow the remaining steps from the original article and create a new web site targeting ASP.NET 4. The web site can then reference the .NET 2.0 version of the Access Providers assembly and everything continues to work as described in the article. You may want to continue reading this article, as it contains a few tips about configuring your ASP.NET 4 web site with the Access Providers assembly.

Upgrading the Access Providers Assembly to .NET 4

The alternative solution is to upgrade the Class Library project with the Access Providers to .NET 4 as well. If you do this, you'll run into a minor issue. With ASP.NET 4, some types that were previously located in the System.Web assembly have now been moved to their own System.Web.ApplicationServices assembly. This means that if you try to compile the sample code from the Access Providers targeting ASP.NET 4, you'll run into errors like the following:

Error 6 The type name 'MembershipCreateStatus' could not be found. This type has been 
forwarded to assembly 'System.Web.ApplicationServices, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35'. Consider adding a reference to that assembly. 

As the error message suggests, the problem can be solved by adding a reference to the System.Web.ApplicationServices assembly. I'll show you how to do this in the following walk through. Additionally, I'll show you how to modify, rather than completely overwrite the web site's web.config (as I showed you how to do in the previous article), to preserve some settings in that file specific to ASP.NET 4. Finally, since even the Visual Web Developer Express editions now support creating class libraries, I'll show you how to create a Visual Studio solution that contains the Access Providers project and your ASP.NET 4 web site so you can manage both from a single Visual Studio solution.

  • First, obtain and install a copy of the Microsoft ASP.NET 2.0 Access Providers template for Visual Studio as explained in the section "Obtaining and Installing the Microsoft ASP.NET 2.0 Access Providers" of the original article.
  • Next, start Visual Web Developer 2010 Express Edition (or a commercial version of Visual Studio 2010) and choose File | New Project. Switch to the Visual C# | Starter Kits category and double-click the ASP.NET Access Providers template to create a new Class Library project with the Access Providers sample code.
  • Right-click the project in the Solution Explorer and choose Properties. In the Application category, change the Assembly Name to SampleAccessProviders. For more details on why you need this, check out the original article.
  • In the same Properties category, make sure that Target Framework is set to .NET Framework 4. You should end up with the following screen:


    The Project Properties Dialog
    Figure 1: The Project Properties dialog

  • If you now try to compile the project, you get the error message mentioned earlier. To fix it, right-click the References node of the Visual Studio project in the Solution Explorer and choose Add Reference. On the .NET tab locate the assembly System.Web.ApplicationServices (you may need to sort the list by name first), select it and click OK.
  • Now you can successfully compile the project.

From here, you could follow the steps from the original article. However, one of the steps in that article tells you to overwrite the web.config file in your ASP.NET web site with the one from the Access Providers sample project. If you do that, you wipe out some important ASP.NET 4 settings in that file, reverting your site back to an earlier version of ASP.NET. So, instead of overwriting the web.config file, you need to modify it by following these steps:

  • First, make sure the Visual Studio Solution file is visible in the Solution Explorer. Choose Tools | Options, make sure that Show All Settings at the bottom of the Options dialog is checked and then make sure that in the Projects and Solutions category the option Always Show Solution is checked. Click OK to dismiss the Options dialog.
  • Next, right-click the Solution in the Solution Explorer (not the Access Provider project) and choose Add | New Web Site. Select the ASP.NET Web Site template (VB or C#; that doesn't matter) and click OK.
  • Right-click the newly created web site and choose Add Reference. On the Projects tab of the dialog that appeared, select the Access Providers project and click OK. If you want to create a file reference instead of a project reference (e.g. when you've already compiled the Access providers to an assembly earlier), you can browse to the DLL file using the Browse tab.
  • Click the file ASPNetDB.mdb in the Access Providers project and drag it into the App_Data folder of the web site project. When you're done, your Solution Explorer should look like this:


    The Solution Explorer with the two projects
    Figure 2: The Solution Explorer with the two projects

  • Open up the web.config file from the sample Access Providers project and copy the elements for membership, roleManager, profile, anonymousIdentification and webParts to the clipboard. Then open the web.config from the project and paste the copied code, overwriting the existing membership, roleManager and profile elements.
  • Copy the AccessFileName connection string from the sample project and paste it in the web.config of the web site as well.You should end up with something like this:
    <configuration>
      <connectionStrings>
        <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated 
            Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
             providerName="System.Data.SqlClient" />
        <add name="AccessFileName" connectionString="~/App_Data/ASPNetDB.mdb" 
             providerName="System.Data.OleDb"/>
      </connectionStrings>
    

    You can delete the ApplicationServices connection string from the config file as it's no longer needed. You can check out the download at the end of this article to see how the web.config file should end up.

  • To test out your provider, right-click Default.aspx in the Solution Explorer and choose View In Browser. At the top of the page click Login in, and the follow the Register link. Sign up for an account by entering your details:

    Creating a new user account
    Figure 3: Creating a new user account

  • If everything worked correctly, the account is created and you get a confirmation message. You can confirm this by opening the Access database from the App_Data folder in Microsoft Access. You should see records in at least the aspnet_Users and aspnet_Membership tables.

For trouble shooting tips and more background, be sure to read the original article discussing the Microsoft Access Providers.

Summary

You could easily reuse an existing assembly that was compiled against .NET 2.0 in an ASP.NET 4 web site. If you previously used the Access Providers, you can simply keep using the existing DLL. If, however, you want your entire code base upgraded to .NET 4, you need to make a change in the references of the class library project of the Access Providers. Starting with .NET 4, some of the types used by the Application Services have been moved to their own assembly file which you now need to reference. With the reference in place, you can compile the code into a .NET assembly, and continue to use the Access Providers as you previously did.

Downloads

The complete source for the Access Providers and the ASP.NET 4 sample web site.


Where to Next?

Wonder where to go next? You can read existing comments below or you can post a comment yourself on this article .


Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.



Feedback by Other Visitors of Imar.Spaanjaars.Com

On Wednesday, January 12, 2011 4:23:24 PM Don said:
Thank you for keeping this up to date!  After Microsoft yanked support for generating the membership db as an Access database in Visual Studio 2005 beta 2, your article in 2006 made a huge difference in enabling me to create sites in medium trust situations that didn't support SQLExpress and didn't call for SQLServer.  Any chance you could do a post on implementing a similar approach using SQLite as the membership database.  Thanks!
On Thursday, January 13, 2011 8:15:58 AM Imar Spaanjaars said:
Hi Don,

Are you familiar with this article: http://www.eggheadcafe.com/articles/20051119.asp It seems to include a complete provider for SqlLite. Haven't tested it though....

Imar
On Thursday, January 13, 2011 8:26:06 PM Don said:
Hi Imar,

Yes, I've looked into that article as well as this one from codeproject http://www.codeproject.com/KB/aspnet/SQLite-Providers.aspx, however, I guess it is a trust factor.  I trust your solutions/approach.  Thanks.
On Saturday, January 15, 2011 5:23:37 PM Imar Spaanjaars said:
Hi Don,

Since I haven't done anything with SqlLite, I think you're better off trusting Peter Bromberg.... ;-)

Cheers,

Imar
On Monday, January 17, 2011 3:20:10 PM Don said:
Hi Imar,

Ok thanks.  Will do.

Cheers,
Don
On Friday, August 31, 2012 6:00:24 PM Lionel Thomas said:
Hi,
Great work! Have used the original version for a long time. But now when I convert the Access database to 7.0, accdb format I get an invalid filename. What am I doing wrong?
Best wishes,
Lionel Thomas
On Saturday, September 01, 2012 10:29:49 AM Imar Spaanjaars said:
Hi Lionel,

I think you need to use a different connection string for this database type. Check this site: http://www.connectionstrings.com/access-2007 for samples and details.

Cheers,

Imar
On Tuesday, November 13, 2012 8:22:23 AM Peshang M.Hussien said:
please can you post article >>  

Using the Microsoft Access Providers for Membership, Roles and Profile under ASP.NET 4.5 withe RegisterExternalLogin

thanks.
On Thursday, November 15, 2012 5:21:40 PM Jaume Pujol-Busquets said:
Thanks for these notes! Are very clarifying! Now, it turns out that the SampleAccessProvider is not available anymore. I have looked everywhere. Do you happen to know where is it now a copy?

Thanks indeed!!

Jaume Pujol-Busquets
On Friday, November 16, 2012 9:32:23 AM Imar Spaanjaars said:
Hi Jaume,

The download link still works for me. Look at the original article (http://imar.spaanjaars.com/404/using-the-microsoft-access-providers-to-replace-the-built-in-sql-server-providers) under the heading "Obtaining and Installing the Microsoft ASP.NET 2.0 Access Providers" for a download link.

Cheers,

Imar
On Tuesday, February 18, 2014 1:40:18 PM Ron Crom said:
I'm trying to use this with mvc4, .net 4 and I get this error: To call this method, the “Membership.Provider” property must be an instance of “ExtendedMembershipProvider”

on this line  if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
                return RedirectToLocal(returnUrl);
            }

any thoughts? thanks
On Wednesday, February 19, 2014 8:42:31 AM Imar Spaanjaars said:
Hi Ron,

For some reason, the MVC code has been prgrammed to only work with the ExtendedMembershipProvider or classes that derive from it. You'll need to rewrite the authentication code in the Account controller to make this work with the Access provider.

Cheers,

Imar
On Monday, March 30, 2015 2:33:36 PM Tamio Mhango said:
Hi,
Please help ; I'm getting the error 'Exception Details: System.ArgumentException: Illegal characters in path' when I try to use Access 2007 as my Membership Provide from an ASP.NET Website Application. Everything works fine in an ASP.NET project on the local server, but because I need to access it from the Internet I created a website application but I get that error on attempting to register a user, or log in. I'm using Visual Studio 2012. Your help would be much appreciated.
On Tuesday, March 31, 2015 7:00:26 AM Imar Spaanjaars said:
Hi Tamio,

Make sure the path of your site and database is valid indeed. Also, make sure your serve is able to access the database. If you're running on a 64-Bit platform, you may need to install additional Access drivers or run your site in 32-Bit mode.

Cheers,

Imar
On Tuesday, March 31, 2015 10:33:30 AM Tamio Mhango said:
Greetings Imar,
Thanks for your response.
The path for my site is C:\inetpub\wwwroot\wcfform6. The database is in the AppData folder of the site. I am able to see the database's tables and queries from Data Connections in Server Explorer; I am also able to use ASP.NET Configuration and see the accounts and roles available. However, I cannot log in from the default Login.aspx page.
Also, please advise how I can run my site in 32 bit mode - I am using a 64 bit machine.
The detailed error message is as below


Illegal characters in path.
  at System.IO.Path.IsPathRooted(String path)
  at Samples.AccessProviders.AccessConnectionHelper.BuildConnectionForFileName(String dbFileName)
  at Samples.AccessProviders.AccessConnectionHelper.GetConnection(String dbFileName, Boolean revertImpersonation)
Unhandled Execution Error  at Samples.AccessProviders.AccessMembershipProvider.ValidateUser(String username, String password)
  at System.Web.UI.WebControls.Login.AuthenticateUsingMembershipProvider(AuthenticateEventArgs e)
  at System.Web.UI.WebControls.Login.AttemptLogin()
  at System.Web.UI.WebControls.Login.OnBubbleEvent(Object source, EventArgs e)
  at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
  at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
On Tuesday, March 31, 2015 10:38:00 AM Imar Spaanjaars said:
Check out this link for 32-Bit mode: http://www.depotsystems.com/ds_mobile_webhelp/webhelp/index.htm#page=Enabling_32_bit_mode.htm

Not sure what else to recommend as I don't know how your site is configured, how you specify the path and so on.

Cheers,

Imar
On Tuesday, March 31, 2015 6:42:54 PM Tamio Mhango said:

Wow !
That did it.
Many, grateful thanks.
Please keep up with the great help - if I run into more issues I'll definitely call back !
Cheers.
On Monday, April 27, 2015 4:52:24 PM Tamio Mhango said:
Greetings Imar,
Please help, once more. I'm using Visual Studio 2012. Everything works fine if I run my ASP.NET project from my local server. Because I'd want it accessed from the Internet, I converted it to a web application. Now I've noticed that all buttons and textboxes are not working - error message is 'TextBox1 is not declared. It may be inaccessible due to its protection level'. Please advise what could be causing this. I've tried creating a new textbox eg. TextBox2, but I get a similar error for the new textbox.
Thanks in advance.
On Monday, April 27, 2015 8:17:18 PM Imar Spaanjaars said:
Hi Tamio,

You don't have to change your web site to a web application in order to make it available online. Both should work just fine.

You can find a detailed walkthrough of the upgrade process here: https://msdn.microsoft.com/en-us/library/aa983476%28v=vs.100%29.aspx?f=255&MSPPError=-2147217396

Cheers,

Imar
On Monday, January 02, 2017 5:31:46 PM Andrew Yearsley said:
Hi,

I'm trying to use this to integrate with VSExpress 2013 but I've stumbled at the first hurdle because the download no longer appears to be available from Microsoft.  Does anyone know where I can get this from or is there a new version for VS2013 and above?

Thanks
Andrew
On Sunday, January 08, 2017 3:46:54 PM Imar Spaanjaars said:
Hi Andrew.

Take a look at my code that comes with the article. it contains most, if not all, of what you need.

Cheers,

Imar

Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.

For more information about the Talk Back feature, check out this news item.