When Written: April 2007
On the development front I have been working with the .NET2 membership services. By using these you can build the login pages, password reminder screens and all the usual user authentication code for a web site with the dragging of a few objects onto a web form in Visual Studio, this sort of code you would normally have to spend some time writing and quite frankly is very boring to do. But before any of this will work you have to set up a database for storing the user’s information. If you find that your development box comes up with errors when you try to use membership services, it is probably because the necessary tables are not set up in the web application’s database. Thankfully Microsoft provides a tool to do this for you, it’s called Aspnet_regsql.exe and the full description about it is on http://msdn2.microsoft.com/en-us/library/ms229862(vs.80).aspx .
ASPNet_regsql makes the setting up of membership services on your domain a breeze
Basically you download this tool, run it and it takes you through a wizard asking which database you wish to add the tables to and the login details for a user with permission to create such tables, and then you are done. All simple stuff but without this tool I was stuck for an hour or so, trying to figure out why things were not working. Having set up all the user login pages and being quite pleased at the result, the next thing that I needed to do was to read the current logged in user so that any entries they made into a database via a data grid on the web form could be marked as theirs. At first I thought this would be a simple task but again after a hour or so I found the answer: When using the Membership system, the currently logged on user’s information can be accessed using Membership.GetUser(), and the UserId value is returned in the MembershipUser object’s UserProviderKey property The code for this was as follows:
Dim currentUser As MembershipUser = Membership.GetUser()
myuser = currentUser.ProviderUserKey
I have often found it to be the case when working with these dev tools that some tasks are so simple that you storm ahead building code, then you want to do something that seems relatively simple and it takes hours! Ah the joys of programming.
Article by: Mark Newton
Published in: Mark Newton