Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
 Walkthrough: Using ASP.NET Applicat...
Walkthrough: Using ASP.NET Application Services

Updated: November 2007

ASP.NET provides application services on the Web that enable client applications to access user authentication, role, and profile information. This information can be accessed by client applications that are written in different languages and that run on different operating systems. The only requirement is that these clients must be able to communicate through SOAP 1.1 protocol.

This walkthrough is divided into the following parts:

  • Part 1 shows how to configure an ASP.NET Web site to expose the application services.

  • Part 2 shows how to build a Windows console application that accesses a user's authentication, roles, and profile information. In this walkthrough you will build a Windows console application, but the ASP.NET application services are available to any client applications that can send and receive messages in SOAP format.

In order to complete this walkthrough, you need:

  • Visual Studio 2008. You cannot use Microsoft Visual Web Developer 2005 for this walkthrough, because you will create a Windows console application, which is not supported in Visual Web Developer Express Edition.

  • Microsoft SQL Server or SQL Server Express Edition installed on your computer.

This section describes how to expose the application services as part of an ASP.NET Web site so that they can be accessed by any client on the network. The steps described here apply only to the server.

Note:

You must use a file system Web site for this walkthrough. The walkthrough assumes that you are using the ASP.NET Development Server instead of IIS to run the examples. For more information, see Web Servers in Visual Web Developer.

To create the application services Web site

  1. Open Visual Studio 2008.

  2. On the File menu, click New Web Site.

    The New Web Site dialog box appears.

  3. Under Visual Studio installed templates, select ASP.NET Web Site.

  4. In the Location list, select File System.

  5. In the Folder text box, name the Web site WcfApplicationServices.

    Note:

    You can use any name. If you use a different name, note the name so that you can substitute it when it is required later in this walkthrough.

  6. Click OK.

    Visual Studio creates a new ASP.NET Web site and opens the Default.aspx page.

  7. In Solution Explorer, click the name of the Web site, and then press F4 to open the Properties window.

  8. In the Properties window, set Use dynamic ports to False.

    This instructs Visual Studio to specify a fixed port instead of a randomly selected port when it starts the ASP.NET Development Server. For this walkthrough, you must have a fixed port number that you can use when you generate the client proxy classes and configuration files. For more information, see How to: Specify a Port for the ASP.NET Development Server.

    Note:

    You cannot access the Web Site Properties window from the Properties Pages window.

  9. Set Port number to 8080.

    Note:

    You can use any available port. If you use a different port, note the port number so that you can substitute that number for 8080 later in this walkthrough.

ASP.NET membership, role, and profile information is stored in a database. This database is created automatically when it is required. In the next procedure, you will create users and roles for your application, which will automatically create the database.

To create users and role information

  1. Add a Global.asax file to the Web site and in the file, replace the existing Application_Start method with the following code:

    Visual Basic
    <%@ Application Language="VB" %>
    
    <script runat="server">
    
        Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
            ' Code that runs on application startup
            ' Warning. This code is just for test purposes 
            ' to generate a SQL Express database quickly.
            ' In a production environment, you must set 
            ' a SQL standard database separately.
            If Not Roles.RoleExists("Administrators") Then
                Roles.CreateRole("Administrators")
            End If
            If Not Roles.RoleExists("Friends") Then
                Roles.CreateRole("Friends")
            End If
        End Sub
    
    
    </script>
    
    
    C#
    
    <%@ Application Language="C#" %>
    
    <script runat="server">
    
        void Application_Start(object sender, EventArgs e) 
        {
            // Code that runs on application startup
            // Warning. This code is just for test purposes 
            // to generate a SQL Express database quickly.
            // In a production environment, you must set   3-25
            // a SQL standard database separately.
            if (!Roles.RoleExists("Administrators")){
                Roles.CreateRole("Administrators");
    
            }
            if (!Roles.RoleExists("Friends")){
                Roles.CreateRole("Friends");
    
            }
    
        }
    
    
    </script>
    
    
    

    The first time that you run the Web application, the code creates the Aspnetdb.mdf local database and adds the roles Administrators and Friends to it. This database is used to store user credentials, roles, and profile information.

  2. Open the Default.apx page and add the following markup it. This adds links for the login page, the profile information page, and the new user page. You will add these pages later in the walkthrough.

    Visual Basic
    <%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html >
    <head id="Head1" runat="server">
        <title>Application Services Home Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <h2>Enter Users' Information</h2>
    
            The following selections enable you to create users, and assign them roles and
            profile information.
    
            <p>
                <asp:Label ID="LoggedId"  Font-Bold="true" ForeColor="red" runat="server"/>
            </p>
    
            <table border="1">
                <tr>
                    <td align="left">Login to change profile</td>
                    <td align="left"><asp:LoginStatus ID="LoginStatus1" runat="server" /></td>
                </tr>
                <tr>
                    <td align="left">Define profile information (you must login first)</td>
                    <td align="left"><a href="ProfileInfo.aspx" target="_self">Profile Information</a></td>
                </tr>
                <tr>
                    <td align="left">Create user and assign role</td>
                    <td align="left"><a href="CreateUser.aspx"target="_self">New User</a></td>
                </tr>
    
             </table>
        </div>
    
        </form>
    </body>
    </html>
    
    
    C#
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html >
    <head runat="server">
        <title>Application Services Home Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <h2>Enter Users' Information</h2>
    
        The following selections enable you to create users, and assign them roles and
            profile information.
    
            <p>
                <asp:Label ID="LoggedId"  Font-Bold="true" ForeColor="red" runat="server"/>
            </p>
    
            <table border="1">
                <tr>
                    <td align="left">Login to change profile</td>
                    <td align="left"><asp:LoginStatus ID="LoginStatus1" runat="server" /></td>
                </tr>
                <tr>
                    <td align="left">Define profile information (you must login first)</td>
                    <td align="left"><a href="Profile.aspx" target="_self">Profile Information</a></td>
                </tr>
                <tr>
                    <td align="left">Create user and assign role</td>
                    <td align="left"><a href="CreateUser.aspx"target="_self">New User</a></td>
                </tr>
    
             </table>
        </div>
    
        </form>
    </body>
    </html>
    
    
  3. In the Default.aspx code-behind file, add code in the Page_Load method that checks whether the user is authenticated.

    The following example shows how to check for an authenticated user.

    Visual Basic
    Imports System
    Imports System.Web
    
    Partial Public Class _Default
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If HttpContext.Current.User.Identity.IsAuthenticated Then
                LoggedId.Text = HttpContext.Current.User.Identity.Name + " you are logged in"
            End If
    
        End Sub
    End Class
    
    
    C#
    using System;
    using System.Web;
    
    public partial class _Default : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
    
            if (HttpContext.Current.User.Identity.IsAuthenticated) {
                LoggedId.Text = HttpContext.Current.User.Identity.Name +
                    " you are logged in";
            } else
                LoggedId.Text = "Not Logged in!";
    
        }
    }
    
    
  4. Add a page named Login.aspx. Make sure that Place code in separate file is selected.

  5. Add a Login control to the Login.aspx file.

    The following example shows the markup for the Login.aspx file:

    Visual Basic
    <%@ Page Language="VB" AutoEventWireup="true" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html >
    <head id="Head1" runat="server">
        <title>Login Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Login ID="Login1"   runat="server" />
        </div>
        </form>
    </body>
    </html>
    
    
    C#
    <%@ Page AutoEventWireup="true" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html >
    <head id="Head1" runat="server">
        <title>Login Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
    <%--    The Login control does all the work  --%>        
    <asp:Login ID="Login1"   runat="server"/>
        </div>
        </form>
    </body>
    </html>
    
    
  6. Add a page named Profile.aspx, and make sure that Place code in separate file is selected.

  7. Add the following markup to the Profile.aspx page:

    Visual Basic
    <%@ Page Language="VB" AutoEventWireup="true" CodeFile="Profile.aspx.vb" Inherits="_Profile" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html >
    <head id="Head1" runat="server">
        <title>Profile Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <h3>Current Authenticated User Profile Information</h3> 
    
            <a href="Default.aspx">back to default page</a>
    
            <h4>Read Profile Information</h4>
            <table>
                <tr>
                    <td align="left">User Name</td>
                    <td align="left">
                        <asp:Label ID="Label1" runat="server" Text="Label"/>
                    </td>
                </tr>
                <tr>
                    <td align="left">User Roles</td>
                    <td align="left">
                        <asp:Label ID="Label2" runat="server" Text="Label"/>
                    </td>
                </tr>
                <tr>
                    <td align="left">First Name</td>
                    <td>
                        <asp:Label ID="Label3" runat="server" Text="Label"/>
                    </td>
                </tr>
                <tr>
                    <td align="left">Last Name</td>
                    <td>    
                        <asp:Label ID="Label4" runat="server" Text="Label"/>
                    </td>
                </tr>
                <tr>
                    <td align="left">ID#</td>
                    <td>    
                        <asp:Label ID="Label5" runat="server" Text="Label"/>
                    </td>
                </tr>
    
            </table>
            <asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
                Text="Read Profile Information" />
            
            <hr />
            
            <h3>Update Profile Information </h3>
    
            <table>
                <tr>
                    <td align="left">First Name</td>
                    <td align="left"><asp:TextBox ID="TextBox1" runat="server"/></td>
                </tr>
                <tr>
                    <td align="left">Last Name</td>
                    <td align="left"><asp:TextBox ID="TextBox2" runat="server"/></td>
                </tr>
                <tr>
                    <td align="left">ID#</td>
                    <td align="left"><asp:TextBox ID="TextBox3" runat="server"/></td>
                </tr>
               
            </table>
            
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text="Update Profile Data" />
            
        </div>
        </form>
    </body>
    </html>
    
    
    C#
    <%@ Page Language="C#" AutoEventWireup="true" 
    CodeFile="Profile.aspx.cs" Inherits="ProfileInformation" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html >
    <head id="Head1" runat="server">
        <title>Profile Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <h3>Current Authenticated User Profile Information</h3> 
    
            <a href="Default.aspx">back to default page</a>
    
            <h4>Read Profile Information</h4>
            <table>
                <tr>
                    <td align="left">User Name</td>
                    <td align="left">
                        <asp:Label ID="Label1" runat="server" Text="Label"/>
                    </td>
                </tr>
                <tr>
                    <td align="left">User Roles</td>
                    <td align="left">
                        <asp:Label ID="Label2" runat="server" Text="Label"/>
                    </td>
                </tr>
                <tr>
                    <td align="left">First Name</td>
                    <td>
                        <asp:Label ID="Label3" runat="server" Text="Label"/>
                    </td>
                </tr>
                <tr>
                    <td align="left">Last Name</td>
                    <td>    
                        <asp:Label ID="Label4" runat="server" Text="Label"/>
                    </td>
                </tr>
                <tr>
                    <td align="left">ID#</td>
                    <td>    
                        <asp:Label ID="Label5" runat="server" Text="Label"/>
                    </td>
                </tr>
    
            </table>
            <asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
                Text="Read Profile Information" />
            
            <hr />
            
            <h3>Update Profile Information </h3>
    
            <table>
                <tr>
                    <td align="left">First Name</td>
                    <td align="left"><asp:TextBox ID="TextBox1" runat="server"/></td>
                </tr>
                <tr>
                    <td align="left">Last Name</td>
                    <td align="left"><asp:TextBox ID="TextBox2" runat="server"/></td>
                </tr>
                <tr>
                    <td align="left">ID#</td>
                    <td align="left"><asp:TextBox ID="TextBox3" runat="server"/></td>
                </tr>
               
            </table>
            
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text="Update Profile Data" />
            
        </div>
        </form>
    </body>
    </html>
    
    

    The Profile page contains Label controls that are used to display the user name and role, and TextBox controls that are used to change the users name and ID.

  8. In the code-behind file for the Profile.aspx page, add the following code:

    Visual Basic
    Imports System
    Imports System.Data
    Imports System.Configuration
    Imports System.Collections
    Imports System.Web
    Imports System.Web.Security
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.WebControls.WebParts
    Imports System.Web.UI.HtmlControls
    
    Partial Public Class _Profile
        Inherits System.Web.UI.Page
    
        Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    
               Dim Profile As ProfileCommon = TryCast(HttpContext.Current.Profile, ProfileCommon)
    
            If HttpContext.Current.User.Identity.IsAuthenticated Then
    
                Label1.Text = HttpContext.Current.User.Identity.Name
                Dim roles As String() = _
                    System.Web.Security.Roles.GetRolesForUser()
    
                Label2.Text = ""
                For Each r As String In roles
                    Label2.Text += r + " "
                Next
    
                Label3.Text = Profile.FirstName()
                Label4.Text = Profile.LastName
    
                Label5.Text = Profile.EmployeeId
            Else
                Label1.Text = "User is not Authenticated"
                Label1.ForeColor = System.Drawing.Color.Red
            End If
        End Sub
    
        Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
            If HttpContext.Current.User.Identity.IsAuthenticated Then
                Label1.Text = HttpContext.Current.User.Identity.Name
                Dim roles As String() = _
                    System.Web.Security.Roles.GetRolesForUser()
                Label2.Text = ""
                For Each r As String In roles
                    Label2.Text += r + " "
                Next
                Label3.Text = Profile.FirstName
                Label4.Text = Profile.LastName
                Label5.Text = Profile.EmployeeId
            Else
                Label1.Text = "User is not Authenticated"
                Label1.ForeColor = System.Drawing.Color.Red
            End If
        End Sub
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            If HttpContext.Current.User.Identity.IsAuthenticated Then
                Profile.FirstName = TextBox1.Text
                Profile.LastName = TextBox2.Text
                Profile.EmployeeId = TextBox3.Text
            End If
        End Sub
    End Class
    
    
    C#
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class ProfileInformation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
         ProfileCommon Profile = HttpContext.Current.Profile
                                      as ProfileCommon;
         
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                Label1.Text = HttpContext.Current.User.Identity.Name;
                string[] roles = Roles.GetRolesForUser();
                Label2.Text = "";
                foreach (string r in roles)
                {
                    Label2.Text += r + " ";
                }
    
                Label3.Text = Profile.FirstName;
                Label4.Text = Profile.LastName;
                Label5.Text = Profile.EmployeeId;
    
            }
            else
            {
                Label1.Text = "User is not Authenticated";
                Label1.ForeColor = System.Drawing.Color.Red;
            }
        }
    
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                Label1.Text = HttpContext.Current.User.Identity.Name;
                string[] roles = Roles.GetRolesForUser();
                Label2.Text = "";
                foreach (string r in roles)
                {
                    Label2.Text += r + " ";
                }
    
                Label3.Text = Profile.FirstName;
                Label4.Text = Profile.LastName;
                Label5.Text = Profile.EmployeeId;
    
            }
            else
            {
                Label1.Text = "User is not Authenticated";
                Label1.ForeColor = System.Drawing.Color.Red;
            }
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                Profile.FirstName = TextBox1.Text;
                Profile.LastName = TextBox2.Text;
                Profile.EmployeeId = TextBox3.Text;
            }
        }
    }
    
    
    Note:

    In the previous example, the class name in the code-behind file has been changed to _Profile to prevent a conflict with the Profile. instance.

    The code in this page enables you to obtain and change user profile information.

    Note:

    The project will not compile until you enable profile properties for the Web site. You will do this in the next procedure.

  9. Add a page named CreateUser.aspx page and add the following markup to it:

    Visual Basic
    <%@ Page Language="VB" AutoEventWireup="true" CodeFile="CreateUser.aspx.vb" Inherits="CreateUser" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html >
    <head id="Head1" runat="server">
        <title>Add New User</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
    
            <h2>Add New User</h2>
    
            <a href="Default.aspx">back to default page</a>
    
            <asp:CreateUserWizard ID="CreateUserWizard1" runat="server"
              OnCreatedUser="On_CreatedUser">
                <wizardsteps>
                    <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server"  />
                    <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server"  />
                </wizardsteps>
            </asp:CreateUserWizard>
            <p> 
                Check the following box to assign the user to the Manager role.
                Otherwise, the user will be assigned to the friends role by default. 
            </p>
            <span style="font-weight:bold; color:Red">Manager</span> 
            <asp:CheckBox ID="CheckBox1" runat="server" />
    
        </div>
        </form>
    </body>
    </html>
    
    
    Visual Basic
    Imports System
    Imports System.Web
    Imports System.Web.Security
    
    Partial Public Class CreateUser
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    
        End Sub
    
        Protected Sub On_CreatedUser(ByVal sender As Object, ByVal e As EventArgs)
            Dim userName As String = CreateUserWizard1.UserName
            If CheckBox1.Checked Then
                HttpContext.Current.Response.Write(userName)
                Roles.AddUserToRole(userName, "Managers")
            Else
                Roles.AddUserToRole(userName, "Friends")
            End If
    
            CheckBox1.Visible = False
    
            HttpContext.Current.Response.Redirect("~/default.aspx")
        End Sub
    End Class
    
    
    C#
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateUser.aspx.cs" Inherits="CreateUser" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html >
    <head runat="server">
       <title>Add New User</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
    
            <h2>Add New User</h2>
    
            <a href="Default.aspx">back to default page</a>
    
            <asp:CreateUserWizard ID="CreateUserWizard1" runat="server"
              OnCreatedUser="On_CreatedUser">
                <wizardsteps>
                    <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server"  />
                    <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server"  />
                </wizardsteps>
            </asp:CreateUserWizard>
            <p> 
                Check the following box to assign the user to the Managers role.
                Otherwise, the user will be assigned to the friends role by default. 
            </p>
            <span style="font-weight:bold; color:Red">Managers</span> 
            <asp:CheckBox ID="CB_manager" runat="server" />
    
        </div>
        </form>
    </body>
    </html>
    
    
    C#
    using System;
    using System.Web;
    using System.Web.Security;
    
    public partial class CreateUser : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
    
        }
        protected void On_CreatedUser(object sender, EventArgs e) {
            string userName = CreateUserWizard1.UserName;
            if (CB_manager.Checked) {
                HttpContext.Current.Response.Write(userName);
                Roles.AddUserToRole(userName, "Managers");
            } else
                Roles.AddUserToRole(userName, "Friends");
    
            CB_manager.Visible = false;
    
            HttpContext.Current.Response.Redirect("~/default.aspx");
        }
    
    }
    
    
    
  10. In the code-behind file for the CreateUser.aspx page, add the following code:

    This page enables you to create users and assign them to a role.

The next step is to enable forms authentication, roles, and profile properties in the Web site. You do this with configuration settings in the Web.config file.

To configure authentication, roles, and profile properties

  1. Open the Web site's Web.config file.

  2. In the system.web group, change the default windows authentication to forms, as shown in the following example:

    <authentication mode="Forms" />
    
  3. In the system.web group, configure the roles service by adding the roleManager element, as shown in the following example:

    <roleManager enabled="true"/>
    
  4. In the system.web group, configure the profile service through the profile section and its properties element as shown in the following example:

    <profile enabled="true">
      <properties>
        <add name="FirstName"   type="String"/>
        <add name="LastName"    type="String"/>
        <add name="EmployeeId"  type="String"/>
      </properties>
    </profile>
    

    This profile setting defines three properties that will be managed by the ASP.NET profile service. At run time, ASP.NET will dynamically create a class of type ProfileCommon that contains these properties.

    The following example shows a portion of the Web.config file with all the required changes.

    <system.web>
    <!-- Other settings. -->
    <authentication mode="Forms" />
    <roleManager enabled="true"/>
    <profile enabled="true">
      <properties>
        <add name="FirstName"   type="String"/>
        <add name="LastName"    type="String"/>
        <add name="EmployeeId"  type="String"/>
      </properties>
    </profile>
    </system.web>
    

You can now create user information that you will use later to log in.

To create users and assign profile information

  1. In Solution Explorer, select the Default.aspx page then press CTRL+F5 to run the page.

    The page is displayed in the browser with the following URL:

    http://localhost:8080/WcfApplicationServices/Default.aspx
    
  2. Click New User.

    The CreateUser.aspx page is displayed.

  3. Create some users and assign them to the predefined roles. To do so, enter the user's credentials then click Create User.

    Record the user names and passwords that you have created. You will need them to assign or change the users' profile information.

    For example, create a user with a user name "joeA" and check the Administrator check box so that joeA is a member of the Administrators role. Create a second user with a user name "joeNA", and do not check the Administrator check box. The user joeNA will be a member of the Friends role but not of the Administrators role. The Friends and Administrators roles are created by code that you added earlier to the Application_Start method in the Global.asax file.

    After a user is created, you are redirected to the Default.aspx page.

  4. In the Default.aspx page, click Login.

    The Login.aspx page is displayed.

  5. Log in using the credentials of one of the users you created earlier.

    If your login is successful, you are redirected to the Default.aspx page.

  6. Click Profile Information.

    The Profile.aspx page is displayed.

  7. Enter or update the profile information of the logged-in user by entering the first name, last name, and identification number.

  8. Click Update Profile Data.

  9. Click Read Profile Information.

    The information that you just entered is displayed. This step lets you make sure that profile properties are working.

You are finished creating users and profile information. You will now make this information available to client applications.

You can now expose the user information you have created by using ASP.NET application services. Before you can access the user's credential and profile information from the client, you must create mapping files (.svc) that point to the application services. This makes the services available to any application that is running on a platform that can send and receive SOAP messages. You must also configure the Web site so the application services are exposed on the network.

To create application services mapping files

  1. Add a WCF service file (.svc) to the Web site and name it MyAuthenSvcWrap.svc.

  2. Replace the existing @ ServiceHost directive with the following directive, which references the System.Web.ApplicationServices..::.AuthenticationService class:

    Visual Basic
    <%@ ServiceHost Language="VB"
      Service="System.Web.ApplicationServices.AuthenticationService" 
      Factory="System.Web.ApplicationServices.ApplicationServicesHostFactory" 
      Debug="true" %>
    

    C#
    <%@ ServiceHost Language="C#"
      Service="System.Web.ApplicationServices.AuthenticationService" 
      Factory="System.Web.ApplicationServices.ApplicationServicesHostFactory" 
      Debug="true" %>
    
    

    This example service does not implement a custom authentication service. Instead, it calls the built in System.Web.ApplicationServices..::.AuthenticationService class.

  3. Delete the interface and class files in the App_Code directory (App_Code\MyAuthenSvcWrap and App_Code\IMyAuthenSvcWrap).

    The interface and class files were created when the .svc file was added to the project. You do not need these files, because you are using the System.Web.ApplicationServices..::.AuthenticationService service and not implementing a custom service.

  4. Add another WCF service file (.svc) to the Web site and name it MyRoleSvcWrap.svc.

  5. Open the MyRoleSvcWrap.svc file and replace its contents with the following @ ServiceHost directive, which creates a reference to the System.Web.ApplicationServices..::.RoleService class:

    Visual Basic
    <%@ ServiceHost Language="VB" 
       Service="System.Web.ApplicationServices.RoleService" 
       Factory="System.Web.ApplicationServices.ApplicationServicesHostFactory" %>
    

    C#
    <%@ ServiceHost Language="C#" 
      Service="System.Web.ApplicationServices.RoleService" 
      Factory="System.Web.ApplicationServices.ApplicationServicesHostFactory" %>
    
  6. In the App_Code directory, delete the interface and class files for the MyRoleSvcWrap service.

  7. Add another WCF service file (.svc) to the Web site and name it MyProfileSvcWrap.svc.

  8. Open the MyProfileSvcWrap.svc file and replace its contents with the following directive, which creates a reference to the System.Web.ApplicationServices..::.ProfileService class:

    Visual Basic
    <%@ ServiceHost Language="VB"
      Service="System.Web.ApplicationServices.ProfileService" 
      Factory ="System.Web.ApplicationServices.ApplicationServicesHostFactory" %>
    

    C#
    <%@ ServiceHost Language="C#"
    Service="System.Web.ApplicationServices.ProfileService" 
    
    Factory ="System.Web.ApplicationServices.ApplicationServicesHostFactory" %>
    
  9. In the App_Code directory, delete the interface and class files for the MyProfileSvcWrap service.

  10. Save and close all .svc files.

You can now configure the Web application to expose the application services.

To configure the application services

  1. Open or switch to the Web.config file.

  2. Below the configSections section, add a new system.web.extensions section.

  3. In the scripting element, enable the application services through the webServices section.

    The following example shows the finished system.web.extensions element.

    </configSections>
    
    <system.web.extensions>
      <scripting>
        <webServices>
          <authenticationService enabled="true"
           requireSSL = "false"/>
          <profileService
              enabled="true"
              readAccessProperties="FirstName,LastName,EmployeeId"/>
          <roleService enabled="true"/>
        </webServices>
      </scripting>
    </system.web.extensions>
    
      <appSettings/>
    
  4. As a child of the system.serviceModel section, add a serviceHostingEnvironment element and set its aspNetCompatibilityEnabled to true.

    The following example shows the finished serviceHostingEnvironment element.

    </runtime>
     <system.serviceModel>
       <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    
  5. In the system.serviceModel group, configure the application services so that they can be accessed by client applications using the SOAP protocol. To do so, follow these steps:

    1. In the system.serviceModel element, find the <behaviors><serviceBehaviors><behavior> element whose name attribute is "MyAuthenSvcWrapBehavior".

    2. Change the behavior element's name attribute value from "MyAuthenSvcWrapBehavior" to "AppServiceBehaviors". In this walkthrough, you configure the application to use the AppServiceBehaviors behavior for the all three services.

    3. Delete the behavior elements whose names are "MyRoleSvcWrapBehavior" and "MyProfileSvcWrapBehavior" from the <system.serviceModel><behaviors><serviceBehaviors> element.

    The following example shows the finished <system.serviceModel> section.

    </runtime>
     <system.serviceModel>
       <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
      <behaviors>
       <serviceBehaviors>
        <behavior name="AppServiceBehaviors">
         <serviceMetadata httpGetEnabled="true" />
         <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
       </serviceBehaviors>
      </behaviors>
    
  6. In the <system.serviceModel><services><service> element that is named "MyAuthenSvcWrapBehavior", change the behavior element's behaviorConfiguration attribute value from "MyAuthenSvcWrapBehavior" to "AppServiceBehaviors".

  7. Change the name attribute value from "MyAuthenSvcWrap" to "System.Web.ApplicationServices.AuthenticationService".

  8. Configure the <service><endpoint> element by doing the following:

    1. Delete the address attribute.

    2. Change the value of the binding attribute value from "wsHttpBinding" to "basicHttpBinding".

    3. Change the value of the contract attribute from "IMyAuthenSvcWrap" to "System.Web.ApplicationServices.AuthenticationService".

    4. Add the bindingNamespace attribute and set it to "http://asp.net/ApplicationServices/v200".

    5. Delete the child identity element.

    The following example shows the markup for the authentication service element.

    <service
      behaviorConfiguration="AppServiceBehaviors" 
      name="System.Web.ApplicationServices.AuthenticationService">
        <endpoint binding="basicHttpBinding" 
      contract="System.Web.ApplicationServices.AuthenticationService"
          bindingNamespace="http://asp.net/ApplicationServices/v200"       
                   >
        </endpoint>
    </service>
    

    For more information, see <service> Element.

  9. Repeat step 8 for the MyAuthenSvcWrap element.

    The following example shows the complete system.serviceModel element.

    <system.serviceModel>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
      <behaviors>
       <serviceBehaviors>
        <behavior name="AppServiceBehaviors">
         <serviceMetadata httpGetEnabled="true" />
         <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
       </serviceBehaviors>
      </behaviors>
      <services>
        <service 
            behaviorConfiguration="AppServiceBehaviors" 
            name="System.Web.ApplicationServices.AuthenticationService">
          <endpoint  binding="basicHttpBinding" 
              contract="System.Web.ApplicationServices.AuthenticationService"
               bindingNamespace="http://asp.net/ApplicationServices/v200"       
                   />
        </service>
        <service 
            behaviorConfiguration="AppServiceBehaviors" 
            name="System.Web.ApplicationServices.RoleService">
          <endpoint  binding="basicHttpBinding" 
              contract="System.Web.ApplicationServices.RoleService"
              bindingNamespace="http://asp.net/ApplicationServices/v200"       
                   />
        </service>
        <service 
            behaviorConfiguration="AppServiceBehaviors" 
            name="System.Web.ApplicationServices.ProfileService">
          <endpoint  binding="basicHttpBinding"
              contract="System.Web.ApplicationServices.ProfileService"
              bindingNamespace="http://asp.net/ApplicationServices/v200"
                   />
        </service>
      </services>
     </system.serviceModel>
    </configuration>
    

You can now run the Web site in order to activate the authentication services that will be used by the Windows client application. This also enables you to verify that the application service infrastructure is working correctly.

To activate the Web site to expose the application services

  • In Solution Explorer, right-click the MyAuthenSvcWrap.svc file and then click View in Browser.

    The Web service is invoked and a page is displayed in the browser that gives you instructions for testing