http://SilverlightDesktop.net

An Examination of the WhoAmI Module

The WhoAmI module that is provided in the basic installation package of SilverlightDesktop, performs two functions. First, to confirm that your SilverlightDesktop is working properly and second to provide an example of the process that SilverlightDesktop modules use to communicate with the Silverlight installation.

A SilverlightDesktop module does not have to communicate with the SilverlightDesktop installation, it can simply launch and communicate with an external web service or not communicate with anything at all. However, when you desire to know who the user is that launched the SilverlightDesktop module, you will use web services.

The WhoAmI module consists of two parts. The WhoAmI Silverlight module and the wsWhoAmI web service.

Both projects compile their output to the SilverlightDesktop website (note: an .asmx or .svc file such as WhoAmI.asmx file must be copied into the project manually).

The wsWhoAmI Project

The wsWhoAmI web service project contains a reference to the SilverlightDesktopCore project. This allows it to call the core methods such as retrieving the current user. The following code demonstrates this:

[WebService(Namespace = "http://SilverlightDesktop.net/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WhoAmI : System.Web.Services.WebService
{
    #region WhoIAm
    [WebMethod(Description = "WhoIAm")]
    [ScriptMethod()]
    public UserInfo WhoIAm(int PortalID, int ModuleId, int UserID, string Password)
    {
        SilverlightDesktopAuthendicationHeader SilverlightDesktopAuthendicationHeader = new SilverlightDesktopAuthendicationHeader();
        SilverlightDesktopAuthendicationHeader.PortalID = PortalID;
        SilverlightDesktopAuthendicationHeader.UserID = UserID;
        SilverlightDesktopAuthendicationHeader.Password = Password;
        SilverlightDesktopAuthendicationHeader.ModuleId = ModuleId;
 
        UserInfo response = new UserInfo();
 
        Authendication Authendication = new Authendication(SilverlightDesktopAuthendicationHeader);
        if (Authendication.IsUserValid())
        {
            response = Authendication.GetUserInfo();
        }
        else
        {
            response.FirstName = "Visitor";
            response.LastName = DateTime.Now.ToLongDateString();
        }
 
        return response;
    }
    #endregion

The WhoAmI Silverlight Project

The Silverlight project calls the wsWhoAmI web service. The Silverlight project must know the location of the web service and the current username and password to use to call the web service.

When SilverlightDesktop creates a new instance of the WhoAmI module it uses the following code to pass the required parameters to the WhoAmI module through the Tag property:

                // Pass the parameters to the control that it will need to connect to the website
                objUserControl.Tag = string.Format("{0}, {1}, {2}, {3}, {4}", intPortalID, intModuleId, intUserID, strPassword, strWebServiceBase);

The WhoAmI module uses the following code to retrieve the parameters and to call the method (ShowWhoAmI()) that will invoke the web service:

        #region Page_Loaded
        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            // Get parameters from the Tag
            char[] delimiterChars = {','};
            string[] strParameters = this.Tag.ToString().Split(delimiterChars);
 
            intPortalID = Convert.ToInt32(strParameters[0]);
            intModuleId = Convert.ToInt32(strParameters[1]);
            intUserID = Convert.ToInt32(strParameters[2]);
            strPassword = strParameters[3];
            strWebServiceBase = strParameters[4];
            
            ShowWhoAmI();
        }
        #endregion

With all the needed parameters, the WhoAmI module can call the web service and display information about the current user:

        #region ShowWhoAmI
        private void ShowWhoAmI()
        {
            BasicHttpBinding bind = new BasicHttpBinding();
            EndpointAddress MyEndpointAddress = new EndpointAddress(strWebServiceBase + "WhoAmI.asmx");
            var proxy = new WhoAmISoapClient(bind, MyEndpointAddress);
 
            proxy.WhoIAmCompleted += new EventHandler<WhoIAmCompletedEventArgs>(proxy_WhoIAmCompleted);
            proxy.WhoIAmAsync(intPortalID, intModuleId, intUserID, strPassword);
        }
 
        void proxy_WhoIAmCompleted(object sender, WhoIAmCompletedEventArgs e)
        {
            UserInfo UserInfo = (UserInfo)e.Result;
            DisplayWhoIAm(UserInfo);
        }
 
        private void DisplayWhoIAm(UserInfo UserInfo)
        {
            this.txtFirstName.Text = UserInfo.FirstName;
            this.txtLastName.Text = UserInfo.LastName;
            this.txtEmail.Text = UserInfo.Email;
        }
        #endregion