Quantcast

ASP.NET 2.0: XML Membership Provider

Get the WebProNews Newsletter:


[ Business]

ASP.NET ships with a SqlMembershipProvider and a ActiveDirectoryMembershipProvider that makes user authentication and authorization very easy to implement, but for some reason those are the only membership providers provided natively by ASP.NET 2.0.

So, what do you do when you don’t want or can’t use SQL Server or Active Directory for memberships? You have to build a custom membership provider that suits your needs.

That’s exactly what I faced when I wanted an XML membership provider for a small web project. The only one I could find on the web was a very simple read-only xml provider from MSDN. I then changed it to be writable as well, so you can dynamically add new users among other things. I also encrypted the passwords so no one is able to make sense out of them when looking at the XML file.

It’s plug n’ play, it works and it makes user authentication ridiculously easy. Download the code at the bottom and dump the XmlMembershipProvider.cs class into the App_Code folder and the users.xml into the App_Data folder. Then write this in the web.config.

<membership defaultProvider="XmlMembershipProvider">

&nbs;p&nbs;p&nbs;p <providers>

&nbs;p&nbs;p&nbs;p <add name="XmlMembershipProvider" type="XmlMembershipProvider" description="XML membership provider" xmlFileName="~/App_Data/users.xml"/>

&nbs;p&nbs;p </providers>
</membership>

Now you have a membership provider that enables you to make use of the collection of built in authentication controls. You can also interact directly with the provider without using the built in controls. Here’s an example of how to create a new user:

MembershipCreateStatus status;
Membership.Provider.CreateUser("admin", "adminpw", "admin@domain.com", string.Empty, string.Empty, true, "admin", out status);

Even though you can access the provider programmatically, nothing beats the simplicity of the built in controls. To start using the XmlMembershipProvider, drag a CreateUserWizard to your webform and let the magic begin.

Download

XmlMembershipProvider.zip (3,45 KB)

Add to Del.icio.us | Digg | Reddit | Furl

Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

ASP.NET 2.0: XML Membership Provider
About Mads Kristensen
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/ WebProNews Writer


Top Rated White Papers and Resources
  • alexio

    Finally what I was looking for :D

    I can’t understand why microsoft developers don’t thing to the simplest thing first!! Thankssss!!!

  • alexio

    argh!! …the download link doesn’t work!!

    • Clarence

      Try: http://blog.madskristensen.dk/post/XML-membership-provider-for-ASPNET-20.aspx

    • webscott
      Also very interested – alas, your download link is erroring
       
      Try: http://blog.madskristensen.dk/post/XML-membership-provider-for-ASPNET-20.aspx
  • Guest

    The download link and secondary download link provided is dead. I was REALLY excited to find this article but the excitement soons evaporates when the beloved download link is dead.

  • Kanedogg

    Hey guys,

    i’m using the Xml membership provider with my asp2 .net site i have it working as to your description above thanks heaps.

    i am hopeless at XML and need some help with how i could go about the following.

    i want to get & set details from the xmlprofile of each user ?! how please.!

    Also want to add an admin page for users, get all users and add them to a grid Or dataview. Again dont know how.!

    Can someone please give me 1 or 2 examples of how to access & save each of the user & profile details of a user pls? as im confused with the 3 object of each kind eg.

    membership user

    membership provider

    membershipUser store

    "store" is that where i save to ?

    I have searched around heaps but cannot find anything. Sorry im new to all of this but am having a go.

    thanks heaps in advance

    Kanedogg

     

  • Guest

    Download link is broken….

  • http://www.programyou.net Goodmedalist

    It seems like the links on this page are broken because the page has moved to the address below:

    http://madskristensen.net/post/XML-membership-provider-for-ASPNET-20.aspx

    The download links worked for me from here.

  • GuestTom

    http://madskristensen.net/post/XML-membership-provider-for-ASPNET-20.aspx

  • argh!! too

    The link doesn’t work, exactly what I was looking for snifffff

  • Join for Access to Our Exclusive Web Tools
  • Sidebar Top
  • Sidebar Middle
  • Sign Up For The Free Newsletter
  • Sidebar Bottom