Share via


Collecting User Data Example (C#)

Collecting User Data Example

The following are the basic coding steps used to access user data from the GetProfileObject method of the PassportIdentity object:

  1. Prevent caching.

    Microsoft® .NET Passport participating sites should construct their code so that HTTP proxies for a user will not cache the user's personal data under any circumstances. Depending on implementation and configuration, certain proxies will cache requests even when they should not. Your site can prevent these problems by using one of the following strategies.

    • Setting Custom Headers

      Set headers specifically to disable caching for HTTP 1.0 and 1.1 with a Pragma directive, Cache-control directive, and an Expires directive with a date in the past. Include the following text lines directly in custom headers on your servers:

      "Pragma: no-cache\r\ncache-control: private\r\nCache-control: private\r\nExpires: Fri, 01 Jan 1999 12:00:00 GMT\r\n"

      Pragma: no-cache follows HTTP 1.0 protocol, whereas the two Cache-control directives (the second to account for possible case-sensitive implementation) and the client-side Expires directive are HTTP 1.1. This should disable caching for all common proxy implementations and configurations that follow HTTP standards.

    • Setting Equivalent Headers Using ASP.NET

      Your site can specifically set headers for an individual page by using the following ASP.NET code, instead of manipulating the HTTP response directly.

<% Response.Cachecontrol="private"; Response.Expires=0; Response.AddHeader("pragma", "no-cache"); %>

    If you use ASP.NET to set headers, make sure to do so before any HTML is sent, or use buffering to ensure that no HTML is sent before the header is formed. Buffering will be on by default in Microsoft® Internet Information Services (IIS) 5.0. Be aware that using ASP.NET methods to set headers may duplicate any existing IIS custom header properties of sites or pages, effectively causing the header directives to be written twice.
  1. Make sure the user is signed in.

    Use the HasTicket property of the PassportIdentity object to check this. For information about establishing whether a user is signed in, and redirecting to the .NET Passport Login server, see Single Sign-In Overview.

  2. Get the user's .NET Passport Unique ID (PUID).

    The PUID is obtained by calling the HexPUID method.

    Alternatively, the PUID can be obtained by calling the GetProfileObject method for the attributes MemberIDHigh and MemberIDLow. The values returned are decimal and should be converted to hexadecimal before combining them to form a single identity.

  3. Get any other attributes that the site requires.

    Other attributes are also retrieved using the GetProfileObject method. For a list of the .NET Passport profile attributes, see Core Profile Table. Remember that, except for MemberIDHigh, MemberIDLow, and BDay_Precision (for Microsoft® Kids Passport), any one of a user's attributes could potentially be null or an empty string.

Example Code for User Data Access

The following shows the outline of ASP.NET code that illustrates these steps.

<%

// prevent page from being cached.
because you are displaying user data
Response.Cachecontrol="private";
Response.Expires=0;
Response.AddHeader("pragma", "no-cache");

// this code assumes other code that signs user in,
// but calling HasTicket as a failsafe
// assumes oMgr (PassportManager object) already created
if (oMgr.HasTicket) {
      String nickname, memberidhigh, memberidlow;
      memberidhigh = oMgr.GetProfileObject("MemberIDHigh");
      memberidlow = oMgr.GetProfileObject("MemberIDLow");
      // now do something with these two values as a combined PUID
      // to uniquely identify this user in your system
      nickname = oMgr.GetProfileObject("Nickname");
      if (nickname != "") {
            Response.Write( "Hello, " + nickname + ".<P>");
      }else{
            Response.Write ("Hello, .NET Passport user.<P>");
      }
}
%>

See Also

.NET Passport Unique ID | PassportIdentity.HexPUID | PassportIdentity.HasProfile