How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

I’ve seen a few questions in the forums lately looking for examples on how to use the CreateUserWizard control to create new users in the ASP.NET 2.0 membership system, and then as part of the registration process assign the new user into custom roles and/or collect and store custom profile properties about them (country, address, gender, zipcode, etc).

 

Stefan from my team had a cool CreateUserWizard control sample that I borrowed and expanded upon to build-up a sample that demonstrates how to build a fairly common user management, roles and personalization system in ASP.NET 2.0 that does this.  I was pleasantly surprised to find it only took about 25 lines of C# code in the entire app. J

 

The sample comes with 6 pages:

 

 

Specifically it supports:

 

1) Login support to enable registered users to login to the web app using forms auth and the new membership system (login.aspx)

2) Registration support to enable visitors to create and register new users in the membership system (CreateNewWizard.aspx)

3) Profile support that enables the site to gather information about the user on registration, and for the users to see that information on a profile page (MyProfile.aspx). 

4) Change Password support to enable registered users to change their password in the membership system (ChangePassword.aspx)

5) Password Recovery support to enable users to reset their password if they forgot them (RecoverPassword.aspx)

 

You can download and run the sample yourself from here.  Note that it is built with the final release of VS 2005 and ASP.NET 2.0 – so it won’t work with Beta2 (although it will probably work with the RC).

 

Implementation Notes on CreateNewWizard.aspx and MyProfile.aspx:

 

Only two of the above pages (CreateNewWizard.aspx and MyProfile.aspx) have code in them.  The others use the built-in Login controls in V2 to-do everything (asp:login, asp:passwordrecovery, asp:changepassword). 

 

CreateNewWizard.aspx is the most interesting page.  It uses the built-in <asp:createuserwizard> server control to-do most of the heavy lifting and has two templated wizard steps defined within the wizard:

 

<asp:createuserwizard> wizard step 1: gathering user-account data

 

 

The <asp:createuserwizard> control handles gathering up the user-account, email, password, and password recovery/answer data and then calling into the ASP.NET 2.0 membership system to register the new user.  You simply have to override the control’s <createuserwizardstep> template and customize the control layout to have things look how you want. 

 

The sample is using the ASP.NET validation controls to perform client-side validation on the inputs as well within the template (example: making sure passwords match, the age is a valid integer, etc).  One added benefit in ASP.NET 2.0 is that these validation controls now support client-side validation for FireFox and other modern browsers (note: all screenshots were done using FireFox).

 

There are then three additional properties (their country, gender and age) that I wanted to gather up about the new user as part of the registration process.  Doing this was pretty easy using the new ASP.NET 2.0 Profile system – simply add their definitions within the <profile> tag of the web.config file to register them and store their values in the new profile system:

 

<profile enabled="true">

    <properties>

        <add name="Country" type="string"/>

        <add name="Gender" type="string"/>

        <add name="Age" type="Int32"/>

    </properties>

</profile>

 

I then handled the “CreatedUser” event on the CreateUserWizard control within my CreateNewWizard.aspx.cs code-behind file to retrieve the values from the controls within the CreateUserWizard control template and set them in the profile store:

 

// CreatedUser event is called when a new user is successfully created

public void CreateUserWizard1_CreatedUser(object sender, EventArgs e) {

 

   // Create an empty Profile for the newly created user

   ProfileCommon p = (ProfileCommon) ProfileCommon.Create(CreateUserWizard1.UserName, true);

 

   // Populate some Profile properties off of the create user wizard

   p.Country = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Country")).SelectedValue;

   p.Gender = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Gender")).SelectedValue;

   p.Age = Int32.Parse(((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Age")).Text);

 

   // Save profile - must be done since we explicitly created it

   p.Save();

}

 

Because the user is being created as part of this step, I explicitly choose to create a new Profile object in code (note that I was passing in the CreatedUserWizard1.UserName property as the username – since the user isn’t logged into the system yet).  I then accessed the controls within the template of the <asp:createuserwizard> control, pulled out their values, and stuck them within the newly created profile.  Calling p.save at the end registered this profile with the new username.  (note: I’ll walk through how we use this profile data later in a page below).

 

<asp:createuserwizard> wizard step 2: picking roles

 

After the user fills out the first step of registration information and clicks the next button, the user is created, the CreatedUser event fires, and we fill in the appropriate information about the user into the profile store. 

 

The <asp:createuserwizard> control then displays the second step template we’ve defined.  This template lists all of the roles currently created in the ASP.NET Role Management system, and allows the user to select which roles they belong to:

 

 

Note that you’d typically never assign roles to an end-user this way (instead you’d add your own logic to somehow calculate which role they belong in), but I thought this scenario was cool nonetheless which is why it works like the sample above.

 

I think it is cool because of the way we populate and access these roles in the template of the <asp:createuserwizard>.  Basically, we have a template definition to the .aspx like this:

 

<asp:WizardStep runat="server" AllowReturn="False"

                OnActivate="AssignUserToRoles_Activate"             

                OnDeactivate="AssignUserToRoles_Deactivate">

    <table>

        <tr>

           <td>

              Select one or more roles for the user:

           </td>

        </tr>

        <tr>

            <td>

               <asp:ListBox ID="AvailableRoles" runat="server"

                            SelectionMode="Multiple" >

               </asp:ListBox>

            </td>

        </tr>

    </table>

</asp:WizardStep>

 

It simply contains a <asp:listbox> control named “AvailableRoles”.  When this wizard step is loaded (after the user hits the next button on the first step in the wizard), it will fire the “OnActivate” event.  We can use this to databind the list of all roles in the system to the above listbox.  When the user hits next again, the wizard will fire the “OnDeactivate” event.  We can then use this to determine which roles were selected in the above listbox, and use them to update the role-manager system.

 

The code to-do both of these actions looks like this:

 

// Activate event fires when user hits "next" in the CreateUserWizard

public void AssignUserToRoles_Activate(object sender, EventArgs e) {

 

    // Databind list of roles in the role manager system to listbox

    AvailableRoles.DataSource = Roles.GetAllRoles(); ;

    AvailableRoles.DataBind();

}

 

// Deactivate event fires when user hits "next" in the CreateUserWizard

public void AssignUserToRoles_Deactivate(object sender, EventArgs e) {

 

    // Add user to all selected roles from the roles listbox

    for (int i = 0; i < AvailableRoles.Items.Count; i++) {

        if (AvailableRoles.Items[i].Selected == true)

           Roles.AddUserToRole(CreateUserWizard1.UserName, AvailableRoles.Items[i].Value);

    }

}

 

That is all of the code for the CreateNewWizard.aspx.cs file – 17 lines total if you omit comments and whitespace (if my count is right). 

 

Next Step: Displaying User Profile Data

 

The only other page in this scenario that required me to add code was the MyProfile.aspx page.  This page looks like this:

 

 

The page itself was pretty simple to implement.  I simply added a few asp:label controls on the page, as well a listbox for the roles.  Populating these controls with the profile and role information involved added a Page_Load event with 7 lines of code like so:

 

protected void Page_Load(object sender, EventArgs e) {

 

   Country.Text = Profile.Country;

   Gender.Text = Profile.Gender;

   Age.Text = Profile.Age.ToString();

 

   RoleList.DataSource = Roles.GetRolesForUser(User.Identity.Name);

   RoleList.DataBind();

}

 

Note that the profile object is strongly typed – which means profile properties will get statement completion and compilation checking in VS 2005 with it.  I can also then query the role management system to retrieve an array of all the roles the current user belongs to and then databind this to the listbox control. 

 

Since the MyProfile.aspx page requires a user to be logged in (otherwise retrieving profile information about them doesn’t make a lot of sense), I also added a <location> based authorization control tag in my web.config file:

 

<location path="MyProfile.aspx">

     

   <system.web>

      <authorization>

            <deny users="?"/>

            <allow users="*"/>

      </authorization>

   </system.web>

 

</location>

 

This is the same configuration I would have added in ASP.NET V1.1 – and basically tells the system to deny all users who aren’t logged in (the ? = anonymous), and then allow all users who are logged in (the * = all). 

 

Those who aren’t logged in will get automatically re-directed to the login.aspx page.  The <asp:login> control can be used there to allow users who have already registered to log-in without the developer having to write any custom code.

 

Summary

 

Hopefully this walks-through a fairly common real-world web app scenario and explains how it can be easily done in ASP.NET 2.0.  The ASP.NET 2.0 Membership, Role and Profile system (especially when combined with the new login controls) pack a lot of productivity power. 

 

What is really nice is that they are all built on the new ASP.NET 2.0 provider model.  Our of the box ASP.NET ships built-in providers that provide Membership, Roles and Profile storage inside SQL Express, SQL Server and Active Directory stores (the later for Membership and Roles only).  What is even better is that you can replace any of these implementations using a custom provider that you either buy or build yourself (see this blog post for more details on how to-do this).  Replacing a provider in ASP.NET 2.0 simply involves changing a value in a web.config file – the above code sample will work regardless of what provider was plugged in (LDAP, Oracle, DB2, Custom SQL Table Schema, File-System, etc).

 

Hope this helps,

 

Scott

 

Published Tuesday, October 18, 2005 12:43 AM by ScottGu

Comments

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Tuesday, October 18, 2005 10:03 AM by Joshua Flanagan
You could use even less code by using my ProfileView control (which was inspired by your ASP.NET 2.0 talk at TechEd04).

http://flimflan.com/files/ProfileView.aspx

(As soon as the RTM bits are on MSDN, I'll update the code appropriately - or, anyone can grab the source and do it themselves)

It'll probably only save you 3 lines in this case, but would make a bigger difference if you had more profile properties, or wanted the user to be able to edit their profile (after the create user wizard).

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Tuesday, October 18, 2005 10:46 AM by scottgu
Cools stuff Joshua! :-)

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Tuesday, October 18, 2005 12:38 PM by Dempsey
Speaking of RTM bits being on MSDN, do you know how much longer before we (MSDN subscribers) can get our hands on VS2005 RTM Scott?

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Tuesday, October 18, 2005 1:41 PM by scottgu
Hi Dempsey,

We haven't officially signed off yet on the final bits. I think it will probably be available for MSDN subscribers shortly before launch (but am not 100% sure of the date).

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Thursday, October 20, 2005 4:47 AM by Henk Feijt
Hi Scott,

Thanks for the examples and explanation. I have a question on the remember me checkbox. If the user checks this, will this mean that is logged in automaticly when she revisites the site. Or does a developer has to handle this herself.

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Thursday, October 20, 2005 10:38 AM by scottgu
Hi Henk,

If the user checks this the Login control will automatically set the forms-auth cookie to be persistant -- so assuming the ticket hasn't expired they will automatically be logged in next time without the developer having to-do anything.

One caveat in ASP.NET V2.0 is that (for security reasons) the default forms auth ticket length is now a sliding 30 minute window instead of the 50 years it defaulted to in V1.1. If you want to change this (which most people probably will), you can change this in your web.config file as a configuration setting.

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Sunday, October 23, 2005 4:59 PM by Michael Annesley
Will you be able to use the Login Controls with your own Database or do you have to allow ASP.Net to do so?

Thank you,

Michael

michael@dorsetwebsolutions.com

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Monday, October 24, 2005 1:50 PM by scottgu
Hi Michael,

Yep -- you can use your own databases used if you implement a Membership provider that goes against your database.

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Monday, October 24, 2005 7:14 PM by Jackson Kuang
Hi Scott,

You are getting me excited! Are you saying that all we have to do is to define the custom properties (country, gender and age) in the web.config, the ASP.NET 2.0 Profile system will create the fields in a table inside Aspnetdb? Wow that will be great if true!

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Monday, October 24, 2005 8:37 PM by scottgu
Hi Jackson,

Yep -- that is all you need to-do. :-)

We'll also be shipping another profile provider on the web that will allow you to bind the profile against an already defined database table as well. That way you'll also be able to integrate with existing data and perform even richer queries/updates on them.

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Wednesday, October 26, 2005 9:02 PM by John
Scott, " profile provider on the web that will allow you to bind the profile against an already defined database table"

Wow, that's excellent. Currently, in my development, I need to extract it manually to my tables.

Thanks,
John

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Saturday, October 29, 2005 4:51 PM by Andreas Kraus
Hey there,

I'm using the Login Controls now the standard way with the SQL Server Express mdf file as storage engine.

If I'd like to switch to MS-SQL (not express) sometime do I only have to copy all the tables and data to MS-SQL and switch the Provider in the web.config from express to mssql?

I would just like to go sure I understood everything :o, thanks a lot for your time!

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Sunday, October 30, 2005 4:31 PM by scottgu
Hi Andreas,

Here is a pointer to a blog entry I wrote about how to use SQL as your membership/roles/profile provider: http://weblogs.asp.net/scottgu/archive/2005/08/25/423703.aspx

If you already have a database using SQL Express, then you should be able to import and mount it inside a SQL Server using Enterprise Manager and avoid having to manually pull out and insert the data.

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Monday, October 31, 2005 2:43 AM by Andreas Kraus
Nice, thanks Scott! Everything worked fine.

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Tuesday, November 01, 2005 1:25 PM by Mike L
Hi Scott

This looks cool but am I missing something as the way that the event handlers AssignUserToRoles_Activate and AssignUserToRoles_Deactivate are created seems rather odd

I am running VS 05 RTM and the only way that I can see to create these is to assign the OnActivate and OnDeactivate property inside the source view of ASPX file and then write the 2 event handlers in the code behind page.

Nothing shows up anywhere under the Events view in the properties window so it a pain to add this code and it feels like the ASP coding days.

Should this be parts of the Tasks ?

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Wednesday, November 02, 2005 10:21 AM by Frank Peixoto
Great summary! this will help me a lot in my SSO project.

do you have any links on implimenting this with an already existing AD userbase?

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Wednesday, November 02, 2005 1:14 PM by scottgu
Hi Mike,

The wizardtask control I added in the sample is in a templated control -- which makes it hard to get to from the property-grid in design-view.

If you don't want to-do event wire hookup declaratively (which is what I did in the sample), you can alternatively do it programmatically by adding a Page_Init() event handler in your code-behind. There you can explictly wire them up without having to ever even open the .aspx file:

public void Page_Init(Object sender, EventArgs e)
{
wsAssignUserToRoles.Activate += new EventHandler(AssignUserToRoles_Activate);
wsAssignUserToRoles.Deactivate += new EventHandler(wsAssignUserToRoles_Deactivate);
}

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Wednesday, November 02, 2005 1:16 PM by scottgu
Hi Frank,

Here is a pointer to someone's blog post that walks through configuring the membership system to use AD instead of a SQL database: http://blogs.msdn.com/gduthie/archive/2005/08/17/452905.aspx

Enjoy!

- Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Wednesday, November 02, 2005 2:52 PM by Mike L
Scott

Many thanks - that is exactly what I need

Mike

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Thursday, November 03, 2005 1:48 PM by Joe
Thank you for your amazing codes.
I'd like to learn C# to better understand your code and more.

Could you recommend a beginner's book (Bible) for C# language, especially, I am more interested in web developing in C Sharp.

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Wednesday, November 09, 2005 2:32 AM by cowgaR
Maybe I am a little late and I am no Scot Gu nor ASP.NET only coder, but my beloved book in 1.0 times was from Jesse Liberty Programming C#, and it was in 4th edition last time I checked covering C#2.0 already.

It was one awesome book, but there are many more favorites out there(Proffesional ASP.NET 2.0 and Bill Evjen, Jeff Prosise was also my fav author on ASP etc) , but if you like to learn language try this book first, it covers ASP.NET to some extent.

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Tuesday, November 15, 2005 6:58 PM by Sense
Hi All,

Great thread about the magic functions of membership and role but does anybody have a suggestion to make all the process multilingual ?

Thanks all

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Tuesday, May 23, 2006 7:17 AM by Chris
This is an awesome example!  Do you have a similar example using Visual Basic?

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Monday, May 29, 2006 11:53 AM by Eric
pretty good

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Tuesday, May 30, 2006 11:10 AM by ScottGu
Hi Chris,

Unfortunately I don't have a VB sample yet -- but it is on my list of things todo!

Thanks,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Tuesday, May 30, 2006 3:15 PM by Robert
Scott --

How does one retrieve information for a given user once on log on, rather than on each page load, and keep that information for the entire session?  I was trying to use the LoggedIn event, but was having trouble.

Thanks,
Robert

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Wednesday, May 31, 2006 2:26 AM by ScottGu
Hi Robert,

You could store the information in the session object if you wanted to.  You can use the Login control's username property to retrieve it within the LoggedIn event to-do this.

In general, though, I'd probably recommend just working directly off of the Profile API -- which will fetch the data on demand as needed.

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Wednesday, May 31, 2006 12:21 PM by Esra
Can u creat all of them on the code behind? without source?

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Thursday, June 01, 2006 1:25 AM by ScottGu
Hi Esra,

Rather than use controls you could also use the Membership and Roles APIs directly to create users and map them into roles programmatically.

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Friday, June 02, 2006 10:32 AM by Brad
Hy Scott!

First, thanks for all you do!

I have a small problem with the CreateUserWizard. I have the LoginCreatedUser property set to false. I am creating their password initially for them and sending it to them in email to verify their email. I also need to do some other database work when their account is created. I need the user id(guid) that gets created but I dont see an easy place to retrieve that in the createduser event. Any idea how to get it?

Again, thanks!!
Brad Coble

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Friday, June 02, 2006 7:35 PM by ScottGu
Hi Brad,

You should be able to write this code within your CreatedUser event handler:

       MembershipUser mu = Membership.GetUser(CreateUserWizard1.UserName);
       Guid puk = (Guid)mu.ProviderUserKey;

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Saturday, June 03, 2006 4:56 PM by wazzzuup
And how can I remove e-mail from user credentials?

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Monday, June 05, 2006 2:42 PM by Adnan
Hello Scott,

I have successfully created a user file by using SQL Memebership provider and added below lines in my web.config file
<profile enabled="true" defaultProvider="AspNetSqlMembershipProvider">
     <providers>
       <add  name="AspNetSqlMembershipProvider"       </providers>
     <properties>
       <add name="FirstName" type="string" />
       <add name="LastName" type="string" />
     </properties>
   </profile>

First Name & Last Name successfully saved in aspnet_profile table. But I am getting error message when I am trying to retireve user profile. Below is my code

ProfileCommon prf = new ProfileCommon();
UserFirstName.Text = prf.FirstName.ToString();
serLastName.Text = prf.LastName.ToString();

and error message is
The settings property 'FirstName' was not found.
The settings property 'LastName' was not found.

Could you please let me know what I am missing.

Regards

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Tuesday, June 06, 2006 7:00 AM by AP
I am trying to create a create account screen based on what you have here. The issue I am facing is I am not able to trap dropdown list box events. I have country dropdown list and upon selecting the Country I would like to populate the state but I am not able to trap the event for dropdown listbox. I am using VB.NET BTW.

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Tuesday, June 06, 2006 11:18 AM by ScottGu
Hi Adnan,

Have you tried doing this:

UserFirstName.Text = Profile.FirstName.ToString();

Does that work?

Thanks,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Tuesday, June 06, 2006 11:19 AM by ScottGu
Hi AP,

I think the problem is that the dropdownlist is within a Wizard template step -- and so you can't use the VB handles event syntax to wire-up an event on it.

Instead, you should manually add a "onselectedchange" attribute to the dropdownlist control to point at the event handler in your code-behind.

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Tuesday, June 06, 2006 11:23 AM by ScottGu
Hi Wazzup,

You can use the Membership.GetUser() method to return a MembershipUser object for the user you want to lookup.  You can then use this to reset or change someone's email address.

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Thursday, June 15, 2006 6:23 AM by woddylee
Hi

I am new to asp.net 2.0

I have some questions here, I would like to build a website that allows user to create a new account and login to the website, and one more thing I would like to do is allow user to save their own information in my website like Full name, Phone NO, Address and so on, and after user login, they are able to change their information and save it again.

And base on this can I use built in asp.net 2.0 database to achieve these functions? or I need to create a new database?

And one more thing that I would like to know is if I use asp.net 2.0 profile provider, could I achieve those function oso ?

And the built in asp.net 2.0 user database is suit to which kind of website?

Thanks! :(

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Friday, June 16, 2006 2:23 PM by L. Pulsifer
Hi Scott,
How can I add a checkbox rather than a textbox or dropdown list as in your example?
I am trying the following:
   <profile enabled="true">
     <properties>
       <add name="Any" type="CheckBox"/> etc. in web.config and in the .cs
// Populate some Profile properties off of the create user wizard
p.Any = (CheckBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Any").NamingContainer;
The compile errors say I am missing an assembly reference to the type="Checkbox" so when I tried "Boolean" but the profile's value is System.Web.UI.WebControls.CheckBox.
Can you help?
Thank you.

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Sunday, June 18, 2006 7:10 PM by ScottGu
Hi L. Pulsifer,

What you'll want to store in the profile is not a checkbox -- but rather a boolean value that maps to the CheckBox's value.

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Sunday, June 18, 2006 11:09 PM by Ashok Padmanabhan
Hi Scott
Having a bit of trouble with the custom profilecommon class i made per your tip at http://webproject.scottgu.com/VisualBasic/migration2/migration2.aspx.
I have the following in the web.config:
<properties>
         <add name="FirstName" allowAnonymous="true" type="string"/>
         <add name="LastName" allowAnonymous="true" type="string"/>
         <add name="Location" allowAnonymous="true" type="string"/>
       </properties>
And this should do it but name doesnt appear:  Dim pc As New ProfileCommon
Response.Write(pc.FirstName)

Any ideas what i shold do?

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Sunday, June 18, 2006 11:46 PM by Bhavin Patel
I am trying to follow your example in VB and I get the below error. I will appreciate if you try to fix this for me. I am not able to save profile for user.
System.NullReferenceException was unhandled by user code
 Message="Object reference not set to an instance of an object."

Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser
       Dim p As ProfileCommon
       p = ProfileCommon.Create(CreateUserWizard1.UserName, True)

       'If p Is Nothing Then
       p.SemaID = (CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtsemaid"), TextBox)).Text
       p.Save()
       ' Else
       Response.Write("Error")
       ' End If
   End Sub

# ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Tuesday, June 20, 2006 3:40 AM by ScottGu's Blog
I usually try and spend at least an hour or two each night hanging out on the ASP.NET Forums answering...

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Wednesday, June 21, 2006 6:05 AM by martin harvey
the article was really useful but is it possible to use the same idea without the wizard using something like this

Public Sub CreateUser_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CreateUser.Click


       Membership.CreateUser(UserName.Text, Password.Text, Email.Text)

Dim p as Profile.Common=????????
   p.Country = country.text  
   p.Gender  = gender.text
   p.Age     = age.text

   End Sub
if so what would the missing code be?

many thanks

martin

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Thursday, June 22, 2006 1:26 AM by ScottGu
Hi Martin,

Yep -- you can absolutely use the concepts without requiring the CreateUserWizard control.  

The missing line of code you are looking for looks like this:

Profile = Profile.Create(userName.Text)

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Thursday, June 22, 2006 1:27 AM by ScottGu
Hi Ashok,

Can you send me email (scottgu@microsoft.com)?  I can then point you at a utility that allows you to use strongly-typed profile properties with the VS 2005 Web Application Project.

Hope this helps,

Scott

# ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Wednesday, July 05, 2006 3:52 AM by monkeyliu19801029

因为导师要个网站的项目, 我又对ASP.NET也蛮感兴趣的,毕竟未来是网络的时代。 看了乱七八糟的书后,开始专门争对自己负责的登陆模块有目的的学习了..... 这是在ASP.NET官方网站发现的最好的ScottGu的Blog, 以后陆续专贴的文章只是为了记录我的学习历程

# Tip/Trick: Gathering Custom User Registration Information

Thursday, July 06, 2006 1:42 AM by ScottGu's Blog

Problem You are building a web-site that allows customers to register new users on the site. As part

# .NET Knowhow &raquo; Blog Archive &raquo;

Tuesday, July 18, 2006 4:56 AM by .NET Knowhow » Blog Archive »

PingBack from http://knowhow.styleblogs.de/2006/07/18/3/

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Tuesday, July 25, 2006 9:01 AM by Andy
Hi Scott, In my recent ASP.NET 2.0 appl, I need to verify that the supplied email address is valid or not. So, here's my situation: - In my area, I created property. - Suppose a new user has been created. I set the profile.isverified to false. OnCreatedUser event I'll send him an email (to the supplied email address) to verify their email address with a link in it to an ASPX page that'll do the verification, e.g: verify.aspx?u=aUserName - On page_load of verify.aspx, I want to change the property to true and set the to true, so he can start log in. How to do this? I'm using ASP.NET 2.0 (VB.NET) and MSSQL 2K for membership database. Thanks in advance, Andy

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Wednesday, July 26, 2006 1:37 AM by ScottGu

Hi Andy,

When you create the new user using the Membership API, you can actually mark the account as not approved directly via the Membership API.  What this means is that you don't need to use the Profile API for this.

You'd then send them a dynamic random URL to click to activate the account.  You could to this using the System.Net.Mail namespace -- which you can learn more about here: http://weblogs.asp.net/scottgu/archive/2006/01/03/434453.aspx

One the page that they link back to from, you'd then write code like this to activate the account

Dim userdetails as MembershipUser

userdetails = Membership.GetUser(username)

userdetails.IsAppoved = true

Membership.UpdateUser(userdetails)

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Friday, July 28, 2006 10:18 AM by Firoz
Hi Scott, How would one go about adding a 2nd password into the login? I would imagine that I'd have to write a custom membership class, and my own login control. But what would initiate the authentication process? Regards Firoz

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Friday, July 28, 2006 9:38 PM by ScottGu

Hi Firoz,

There are a couple of ways you could do this.  One would be to take the built-in ASP.NET Membership Provider and customize/extend it to support another column for a second password, and then just have the ValidateUser method check both passwords.

If you use this approach then you don't need to re-implement any Login controls -- since they'll just call into your provider method (whose signature would stay the same).

This website has tons of information on how to build providers, and also includes the source-code to the built-in ASP.NET ones: http://msdn.microsoft.com/asp.net/downloads/providers/

Hope this helps,

Scott

# re: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code

Monday, July 31, 2006 2:12 AM by SixSide
Great article! Exactly what I was looking for. One question if you don't mind Scott... can you explain a little bit about what happens to the database once you add the custom profile properties to the web.config file? Does it actually create new columns in the database to store this data? If so, at what p