
Exposing the Application Services
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.
To create the application services Web site
Open Visual Studio 2008.
On the File menu, click New Web Site.
The New Web Site dialog box is displayed.
Under Visual Studio installed templates, select ASP.NET Web Site.
In the Location list, select File System.
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. |
Click OK.
Visual Studio creates a new ASP.NET Web site and opens the Default.aspx page.
In the View menu, click Other Windows, and then click Properties Window.
In Solution Explorer, click the name of the Web site.
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. |
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 first accessed. In the next procedure, you will write the code that creates users and roles for the Web site. The first time that the Web site is accessed, the database is automatically created. In addition, two roles (Managers and Friends) are added to it.
To create users and role information
Add a Global.asax file to the Web site.
Open the Global.asax file and replace the existing Application_Start method with the following code:
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("Managers") Then
Roles.CreateRole("Managers")
End If
If Not Roles.RoleExists("Friends") Then
Roles.CreateRole("Friends")
End If
End Sub
void Application_Start(object sender, EventArgs e) {
if (!Roles.RoleExists("Managers")) {
Roles.CreateRole("Managers");
}
if (!Roles.RoleExists("Friends")) {
Roles.CreateRole("Friends");
}
}
The first time that the Web site is accessed, the code creates the Aspnetdb.mdf database in the App_Data folder and adds the roles Managers and Friends to it. This database is used to store user credentials, roles, and profile information.
Open the Default.aspx page and replace the markup that is in the page with the following markup.
<%@ 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>
<%@ 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="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>
This markup creates links for the login page, the profile information page, and the new user page. You will add these pages later in the walkthrough.
In the Default.aspx code-behind file, add the following code in the Page_Load method.
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
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!";
}
}
The code checks whether the user is authenticated.
Add a page named Login.aspx.
Add a Login control to the Login.aspx file.
The following example shows the markup for the Login.aspx file:
<%@ 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>
<%@ 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>
Add a page named ProfileInfo.aspx, and make sure that Place code in separate file is selected.
Add the following markup to the ProfileInfo.aspx page:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ProfileInfo.aspx.vb" Inherits="ProfileInfo" %>
<!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">Phone #</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">Phone Number</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>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProfileInfo.aspx.cs" Inherits="ProfileInfo" %>
<!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>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="Lbl_UserName" runat="server" Font-Bold="true"/>
</td>
</tr> <tr>
<td align="left">User Roles:</td>
<td align="left">
<asp:Label ID="Lbl_Roles" runat="server" Font-Bold="true" />
</td>
</tr> <tr>
<td align="left">First Name:</td>
<td>
<asp:Label ID="Lbl_FirstName" runat="server" Font-Bold="true"/>
</td>
</tr> <tr>
<td align="left">Last Name:</td>
<td>
<asp:Label ID="Lbl_LastName" runat="server" Font-Bold="true" />
</td>
</tr> <tr>
<td align="left">Phone #:</td>
<td>
<asp:Label ID="Lbl_Phone" runat="server" Font-Bold="true"/>
</td>
</tr>
</table>
<asp:Button ID="But_ReadProfile" runat="server" onclick="But_ReadProfile_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="TB_FirstName" runat="server"/></td>
</tr> <tr>
<td align="left">Last Name</td>
<td align="left"><asp:TextBox ID="TB_LastName" runat="server"/></td>
</tr> <tr>
<td align="left">Phone Number</td>
<td align="left"><asp:TextBox ID="TB_phoneNumber" runat="server"/></td>
</tr>
</table>
<asp:Button ID="But_UpdateProfile" runat="server" onclick="But_UpdateProfile_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 user name and phone number.
In the code-behind file for the ProfileInfo.aspx page, add the following code:
Imports System
Imports System.Web
Imports System.Web.Security
Partial Class ProfileInfo
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.PhoneNumber
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.PhoneNumber
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.PhoneNumber = TextBox3.Text
End If
End Sub
End Class
public partial class ProfileInfo : 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) {
Lbl_UserName.Text = HttpContext.Current.User.Identity.Name;
string[] roles = Roles.GetRolesForUser();
Lbl_Roles.Text = "";
foreach (string r in roles) {
Lbl_Roles.Text += r + " ";
}
Lbl_FirstName.Text = Profile.FirstName;
Lbl_LastName.Text = Profile.LastName;
Lbl_Phone.Text = Profile.PhoneNumber;
} else {
Lbl_UserName.Text = "User is not Authenticated";
Lbl_UserName.ForeColor = System.Drawing.Color.Red;
}
}
protected void But_ReadProfile_Click(object sender, EventArgs e) {
if (HttpContext.Current.User.Identity.IsAuthenticated) {
Lbl_UserName.Text = HttpContext.Current.User.Identity.Name;
string[] roles = Roles.GetRolesForUser();
Lbl_Roles.Text = "";
foreach (string r in roles) {
Lbl_Roles.Text += r + " ";
}
Lbl_FirstName.Text = Profile.FirstName;
Lbl_LastName.Text = Profile.LastName;
Lbl_Phone.Text = Profile.PhoneNumber;
} else {
Lbl_UserName.Text = "User is not Authenticated";
Lbl_UserName.ForeColor = System.Drawing.Color.Red;
}
}
protected void But_UpdateProfile_Click(object sender, EventArgs e) {
if (HttpContext.Current.User.Identity.IsAuthenticated) {
Profile.FirstName = TB_FirstName.Text;
Profile.LastName = TB_LastName.Text;
Profile.PhoneNumber = TB_phoneNumber.Text;
}
}
}
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. |
Add a page named CreateUser.aspx and add the following markup to it:
<%@ 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>
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
<%@ 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>
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");
}
}
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
Open the Web site's Web.config file.
In the authentication element in the system.web section, change the default windows authentication to use forms, as shown in the following example:
<authentication mode="Forms" />
In the system.web section, configure the roles service by adding the roleManager element, as shown in the following example:
<roleManager enabled="true"/>
In the system.web section, 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="PhoneNumber" type="String"/>
</properties>
</profile>
This profile setting defines three properties (FirstName, LastName, and PhoneNumber) 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 part of the Web.config file that has 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="PhoneNumber" type="String"/>
</properties>
</profile>
<!-- Other settings. -->
</system.web>
You can now create user information that you will use later to log in.
To create users and assign profile information
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
Click New User.
The CreateUser.aspx page is displayed.
Create some users and assign them to the predefined roles. To do this, 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 that has a user name "joeM" and select the Managers radio button so that joeM is a member of the Managers role. Create a second user that has a user name "joeNA", and select the Friends radio button. The user joeNA will be a member of the Friends role but not of the Managers role. The Friends and Managers 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.
In the Default.aspx page, click Login.
The Login.aspx page is displayed.
Log in using the credentials of one of the users that you created earlier.
If your login is successful, you are redirected to the Default.aspx page.
Click Profile Information.
The Profile.aspx page is displayed.
Enter or update the profile information for the logged-in user by entering the first name, last name, and phone number.
Click Update Profile Data.
Click Read Profile Information.
The information that you just entered is displayed. This step lets you make sure that profile properties are working.
Return to the Default.aspx page.
Log out from the user account for the user in the manager role.
Log in as a user in the Friends role.
For example, log in as joeNA.
Enter profile information for the logged-in user.
You are finished creating users, roles, and profile information. You will now make this information available to client applications.
Mapping and Configuring the Application Services
You can now expose the user information that 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
Add a WCF service file (.svc) to the Web site and name it MyAuthenticationSvcWrap.svc.
Open the MyAuthenticationSvcWrap.svc file and replace the existing @ ServiceHost directive with the following directive, which references the System.Web.ApplicationServices..::.AuthenticationService class:
<%@ ServiceHost Language="VB"
Service="System.Web.ApplicationServices.AuthenticationService"
Debug="true" %>
<%@ ServiceHost Language="C#"
Service="System.Web.ApplicationServices.AuthenticationService"
Debug="true" %>
This example service does not implement a custom authentication service. Instead, it calls (wraps) the built-in System.Web.ApplicationServices..::.AuthenticationService class.
Delete the interface and class files for the MyAuthenticationSvcWrap service in the App_Code directory (App_Code\MyAuthenticationSvcWrap and App_Code\IMyAuthenticationSvcWrap).
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 are not implementing a custom service.
Add another WCF service file (.svc) to the Web site and name it MyRoleSvcWrap.svc.
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:
<%@ ServiceHost Language="VB"
Service="System.Web.ApplicationServices.RoleService" %>
<%@ ServiceHost Language="C#"
Service="System.Web.ApplicationServices.RoleService" %>
In the App_Code directory, delete the interface and class files for the MyRoleSvcWrap service.
Add another WCF service file (.svc) to the Web site and name it MyProfileSvcWrap.svc.
Open the MyProfileSvcWrap.svc file and replace its contents with the following directive, which creates a reference to the System.Web.ApplicationServices..::.ProfileService class:
<%@ ServiceHost Language="VB"
Service="System.Web.ApplicationServices.ProfileService"
%>
<%@ ServiceHost Language="C#"
Service="System.Web.ApplicationServices.ProfileService"
%>
In the App_Code directory, delete the interface and class files for the MyProfileSvcWrap service.
Save and close all .svc files
Build the WcfApplicationServices project to make sure that the markup, code, and configuration settings compile.
You can now configure the Web application to expose the application services.
To configure the application services
Open or switch to the Web.config file.
Between the configSections element and appSettings element, add a new system.web.extensions element.
Add a scripting element to the system.web.extensions element.
In the scripting element, enable the authentication, profile, and roles services through the webServices section.
The following example shows the finished system.web.extensions element.
<configSections>
...
</configSections>
<system.web.extensions>
<scripting>
<webServices>
<authenticationService enabled="true"
requireSSL = "false"/>
<profileService
enabled="true"
readAccessProperties="FirstName,LastName,PhoneNumber"/>
<roleService enabled="true"/>
</webServices>
</scripting>
</system.web.extensions>
<appSettings/>
Note: |
|---|
In a live Web site, you should set the requireSSL attribute to true. |
As a child of the system.serviceModel section, add a serviceHostingEnvironmentelement and set its aspNetCompatibilityEnabled attribute to true.
The following example shows the finished serviceHostingEnvironment element.
<runtime>
...
</runtime>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<behaviors>
...
</behaviors>
In the system.serviceModel group, configure the application services so that they can be accessed by client applications by using the SOAP protocol. To do this, follow these steps:
In the system.serviceModel element, find the <behaviors><serviceBehaviors><behavior> element whose name attribute is MyAuthenticationSvcWrapBehavior.
Change the behavior element's name attribute value from MyAuthenticationSvcWrapBehavior to AppServiceBehaviors. In this walkthrough, you configure the application to use the AppServiceBehaviors behavior for the all three services.
Delete the behavior elements whose names are MyRoleSvcWrapBehavior and MyProfileSvcWrapBehavior from the <system.serviceModel><behaviors><serviceBehaviors> element.
In the <system.serviceModel><services><service> element that has a behaviorConfiguration attribute whose value is MyAuthenticationSvcWrapBehavior, do the following:
Change the behaviorConfiguration attribute value from MyAuthenticationSvcWrapBehavior to AppServiceBehaviors.
Change the name attribute value from MyAuthenticationSvcWrap to System.Web.ApplicationServices.AuthenticationService.
Configure the <service><endpoint> element by doing the following:
Delete the address attribute.
Change the value of the binding attribute value from wsHttpBinding to basicHttpBinding.
Change the value of the contract attribute from IMyAuthenticationSvcWrap to System.Web.ApplicationServices.AuthenticationService.
Add the bindingNamespace attribute and set it to http://asp.net/ApplicationServices/v200.
Delete the child identity element.
Delete the endpoint element whose address attribute is set to mex.
The following example shows the markup for the authentication service element.
<service behaviorConfiguration="AppServiceBehaviors"
name="System.Web.ApplicationServices.AuthenticationService">
<endpoint binding="basicHttpBinding"
bindingNamespace="http://asp.net/ApplicationServices/v200"
contract="System.Web.ApplicationServices.AuthenticationService"/>
</endpoint>
</service>
For more information, see <service> element and <endpoint> element.
Repeat step 7 for the service elements that have a behaviorConfiguration attribute whose values are MyRoleSvcWrapBehavior and MyProfileSvcWrapBehavior. Substitute the following values for the name and contract attributes:
The following example shows the complete system.serviceModel element.
<configuration>
...
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<!-- this enables the WCF AuthenticationService endpoint -->
<service behaviorConfiguration="AppServiceBehaviors"
name="System.Web.ApplicationServices.AuthenticationService">
<endpoint binding="basicHttpBinding"
bindingNamespace="http://asp.net/ApplicationServices/v200"
contract="System.Web.ApplicationServices.AuthenticationService"/>
</service>
<!-- this enables the WCF RoleService endpoint -->
<service behaviorConfiguration="AppServiceBehaviors"
name="System.Web.ApplicationServices.RoleService">
<endpoint binding="basicHttpBinding"
bindingNamespace="http://asp.net/ApplicationServices/v200"
contract="System.Web.ApplicationServices.RoleService"/>
</service>
<!-- this enables the WCF ProfileService endpoint -->
<service behaviorConfiguration="AppServiceBehaviors"
name="System.Web.ApplicationServices.ProfileService">
<endpoint binding="basicHttpBinding"
bindingNamespace="http://asp.net/ApplicationServices/v200"
contract="System.Web.ApplicationServices.ProfileService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="AppServiceBehaviors">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
...
</configuration>
You can now run the Web service in order to activate the authentication, profile, and roles services that will be used by the client application. This also enables you to verify that the application service is working correctly.
To activate the Web site to expose the application services
In Solution Explorer, right-click the MyAuthenticationSvcWrap.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 the service. Make a note of the service URL (the URL that ends with "?wsdl"), because you will need it in the next steps to access the service from the client application. Repeat the process for the other two service files.
You have finished configuring the application services so that they are available on the Web. The next step is to invoke these services from a client application.