
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.
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
Open Visual Studio 2008.
On the File menu, click New Web Site.
The New Web Site dialog box appears.
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 Solution Explorer, click the name of the Web site, and then press F4 to open the Properties window.
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 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
Add a Global.asax file to the Web site and in the file, replace the existing Application_Start method with the following code:
|
<%@ 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>
|
|
<%@ 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.
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.
|
<%@ 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="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>
|
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.
|
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!";
}
}
|
Add a page named Login.aspx. Make sure that Place code in separate file is selected.
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 Profile.aspx, and make sure that Place code in separate file is selected.
Add the following markup to the Profile.aspx page:
|
<%@ 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>
|
|
<%@ 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.
In the code-behind file for the Profile.aspx page, add the following code:
|
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
|
|
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.
|
Add a page named CreateUser.aspx page 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 system.web group, change the default windows authentication to forms, as shown in the following example:
|
<authentication mode="Forms" />
|
In the system.web group, configure the roles service by adding the roleManager element, as shown in the following example:
|
<roleManager enabled="true"/>
|
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
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 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.
In the Default.aspx page, click Login.
The Login.aspx page is displayed.
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.
Click Profile Information.
The Profile.aspx page is displayed.
Enter or update the profile information of the logged-in user by entering the first name, last name, and identification 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.
You are finished creating users and profile information. You will now make this information available to client applications.