Using the ASP.Net membership provider in a Windows forms application part 1.
One of the new features in ASP.Net 2.0 is the membership and role
provider system. This is nicely designed system that allows the developer to
handle the storage of user information pretty much any way he likes, all he has
to do is create the appropriate providers and configure his application to use
these. While this is very powerful there is something else I was much more
interested in and that is the fact that user and role management can be added
to an application without having to do any of the implementation work. Now this
is very convenient if you don’t already have a user database to work with
and something I would like to be able to use in my Windows forms applications
as well as in my web application.
Fortunately this is possible without much work at all! So let’s
create a very small console application with user management.
First create a new Visual Basic console application in Visual Studio
2005. No problem if you prefer C#, all you need to do is translate the
remaining syntax to C#.
Add a reference to the System.Web namespace.
Add a line with “Imports System.Web.Security”
to the top of the Module1.vb.
Now add the following code to the Sub Main()
' Validate a username/password
If Membership.ValidateUser("Maurice", "Password_1") Then
Go ahead and run the application.
Not surprisingly the application reports that the user is invalid,
after all how it can’t know which users are valid.
Now add the following code to the top of the Sub Main()
' Creating a new user
Dim status As MembershipCreateStatus
Go ahead and run the application again.
If you have never used the membership provider in a Web application you
might me surprised to see this just works, the user is created and is
validated. Run the application again and the creation will return a
DuplicateUserName error but the ValidateUser() still works. How is this
possible as we haven’t done anything to configure the membership provider
or told it which database to use? Well the secret is that the membership provider
checks for, and creates if needed, a new directory under the application
directory named App_Data with a SQL Express database ASPNETDB with the required
structure. This database is then used to store the user information.
Maurice de Beijer