In this tutorial you will learn how to create a secured Web site where users need to provide user name and password to sign in.
![]()
Source Code You can find the source code for this chapter in the Examples\VB\SAFormsVB (or Examples\CS\SAFormsCS) folder in the Secure Access installation folder.
Create a new ASP.NET project or open an existing one.
Set proper authentication on the virtual directory in the Internet Information Services console (you can find it in the Control Panels, in the Administrative Tools folder): Locate your Web application in the tree and view its properties. Choose the "Directory Security" tab, click on the "Edit..." button and make sure that only "Anonymous access" box is checked and the "Digest authentication", "Basic authentication" and "Integrated Windows Authentication" boxes are unchecked.
![]() |
Tip | |
---|---|---|
Tip: You may prefer to leave the "Windows Authentication" box checked during the development phase so that you are allowed to debug your applications from the Visual Studio.NET environment. |
Add reference to the Secure Access libraries into your project:
Choose "Project" - "Add Reference..." item in the main menu.
Choose the ".NET" tab on the "Add Reference" dialog and click "Browse...". Choose ARDataServices.dll, ARObjects.dll and SecureAccess.dll files in the "DLLs" subfolder in the installation folder. Click the "Select" button and then click the "OK" button.
Add following lines in the "configuration" section of the Web.config file:
|
Modify data source, initial catalog, user id and password to appropriate values.
Add following lines in the "system.web" section (or replace the same section if it already exists):
|
You set up the ASP.NET security for the forms authentication with following parameters (these are standard ASP.NET configuration settings, you can find more details in the .NET Framework SDK documentation):
Name - name of the cookie that will be used to store user identity on the client side.
Path - describes the path (including subfolders) the authentication cookie is valid for.
LoginUrl - name of the page the users will be redirected to when they come not authenticated to your Web site.
Protection - method of cookie encryption.
Timeout - number of minutes until the authentication cookie expires. If user is inactive for specified period he/she must sign in again. If you use session variables for storing user information (user ticket) you should set this value to a smaller value than for the session time-out in the configuration/system.web/sessionState section.
Add or modify the "authorization" element in the "system.web" section so that it looks like this one:
|
Open your Global.asax file and add following lines at the beginning of its code-behind:
[Visual Basic] Imports System.Web.Security Imports System.Resources Imports PortSight.SecureAccess.ARDataServices Imports PortSight.SecureAccess.ARObjects Imports System.Threading Imports System.Globalization [C#] using System.Web.Security; using System.Resources; using PortSight.SecureAccess.ARDataServices; using PortSight.SecureAccess.ARObjects; using System.Threading; using System.Globalization; |
Add following code to the Application_Start event in the Global.asax:
[Visual Basic] 'create resource manager and store it in the application variable Dim assmbl As System.Reflection.Assembly = System.Reflection.Assembly.Load("SecureAccess") Dim Resman As ResourceManager = New ResourceManager("SecureAccess.strings", assmbl) Application("RM") = Resman [C#] //create resource manager and store it in the application variable System.Reflection.Assembly assmbl = System.Reflection.Assembly.Load("SecureAccess"); ResourceManager Resman = new ResourceManager("SecureAccess.strings", assmbl); Application["RM"] = Resman; |
Add following lines in the Session_Start event in the Global.asax:
[Visual Basic] 'create user ticket Dim currentUser As ARUser Dim arCN As ARConnection If User.Identity.IsAuthenticated Then arCN = New ARConnection() arCN.ConnectToCatalog() currentUser = arCN.GetUserByLogin(User.Identity.Name) If currentUser Is Nothing Then 'user was not found in the database arCN.Close() Response.Redirect("AccessDenied.aspx") Else If currentUser.IsLocked Then 'account is locked -> access denied arCN.Close() Response.Redirect("AccessDenied.aspx") Else 'account has been found and is not locked 'create user ticket and store it in the session variable Session("ARUserTicket") = New ARUserTicket(currentUser) arCN.Close() End If End If End If [C#] //create user ticket ARUser currentUser; ARConnection arCN; if (User.Identity.IsAuthenticated) { arCN = new ARConnection(); arCN.ConnectToCatalog(); currentUser = arCN.GetUserByLogin(User.Identity.Name); if (currentUser == null) { // user was not found in the database arCN.Close(); Response.Redirect("AccessDenied.aspx"); } else { if (currentUser.IsLocked()) { //account is locked -> access denied arCN.Close(); Response.Redirect("AccessDenied.aspx"); } else { //account has been found and is not locked //create user ticket and store it in the session variable Session["ARUserTicket"] = new ARUserTicket(currentUser); arCN.Close(); } } } |
This code is duplicated with the logon form code. It's used if user chose the "Remember password" option on the logon form and didn't authenticate through the logon form.
The code creates user ticket and stores it in the session variable "ARUserTicket". The user ticket contains cached information about user, such as full name and membership in roles, user groups and organizational units.
Create the LogonForm.aspx page with logon form. Copy the following files from the Secure Access Web application (please note you only copy these files into your application's folder, you don't include them in the project!):
ARUILogonForm.ascx
ARUISendPassword.ascx
ARUISetPassword.ascx
ARUISignOut.ascx
Edit HTML source of the logon.aspx page and add following line on the second line (after the <%@ Page& directive):
<%@ Register TagPrefix="uc1" TagName="ARUILogonForm" Src="ARUILogonForm.ascx" %> |
Put the following text somewhere inside the <form></form> tags:
<uc1:ARUILogonForm id="ARUILogonForm1" runat="server"></uc1:ARUILogonForm> |
You created LogonForm.aspx page and added the Secure Access logon control on it. This control ensures all that you need to authenticate user. It checks user's password and displays error messages when provided credentials are wrong or user account is locked. It also allows users to be sent with their password if they forget it and change their password if it expired. Please refer to the Appendix A - ASP.NET User Controls Reference to find detailed information about this control.
Create new page "default.aspx" and put the following code at the beginning of its HTML code:
<%@ Register TagPrefix="uc1" TagName="ARUISignOut" Src="ARUISignOut.ascx" %> |
Put the following text somewhere inside the <form></form> tags:
<uc1:ARUISignOut id="ARUISignOut1" runat="server"></uc1:ARUISignOut> |
Add following lines at the beginning of the default.aspx code-behind:
[Visual Basic] Imports PortSight.SecureAccess.ARDataServices Imports PortSight.SecureAccess.ARObjects [C#] using PortSight.SecureAccess.ARDataServices; using PortSight.SecureAccess.ARObjects; |
Add a new Label control on the default.aspx form and name it Label1. Add following code in the Page_Load method:
[Visual Basic] Dim userTicket As ARUserTicket userTicket = CType(Session("ARUserTicket"), ARUserTicket) If Not userTicket Is Nothing Then Label1.Text = "Hi " & userTicket.ObjectName & ", welcome to the PortSight Secure Access demo." End If [C#] ARUserTicket userTicket; userTicket = (ARUserTicket) Session["ARUserTicket"]; if (userTicket != null) { Label1.Text = "Hi " + userTicket.ObjectName + ", welcome to the PortSight Secure Access demo."; } |
Right-click the "default.aspx" page in the Solution Explorer and set it as a default. Compile and run your new Web application. You should see the log on form after the application starts:
Enter your user name and password as you defined them in the Secure Access database and click the "Log on" button. You should see the welcome message now:
Click the "Sign out" button and you will be redirected back to the logon form.