Sharker Khaleed Mahmud's Blog

ASP.NET, MVC, BLEND, Silverlight, AJAX and JQuery

  • Me

    Sharker Khaleed Mahmud
  • Visit my recent blog silverlightips.wordpress.com

Using ASP.NET Profile in Silverlight Part 1: Create Profile

Posted by Sharker Khaleed Mahmud | shamrat231 on December 29, 2009

This week I will try to show you how easy it is to use ASP.NET profile in Silverlight. For this demonstration I will be using VS2010 Beta 2 and Silverlight.Net 4.0. The same technique is also applicable for people using VS2008 and Silverlight 3.0.

So lets create a simple Silverlight project

Now add couple things on the order shown.

  1. Add App_Code folder and then add a Profile.cs
  2. Add App_Data folder and then add a Profle.mdf. On Data section in Add New Item form you will be able to add SQL Server Database.
  3. Add a folder called WebServices. Then add Silverlight-enabled web service from Add New Item. This item is available at Silverlight section. Now name the service as ProfileService.svc

The output should look something like this.

 

Now add the newly created database in your SQL Server. In this case I am using SQL Server 2008. Use the attach property to add your database. The below illustration shows database configuration on my laptop. Do not expect the same settings on your pc. Use the attach option to attach the new database (Profile.mdf). Then click on the add button. Choose the database. Afterwards click on the ok button. It will attach the database and will create random name for the db. Rename to Profile. Confused? see below.

Now we add the ConnectionString to our web.Config for this database

Web.Config

<connectionStrings>
    <add name=”ConnectionString” connectionString=”Data Source=(local);Initial Catalog=Profile;Integrated Security=True”
        providerName=”System.Data.SqlClient” />
  </connectionStrings>

Ok now we will use aspnet_regsql.exe to generate all the necessary tables related to Login system. You will find the exe file in for ASP.NET 4.0

Run the exe depending on the version you are using. For this case I will be using v4.0. Give your server name and choose the profile database that you have created.

Click next and finish. If you now go to your database you will see all the necessary tables have been created. Now lets come back to VS2010. Open the profile class. I will now add property that i want to get or set in the database on the profile table. Here is an example on how that can be done. For eg. I will add a property call DashboardIndex where it will store some variables that the current user logged in. Rename the class name from profile to UserProfile and extend the class to ProfileBase as shown below.

Profile.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Profile;
using System.Web.Security;

namespace Profile.Web
{
    public class UserProfile : ProfileBase
    {
        public string DashboardIndex
        {
            get { return base[“DashboardIndex”] as string; }
            set { base[“DashboardIndex”] = value; }
        }

        public static UserProfile GetUserProfile(string username)
        {
            return Create(username) as UserProfile;
        }

        public static UserProfile GetUserProfile()
        {
            MembershipUser user = Membership.GetUser();
            if (user != null)
            {
                return Create(user.UserName) as UserProfile;
            }
            else
            {
                return null;
            }
        }
    }
}

The first method creates a user profile against the asp.net login username that you assign. The second method creates an asp.net profile based on the current logged in user. Lets for now keep the profile class as simple as possible. Go to your current Web.config to add the profile tag as shown below.

Web.Config

<system.web>
    <compilation debug=”true” targetFramework=”4.0″ />
    <profile enabled=”true” defaultProvider=”CustomProfileProvider” inherits=”Profile.Web.UserProfile” automaticSaveEnabled=”true”>
      <providers>
        <clear/>
        <add name=”CustomProfileProvider” type=”System.Web.Profile.SqlProfileProvider” connectionStringName=”ConnectionString” applicationName=”/” />
      </providers>
    </profile>
</system.web>

Look at the bold code part. Here Profile.Web is the namespace in Profile.cs (have class UserProfile).

Thats all about setting up profiling. Now to access asp.net profile information, I will create two methods on my Profile.svc. One method is to get the value from the database on DasboardIndex property. Another method will be used to save the dashboard index value given by the user. See the code below.

 ProfileService.svc

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;

namespace Profile.Web
{
    [ServiceContract(Namespace = “”)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ProfileService
    {
        [OperationContract]
        public string GetCurrentDashboardSettings()
        {
            UserProfile profile = UserProfile.GetUserProfile();
            return profile.DashboardIndex;
        }

        [OperationContract]
        public void SaveCurrentDashboardSettings(string indexCollection)
        {
            UserProfile profile = UserProfile.GetUserProfile();
            profile.DashboardIndex = indexCollection;
            profile.Save();
        }
    }
}

Now go to web.config and remove the bindingConfiguration=”Profile.Web.ProfileService.customBinding0″ and change the binding to binding=”basicHttpBinding” as shown below

Web.Config

         <services>
            <service name=”Profile.Web.ProfileService”>
                <endpoint address=”” binding=”basicHttpBinding”
                    contract=”Profile.Web.ProfileService” />
                <endpoint address=”mex” binding=”mexHttpBinding” contract=”IMetadataExchange” />
            </service>
        </services>

Ok now go to Profile Silverlight project. Right click on the project and choose the Add Service Reference option. A form will open. Click on the Discover button.

On clicking the ok button a ProfileServiceReference will be created. Guess I will finish here. Any comments welcome. On next post i will continue with part 2.

Thanks
Sharker Khaleed Mahmud
MCTS, MCPD
DHAKA, BANGLADESH

One Response to “Using ASP.NET Profile in Silverlight Part 1: Create Profile”

  1. Hello there – just a quick note to say thanks for this entry. Very educational.

Leave a reply to Erica Schwanke Cancel reply