PortSight Secure Access Documentation

Storing and Retrieving User Settings

 

In this tutorial you will learn how to store user's preferred color in the Secure Access database and how to retrieve it in your application.

  1. Open your project with Secure Access authentication implemented. Add a new page called UserSettings.aspx.

  2. Add following lines at the beginning of the page code-behind, so that you can use Secure Access libraries in this page.

    [Visual Basic]
    
    Imports PortSight.SecureAccess.ARDataServices
    Imports PortSight.SecureAccess.ARObjects
    
    
    [C#]
    
    using PortSight.SecureAccess.ARDataServices;
    using PortSight.SecureAccess.ARObjects;				

  3. Put a new button called "Button1" and a new textbox called "Label1". Change the text of the "Label1" control to "Enter your preferred color:". Add a new label named "Label2" and change its text to "This is your preferred color.".

  4. Add following code to the Page_Load method:

    [Visual Basic]
    
    If Not Page.IsPostBack Then
        'get user's preferred color
        Dim prefColor As String
        Dim arcn As New ARConnection()
        arcn.ConnectToCatalog()
        prefColor = arcn.GetUserByLogin(User.Identity.Name).GetPropertyValue("preferred_color")
        If prefColor Is Nothing Then
            prefColor = ""
        End If
        Label2.Attributes.Add("style", "color:" & prefColor)
        txtPreferredColor.Text = prefColor
        arcn.Close()
    End If
    
    
    [C#]
    
    if (! Page.IsPostBack)
    {
    	// get user's preferred color
    	String prefColor;
    	ARConnection arcn = new ARConnection();
    	arcn.ConnectToCatalog();
    	prefColor = arcn.GetUserByLogin(User.Identity.Name).GetPropertyValue("preferred_color");
    	if (prefColor == null)
    	{
    		prefColor = "";
    	}
    	Label2.Attributes.Add("style", "color:" + prefColor);
    	txtPreferredColor.Text = prefColor;
    	arcn.Close();
    }

    What you did:

    You added code that retrieves preferred color from the user's settings, fills the text box and changes color of the main label.

  5. Double-click the "Button1" button and you will see the event handler code for this button. Add following lines to this method:

    [Visual Basic]
    
    'store preferred user color
    Dim arcn As New ARConnection()
    arcn.ConnectToCatalog()
    arcn.GetUserByLogin(User.Identity.Name).SetPropertyValue("preferred_color", txtPreferredColor.Text)
    Label2.Attributes.Add("style", "color:" & txtPreferredColor.Text)
    arcn.Close()
    
    
    [C#]
    
    //store preferred user color
    ARConnection arcn = new ARConnection();
    arcn.ConnectToCatalog();
    arcn.GetUserByLogin(User.Identity.Name).SetPropertyValue("preferred_color", txtPreferredColor.Text);
    Label2.Attributes.Add("style", "color:" + txtPreferredColor.Text);
    arcn.Close();								

    What you did:

    You added code that stores preferred color in the user's settings.

  6. Compile and run your application. Log on and navigate to the UserSettings.aspx page. Click on the button. Try to enter new color (you can use the same names you use in HTML) and store it in your settings. Close the browser, open a new browser window and navigate to this page. You should see the label again in the color you chosen.