The following example shows an ASP.NET Web page that authenticates the user by using client script. The example requires that you have configured the server as described earlier in this topic. The name of the restricted folder is assumed to be Secured.
The page contains an <asp:ScriptManager> element. When this element is included on the page, the AuthenticationService object is automatically available to any client script on the page.
The page has a button with an associated event handler named OnClickLogin. Code in the method handler calls the login method of the AuthenticationService class.
After you are logged in, the button text changes and the text at the top of the page changes to indicate your logged-in status. Click the link at the bottom of the page to move to a page located in the Secured folder. Because you are now logged in, you can access pages in this folder without being redirected to the login page.
On the sample page, you can click a button to log out. This calls the OnClickLogout button event handler, which calls the logout method. After you have logged out, the text at the top of the page changes. If you try to access the page in the secured folder, you will be redirected to the login page, because your browser no longer has a forms authentication cookie.
The example code provides asynchronous completed callback functions for the login and logout methods. You can also create failure callback functions for both methods. For more information, see the example provided in the AuthenticationService class overview.
<%@ Page Language="VB" %>
<!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>Authentication Service</title>
<style type="text/css">
body { font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text { font: 8pt Trebuchet MS }
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManagerId">
<Scripts>
<asp:ScriptReference Path="Authentication.js" />
</Scripts>
</asp:ScriptManager>
<h2>Authentication Service</h2>
<span id="loggedin"
style="visibility:hidden; color:Green; font-weight:bold; font-size:large"
visible="false"><b>You are logged in! </b>
</span>
<span id="notloggedin"
style="visibility:visible;color:Red; font-weight:bold; font-size:large">
You are logged out!
<br /> <br />
<span style="font-weight:normal; font-size:medium; color:Black">
Please, use one of the following [username, password]
combinations:<br />
[user1, u$er1] <br/>
[user2, u$er2] <br/>
[user3, u$er3]
</span>
</span>
<br /><br />
<div>
<table>
<tr id="NameId" style="visibility:visible;">
<td style="background-color:Yellow; font-weight:bold; color:Red">
User Name:
</td>
<td>
<input type="text" id="username"/>
</td>
</tr>
<tr id="PwdId" style="visibility:visible;">
<td style="background-color:Yellow; font-weight:bold; color:Red">
Password:
</td>
<td>
<input type="password" id="password" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<button id="ButtonLogin"
style="background-color:Aqua;"
onclick="OnClickLogin(); return false;">Login</button>
<button id="ButtonLogout"
style="background-color:Aqua; visibility:hidden;"
onclick="OnClickLogout(); return false;">Logout</button>
</td>
</tr>
</table>
<br />
<br />
<a href="secured/Default.aspx" target="_top2" >
Attempt to access a page
that requires authenticated users.</a>
<br />
<br />
<!-- <a href="CreateNewUser.aspx"><b>Create a new user.</b></a>
-->
</div>
</form>
<hr />
<div id="FeedBackID" style="visibility:visible" />
</body>
</html>
<%@ Page Language="C#" %>
<!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>Authentication Service</title>
<style type="text/css">
body { font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text { font: 8pt Trebuchet MS }
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManagerId">
<Scripts>
<asp:ScriptReference Path="Authentication.js" />
</Scripts>
</asp:ScriptManager>
<h2>Authentication Service</h2>
<span id="loggedin"
style="visibility:hidden; color:Green; font-weight:bold; font-size:large"
visible="false"><b>You are logged in! </b>
</span>
<span id="notloggedin"
style="visibility:visible;color:Red; font-weight:bold; font-size:large">
You are logged out!
<br /> <br />
<span style="font-weight:normal; font-size:medium; color:Black">
Please, use one of the following [username, password]
combinations:<br />
[user1, u$er1] <br/>
[user2, u$er2] <br/>
[user3, u$er3]
</span>
</span>
<br /><br />
<div>
<table>
<tr id="NameId" style="visibility:visible;">
<td style="background-color:Yellow; font-weight:bold; color:Red">
User Name:
</td>
<td>
<input type="text" id="username"/>
</td>
</tr>
<tr id="PwdId" style="visibility:visible;">
<td style="background-color:Yellow; font-weight:bold; color:Red">
Password:
</td>
<td>
<input type="password" id="password" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<button id="ButtonLogin"
style="background-color:Aqua;"
onclick="OnClickLogin(); return false;">Login</button>
<button id="ButtonLogout"
style="background-color:Aqua; visibility:hidden;"
onclick="OnClickLogout(); return false;">Logout</button>
</td>
</tr>
</table>
<br />
<br />
<a href="secured/Default.aspx" target="_top2" >
Attempt to access a page
that requires authenticated users.</a>
<br />
<br />
<!-- <a href="CreateNewUser.aspx"><b>Create a new user.</b></a>
-->
</div>
</form>
<hr />
<div id="FeedBackID" style="visibility:visible" />
</body>
</html>