Results 1 to 12 of 12

Thread: Using the Profile Provider in ASP.Net 2.0

Threaded View

  1. #1

    Thread Starter
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Using the Profile Provider in ASP.Net 2.0

    Hello,

    The following tutorial will attempt to explain the steps that need to be carried out in order to use the built-in ASP.Net Profile Provider.

    Why would you want to use the built-in Profile Provider?

    Let's say you wanted to store additional information about the user's that get created on your website, for instance, their first name, or their last name. These are not things that are created for you out of the box, but with a few small steps you can add as many additional properties for each user as you want.

    At the end of this codebank submission I have posted a small sample application that shows the use of the Profile Provider.

    Step 1:
    Create a new website project

    Step 2:
    Use the ASP.Net Website Configuration tool to select the Authentication Type for the Website as "From the internet". Create the users and roles that you want to use on the website.

    Step 3:
    To make things easier later, I normally copy the standard Provider Settings from the machine.config file, and override them in my web.config file. You can normally find the machine.config at the following location:

    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG

    Here are the standard settings for each provider; Membership, Roles, and Profile:

    Code:
    <system.web>
        <membership>
            <providers>
                <add name="AspNetSqlMembershipProvider" 
                    type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
                    connectionStringName="LocalSqlServer" 
                    enablePasswordRetrieval="false" 
                    enablePasswordReset="true" 
                    requiresQuestionAndAnswer="true" 
                    applicationName="/" 
                    requiresUniqueEmail="false" 
                    passwordFormat="Hashed" 
                    maxInvalidPasswordAttempts="5" 
                    minRequiredPasswordLength="7" 
                    minRequiredNonalphanumericCharacters="1" 
                    passwordAttemptWindow="10" 
                    passwordStrengthRegularExpression=""/>
                </providers>
            </membership>
    	<profile>
    	    <providers>
    	        <add name="AspNetSqlProfileProvider" 
                        connectionStringName="LocalSqlServer" 
                        applicationName="/" 
                        type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
               </providers>
    	</profile>
    	<roleManager enabled="true">
    	    <providers>
    		<add name="AspNetSqlRoleProvider" 
                        connectionStringName="LocalSqlServer" 
                        applicationName="/" 
                        type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    	    </providers>
            </roleManager>
    With this in place, you can then start to create the properties that you want to be available for each user. These settings get added to the <profile> node in your web.config file using the <properties> node. It is possible to create groups within this <properties> node to group common settings together. Once in your code behind files, you will be able to access these properties in a strongly typed way. Here is an example of the types of properties that you can add:

    Code:
    <properties>
        <add name="FirstName" type="String" serializeAs="String"/>
        <add name="LastName" type="String" serializeAs="String"/>
        <add name="Gender" type="String" serializeAs="String"/>
        <add name="BirthDate" type="DateTime" serializeAs="String"/>
        <add name="Occupation" type="String" serializeAs="String"/>
        <add name="Website" type="String" serializeAs="String"/>
        <group name="Forum">
            <add name="Posts" type="Int32" defaultValue="0"/>
            <add name="AvatarUrl" type="String" serializeAs="String"/>
            <add name="Signature" type="String" serializeAs="String"/>
        </group>
        <group name="Address">
            <add name="Street" type="String" serializeAs="String"/>
            <add name="PostalCode" type="String" serializeAs="String"/>
            <add name="City" type="String" serializeAs="String"/>
            <add name="State" type="String" serializeAs="String"/>
            <add name="Country" type="String" serializeAs="String"/>
        </group>
        <group name="Contacts">
            <add name="Phone" type="String" serializeAs="String"/>
            <add name="Fax" type="String" serializeAs="String"/>
        </group>
    </properties>
    Specifying the type will mean that these properties are treated as this type in your code.

    Step 4:
    Add the necessary controls to begin accessing these properties. In the sample project, you will find that I have added a Login Control so we can log in as a specified user, and also a CreateUserWizard for creating new users and creating new profiles.

    In order to piece all these things together, there are a number of things that need to be done. For instance, when creating a new user, you need to specifically create a new profile, and this is done using the following in the CreatedUser Event:

    Code:
        protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
        {
            // Create an empty Profile for the newly created user
            ProfileCommon p = (ProfileCommon)ProfileCommon.Create(CreateUserWizard1.UserName, true);
    
            // Save profile - must be done since we explicitly created it
            p.Save();
        }
    This is a little bit of overkill here, as we are not actually adding anything to the profile, but you could add additional steps in the CreateUserWizard to capture information and add them here.

    Step 5:
    Now we need to extract the information. When a valid user logs into the sample application, they are re-directed to another page, and that page loads information from the profile, and also allows the user to change the information and save it again.

    The information is extracted in the load event using:

    Code:
            if (!this.IsPostBack)
            {
                ProfileCommon p = this.Profile.GetProfile(this.User.Identity.Name);
    
                FirstNameTextBox.Text = Profile.FirstName;
                LastNameTextBox.Text = Profile.LastName;
                GenderTextBox.Text = Profile.Gender;
                BirthDateTextBox.Text = Profile.BirthDate.ToString();
                OccupationTextBox.Text = Profile.Occupation;
                WebSiteTextBox.Text = Profile.Website;
                PostsTextBox.Text = Profile.Forum.Posts.ToString();
                AvatarURLTextBox.Text = Profile.Forum.AvatarUrl;
                SignatureTextBox.Text = Profile.Forum.Signature;
                StreetTextBox.Text = Profile.Address.Street;
                PostalCodeTextBox.Text = Profile.Address.PostalCode;
                CityTextBox.Text = Profile.Address.City;
                StateTextBox.Text = Profile.Address.State;
                CountyTextBox.Text = Profile.Address.Country;
                PhoneTextBox.Text = Profile.Contacts.Phone;
                FaxTextBox.Text = Profile.Contacts.Fax;
            }
    And then in the save button we save the information back again:

    Code:
            ProfileCommon p = this.Profile.GetProfile(this.User.Identity.Name);
    
            p.FirstName = FirstNameTextBox.Text;
            p.LastName = LastNameTextBox.Text;
            p.Gender = GenderTextBox.Text;
            p.BirthDate = DateTime.Parse(BirthDateTextBox.Text);
            p.Occupation = OccupationTextBox.Text;
            p.Website = WebSiteTextBox.Text;
            p.Forum.Posts = int.Parse(PostsTextBox.Text);
            p.Forum.AvatarUrl = AvatarURLTextBox.Text;
            p.Forum.Signature = SignatureTextBox.Text;
            p.Address.Street = StreetTextBox.Text;
            p.Address.PostalCode = PostalCodeTextBox.Text;
            p.Address.City = CityTextBox.Text;
            p.Address.State = StateTextBox.Text;
            p.Address.Country = CountyTextBox.Text;
            p.Contacts.Phone = PhoneTextBox.Text;
            p.Contacts.Fax = FaxTextBox.Text;
    
            p.Save();
    You can see here that I have specifically and to convert to and from integers and DateTime due to the settings that we used in the web.config file.

    Hopefully this has showed you the basic steps to using a Profile within ASP.Net.

    The default database that I created for whatever reason is 10MB in size!! I have tried to shrink it, but it is not working. Instead of attaching this file, I have created an sql file that will generate the required database.

    If you have any questions, then feel free to post back.

    Gary
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width