Search code examples
asp.netauthenticationcustom-membershipprovider

Simple Asp.net Custom Membership Provider


In the past I've used the default SQL Membership provider to secure a website. This worked well but required the creation of the ASP_Authentication database (using the reg_sql tool) and dozens of tables / stored procs etc.

I now have a simple website on a £10 per month host. I get a SQL Server instance as part of the deal but don't have the ability to connect to it through SQL Management Studio. I can't run the reg_sql tool or even take a script from a previously generated database and run that.

Actually, the ASP_Authentication database is overkill for what I need. I just was a simple username / login store that I can authenticate against. I have no need for group / roles etc.

Does anyone know of a good blog article or similar that describes how to do this?

Many thanks

Rob.


Solution

  • I would advise you to stick with the sql membership if you just want a simple login mechanism. You dont have to use the roles or any additional features such as secret question/answer.

    Via The Commandline

    You can use the aspnet_regsql tool locally to just generate sql scripts. You must have access to some kind of tool for managing the database so perhaps this will be enough for you?

    I have written an article on using this tool via the commandline. I also address the issue of only adding the tables you actually want to use (no need for the personalization, web parts, etc tables).

    The article is on my blog here:

    It doesn't cover how you can dump the sql scripts out to a file but it does give you a hint (it says you can use -? to view all commandline arguments). Open up a command window (start | run | cmd | enter) and type:

    asp.net v2:

    cd C:\Windows\Microsoft.NET\Framework\v2.0.50727\
    aspnet_regsql.exe -sqlexportonly C:\aspnetmembership.sql -A mrp
    

    asp.net v4:

    cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\
    aspnet_regsql.exe -sqlexportonly C:\aspnetmembership.sql -A mrp
    

    If the v4 doesn't work then look in the next one up and use the latest v4 folder in there.

    You will then find a file called aspnetmembership.sql in the root of your C: drive which you can use with whatever database management they provide.

    Via Code

    There is another option to getting the tables set up which is generally less well known by the community; you can actually do it through a method in the System.Web.Management namespace. I learned this technique in a post by Peter Bromberg and keep it tucked away. Its a simple one liner:

    Management.SqlServices.Install("server", "USERNAME", "PASSWORD", "databasename", SqlFeatures.All)
    

    You can read the rest of his advice here.

    Custom Membership Provider

    To actually answer your question though, there are tons of articles on the web which explain how to create custom membership providers:

    If you do decided to "roll your own" then please stick to the plan and use the membership provider framework rather than writing your own from the ground up.