ASP.NET – Membership Hides Its Privileges

One of the problems I have had thus far with my foray into ASP.NET has been a simple concept.  How do you deal with users logging in.  Not so much the actual mechanics of it, but where is the data, and how can I get to it?  By default, when you are using forms based authentication, that data is stored in tables that you cannot easily get to, and certainly not in the Solutions Explorer.  Essentially, nothing is showing up in your “App_Data” node, and there is no .MDF file to add from your project directory.

What then are you to do when you want to have direct access to your membership tables from within Visual Studio?  Well, let me walk you through the steps, as I did them, to achieve this.  I won’t say that this is the official way, but it’s what worked for me.  I hope to save you some of the pain and suffering associated with my searching on the web to find these answers.

Create your new project

This is self-explanatory.

Turn on forms based authentication

This is accomplished by editing your web.config file.  Look for the following:

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" />
</authentication>
Run the ASP.NET Configuration

It’s important that you don’t look for this option (under the “Project” menu) when you have the web.config file as your open window.  It won’t be there.

Add A User

Click on the security tab, and from there you can manage your users.  Add one.  One time when I did this, I saw the following error:

The following message may help in diagnosing the problem: Could not find stored procedure ‘dbo.aspnet_CheckSchemaVersion’.

The annoying bit was that when I tested the connection, through the tool, it was able to successfully establish a connection.  Wonderful.  I am not sure how I fixed this the one time I hit it, but I wanted to throw it out there as a potential issue.

The Unfun Complicated Bit

So, this is where having a friend on the dev team is important.  I had read one of the tutorials over at asp.net which had content on this topic. They tell you to run “aspnet_regsql” from a Visual Studio command prompt.  If you do nothing else, know this: if you are using SQL Express, your server that you want to connect to is in the form of <machine name>\sqlexpress.  If you get that wrong, this tool will fail.  Always.  That’s roughly two hours I just saved you.

I am not sure what this tool did that this command line bit didn’t do for me (courtesy developer friend).

NAVIGATE TO:

C:\Windows\Microsoft.NET\Framework\v2.0.50727>

RUN:

aspnet_regsql.exe -d "<path to project dir>\<projectname>\App_Data\ASPNETDB.MDF"

With the following options:

-A all -C "Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True"

If everything goes according to plan, you will see:

Start adding the following features:
Membership
Profile
RoleManager
Personalization
SqlWebEventProvider
…..
Finished.

If not, you will see many errors. This broke for me when the ASPNETDB.MBF file was not already there.  It showed up for me only after I had successfully built and run the project and added a user.  The asp.net mvc default app has a new user wizard, which I could also use (which I did when I hit that stored proc error above in “Add a User.”  Of course it worked, and I don’t know why.

Add the Database File

In the Solutions Explorer Window, right mouse click, and select “Add/Existing Item.”  Select ASPNETDB.MDF.  Careful that you don’t select the _log file.  I did that.  Don’t do what I did.  Fail.

There you go.  A step by step, “how the hell do I get my user tables into my project so that I can edit them” tutorial.  Hopefully this saves some poor unsuspecting n00b some time.