Step 7: Add User Login and Logoff Pages

In this step, you will add both user login and logoff pages to your Web site. Step-by-step instructions are provided to use the Visual Studio .NET interface and Commerce Server 2002 objects.

  1. Click Start, point to Programs, point to Microsoft Visual Studio .NET, and then click Microsoft Visual Studio .NET.

  2. In the Microsoft Development Environment [design] – Start Page screen, click Open Project.

  3. In the Open Project dialog box, select the NorthwindTraders project, and then click Open.

  4. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Default.aspx window, in the Solution Explorer - NorthwindTraders window, right-click NorthwindTraders, point to Add, and then click Add Web Form.

  5. In the Add new Item - NorthwindTraders dialog box, in the Name box, type Login.aspx, and then click Open.

  6. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Login.aspx window, click View on the toolbar, and then click Toolbox.

  7. In the Toolbox window, in the Web Forms box, drag and drop a Label object onto the form.

  8. In the Properties window, make sure the object you just created is selected, and then do the following:

    Use this To do this
    ID Type userNameLabel.
    Text Type Username:.
    Font Expand this property.
    Bold Select True from the drop-down list.
  9. In the Toolbox window, in the Web Forms, drag and drop another Label object onto the form.

  10. In the Properties window, make sure the object you just created is selected, and then do the following:

    Use this To do this
    ID Type passwordLabel.
    Text Type Password:.
    Font Expand this property.
    Bold Select True from the drop-down list.
  11. In the Toolbox window, in the Web Forms list, drag and drop a Button object onto the form.

  12. In the Properties window, make sure the object you just created is selected, and then do the following:

    Use this To do this
    ID Type loginBtn.
    Text Type Login.
    Font Expand this property.
    Bold Select True from the drop-down list.
  13. In the Toolbox window, in the Web Forms list, drag and drop a TextBox object onto the form, and place it right next to the Username label object.

  14. In the Properties window, make sure the object you just created is selected, and then in the ID property box type userName.

  15. In the Toolbox window, in the Web Forms list, drag and drop another TextBox object onto the form, and place it right next to the Password label object.

  16. In the Properties window, make sure the object you just created is selected, and then do the following:

    Use this To do this
    ID Type password.
    TextMode Select Password from the drop-down list.

    This option will hide the contents of the text box.

  17. In the Toolbox window, in the Web Forms list, drag and drop a Label object onto the form, and place it right under the Login button object.

  18. In the Properties window, make sure the object you just created is selected, and then do the following:

    Use this To do this
    ID Type messageLabel.
    Text Clear the contents.
  19. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Login.aspx window, in the Solution Explorer - NorthwindTraders window, click View Code.

  20. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Login.aspx.cs window, in the using section, add the following lines of code:

    using Microsoft.CommerceServer.Runtime;
    using Microsoft.CommerceServer.Runtime.Diagnostics;
    using Microsoft.CommerceServer.Runtime.Profiles;
    using System.Diagnostics;
    
  21. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Login.aspx window, in the Solution Explorer - NorthwindTraders window, click View Designer.

  22. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Login.aspx window, double-click the Login button.

  23. Press F5 to build the solution. A blank Default.aspx appears in the browser.

  24. Close the Internet browser.

  25. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Login.aspx window, in the LoginBtn_Click event, add the following lines of code:

    AuthenticationInfo authInfo = CommerceContext.Current.AuthenticationInfo;
    Profile prof;
    ProfileContext profContext = CommerceContext.Current.ProfileSystem;
    string pwd;
    
    try
    {
       prof = profContext.GetProfile(userName.Text, "UserObject");
    
       pwd = (string)prof.Properties["GeneralInfo.user_security_password"].Value;
       if(string.Compare(pwd, password.Text)==0)
       {
          //Extract the user's ID
          string userID = (string)prof.Properties["GeneralInfo.user_id"].Value;
          //Set the authorization ticket.
          authInfo.SetAuthTicket(userID, true, 200);
          //Create the profile ticket.
          authInfo.SetProfileTicket(userID, true);
          Response.Redirect("default.aspx");
       }
    
    }
    catch(Exception)
    {
       messageLabel.Text = "Login failed!";
    }
    
  26. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Login.aspx.cs window, in the Solution Explorer - NorthwindTraders window, right-click Login.aspx, and then select Set As Start Page.

  27. Press F5 to build and execute the project.

    The Internet Browser displays the login page.

  28. Close the Internet Browser window.

  29. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Login.aspx.cs window, in the Solution Explorer - NorthwindTraders window, right-click NorthwindTraders, point to Add, and then click Add Web Form.

  30. In the Add new Item - NorthwindTraders dialog box, in the Name box, type Logoff.aspx, and then click Open.

  31. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Logoff.aspx window, in the Solution Explorer - NorthwindTraders window, click View Code.

  32. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Logoff.aspx.cs window, in the Using section, add the following lines of code:

    using Microsoft.CommerceServer.Runtime;
    using Microsoft.CommerceServer.Runtime.Diagnostics;
    using Microsoft.CommerceServer.Runtime.Profiles;
    using System.Diagnostics;
    
  33. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Logoff.aspx window, in the Page_Load() method, add the following lines of code:

    if(null != CommerceContext.Current.AuthenticationInfo.AuthTicket)
    {
       CommerceContext.Current.AuthenticationInfo.AuthTicket.SignOut();
    }
    Response.Redirect("default.aspx");
    

Copyright © 2005 Microsoft Corporation.
All rights reserved.