Client Application Services - Part 3
page 3 of 7
by Bilal Haidar
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 35480/ 61

Web User Settings

Now that the Profile service has been enabled and configured on the ASP.NET host application it is time to configure the Windows client application to use that remote Profile service.

In Windows Forms, there has been a feature known as Application Settings. This feature allows you to store data per application and per user locally similar in a way to the concept of Application and Session variables in ASP.NET. The data in both cases is stored in XML files in the user’s local hard disk. The way to add those settings is by adding a special item to your application with the extension of .Settings. This item is nothing but an XML file with a nice UI designer that allows you to add settings that target both application and user settings. Once you add any settings to that file, a new instance of the Settings class is now accessible in your client application by using the Properties.Settings.Default. The Default object is the default instance of the Settings class which has a base class of ApplicationSettingsBase. All the settings you add to the Windows client application are automatically added as strongly typed properties to the Default object so that you can easily access them inside your code. For more information on Application Settings, follow this link: Application Settings Overview.

Going back to enabling the Profile service inside the Windows client application, Figure 1 below shows you how to add the URL of the remote Profile service at the Services tab of the client application.

Figure 1: Services Tab

As you can see in the figure above, the last section is the Web settings services. As mentioned in the above paragraph, there are Application and User settings, however, when accessing Profile service, what we will do is import all the Profile objects into the Windows client application and Visual Studio automatically imports the Profile objects as User settings with a special notation called Web settings. Therefore, Profile objects will be imported as Web User settings! To load those objects, first make sure you have configured the Web settings service location to the same URL where the Authentication and Authorization services are located. Client Application Services framework automatically appends the Profile_JSON_AppService.axd when the Profile objects are accessed. In the next step, create a new .Settings file in your Windows application, open it, and then press on the Load Web Settings button on the top toolbar as shown in Figure 2 below.

Figure 2: Loading Web user settings

By pressing on the Load Web Settings, you will notice two new user settings were created for you. Those user settings were loaded from the Profile service configuration on the ASP.NET host application. This step is necessary before you can work with the Web user settings.

The Web user settings are now loaded and ready to be accessed by your application to read from and write to.

Now back to the Windows client main application Form_Load event, we will issue a call to a method called BindWebSettings that will retrieve the Web user settings and bind their values on the main form as follows.

Listing 3

private void BindWebSettings()
{
  try
  {
    // Add binding to the last login date
    this.lblLastLoginDate.DataBindings.Add("Text", Properties.Settings.Default,
      "LastLoginDate");
 
    // Set the background color to the one stored in the application settings
    if (string.IsNullOrEmpty((Properties.Settings.Default.Back_Color)))
      Properties.Settings.Default.Back_Color = "#909090";
 
    this.BackColor = System.Drawing.ColorTranslator.FromHtml(
      (Properties.Settings.Default.Back_Color));
  }
  catch (System.Net.WebException)
  {
    MessageBox.Show("Unable to access the Web Settings service.""Warning",
      MessageBoxButtons.OK, MessageBoxIcon.Warning);
  }
}

As you can see, working with Web user settings is no different than working with normal application settings. First of all, a binding is added to a label on the main form to show the last login date for the currently authenticated user. In addition, the color name stored in another Web user setting is converted to a real color and set to the main form’s background color.

A textbox is now placed on the main form with a label to allow the user to change the value of the Back_Color Web user setting. When the user clicks on that button, the code below executes to save the changes locally into the remote Profile service as follows.

Listing 4

private void SaveSettings()
{
  if
    (!System.Threading.Thread.CurrentPrincipal.Identity.AuthenticationType.Equals(
      "ClientForms"))
    return ;
 
  try
  {
    Properties.Settings.Default.Save();
  }
  catch (System.Net.WebException ex)
  {
    // This means you are logged out
    if (ex.Message.Contains("You must log on to call this method."))
    {
      MessageBox.Show(
        "Your session has expired. Please login again to be able to save" +
        "your settings.""Saving Web Settings");
 
      try
      {
        // Show the Login form for the user to enter his/her credentials
        // to login again
        if (!Membership.ValidateUser(String.Empty, String.Empty))
        {
          MessageBox.Show("Unable to authenticate. " +
            "Settings were not saved on the remote service.""Not logged in",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
          // Try again saving the settings
          // after the user has been authenticated
          SaveSettings();
        }
      }
      catch (System.Net.WebException)
      {
        MessageBox.Show("Unable to access the authentication service. " +
          "Settings were not saved on the remote service.""Not logged in",
          MessageBoxButtons.OK, MessageBoxIcon.Warning);
      }
    }
    else
    {
      MessageBox.Show("Unable to access the Web settings service. " +
        "Settings were not saved on the remote service.""Not logged in",
        MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
  }
}

As you can see, some checking is added to make sure the user is logged in using the ClientForms authentication type. The Save method, defined on the ApplicationBaseSettings base class, is executed to save the Web user settings locally into the remote Profile service. If the user was not logged in, the Login form pops up to allow the user to enter his/her credentials again to be authenticated before being able to save the Web user setting remotely.

Finally, to make sure we are on the safe side, we have subscribed to the Form_Closing event to make sure a call to save the Web user settings is present. If the user, by mistake or intentionally, closed the main application, we have to make sure the Web user settings are saved back to the remote Profile service successfully.


View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 11:25:21 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search