12. Create the "Add" methods
VB: Public Sub Add(ByVal User As Dave.Object.User)
Try
Me.List.Add(User)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub Add(ByVal ID As Integer, ByVal FirstName As String, _
ByVal LastName As String, ByVal UserName As String,)
Dim User As User
Try
User = New User(ID, FirstName, LastName, UserName)
Me.List.Add(User)
Catch ex As Exception
Throw ex
Finally
User = Nothing
End Try
End Sub
C# public void Add(User Item)
{ try
{ this.List.Add(Item); }
catch (Exception ex)
{ throw ex; }
finally
{ Item = null; }
}
public void Add(int ID, string FirstName, string LastName,
string UserName, string Password, bool Active)
{ User Item;
try
{ Item = new User(ID, FirstName, LastName,
UserName, Password, Active);
this.List.Add(Item); }
catch (Exception ex)
{ throw ex; }
finally
{ Item = null; }
}
13. Create the "Remove" method
VB: Public Sub RemoveByUserID(ByVal UserID As Integer)
For i As Integer = 0 To list.Count - 1
If CType(list(i), Dave.Object.User).ID = UserID Then
list.RemoveAt(i)
Exit Sub
End If
Next
End Sub
C#: public void RemoveByUserID(int UserID)
{ for (int i = 0; i < List.Count - 1; i++)
{ if (((User)List[i]).ID == UserID)
{
List.RemoveAt(i);
}
}
}
14. Create the "Comparer" class for the sorting (This is a nested Class within Collection)
VB: Public Class Comparer
Public Class LastName
Implements IComparer
Private _SDirection As Dave.Object.UserCollection.SortDirection
Public Sub New(ByVal SDirection As Dave.Object.UserCollection.SortDirection)
_SDirection = SDirection
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim Doc1 As Dave.Object.User
Dim Doc2 As Dave.Object.User
If GetType(Dave.Object.User).IsInstanceOfType(x) Then
Doc1 = CType(x, Dave.Object.User)
Else
Throw New Exception("Casting failed...")
End If
If GetType(Dave.Object.User).IsInstanceOfType(y) Then
Doc2 = CType(y, Dave.Object.User)
Else
Throw New Exception("Casting failed...")
End If
Select Case _SDirection
Case SortDirection.ASC
Return CInt(Doc1.LastName.ToLower < Doc2.LastName.ToLower)
Case SortDirection.DESC
Return CInt(Doc1.LastName.ToLower > Doc2.LastName.ToLower)
End Select
End Function
End Class
Public Class UserName
Implements IComparer
Private _SDirection As Dave.Object.UserCollection.SortDirection
Public Sub New(ByVal SDirection As Dave.Object.UserCollection.SortDirection)
_SDirection = SDirection
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim Doc1 As Dave.Object.User
Dim Doc2 As Dave.Object.User
If GetType(Dave.Object.User).IsInstanceOfType(x) Then
Doc1 = CType(x, Dave.Object.User)
Else
Throw New Exception("Casting failed...")
End If
If GetType(Dave.Object.User).IsInstanceOfType(y) Then
Doc2 = CType(y, Dave.Object.User)
Else
Throw New Exception("Casting failed...")
End If
Select Case _SDirection
Case SortDirection.ASC
Return CInt(Doc1.UserName < Doc2.UserName)
Case SortDirection.DESC
Return CInt(Doc1.UserName > Doc2.UserName)
End Select
End Function
End Class
End Class
C#: public class Comparer
{ public class LastName : IComparer
{ private UserCollection.SortDirection _SortDirection;
public void Comparer(UserCollection.SortDirection SortDirection)
{ _SortDirection = SortDirection; }
int IComparer.Compare(object x, object y)
{ User Obj1;
User Obj2;
if (x is User)
Obj1 = (User)x;
else
throw new Exception("Casting failed...");
if (y is User)
Obj2 = (User)y;
else
throw new Exception("Casting failed...");
switch (_SortDirection)
{ case SortDirection.ASC:
{ return (int)Obj1.LastName.ToLower().CompareTo(Obj2.LastName.ToLower());
}
case SortDirection.DESC:
{ return (int)Obj2.LastName.ToLower().CompareTo(Obj1.LastName.ToLower());
}
default:
return 0;
}
}
}
public class UserName : IComparer
{ private UserCollection.SortDirection _SortDirection;
public void Comparer(UserCollection.SortDirection SortDirection)
{ _SortDirection = SortDirection; }
int IComparer.Compare(object x, object y)
{
User Obj1;
User Obj2;
if (x is User)
Obj1 = (User)x;
else
throw new Exception("Casting failed...");
if (y is User)
Obj2 = (User)y;
else
throw new Exception("Casting failed...");
switch (_SortDirection)
{ case SortDirection.ASC:
{ return (int)Obj1.UserName.ToLower().CompareTo(Obj2.UserName.ToLower());
}
case SortDirection.DESC:
{ return (int)Obj2.UserName.ToLower().CompareTo(Obj1.UserName.ToLower());
}
default:
return 0;
}
}
}
}
15. Create the "Sort" method in the original Collection Class, not the Comparer sub-class
VB: Public Sub Sort(ByVal SortField As SortField, ByVal SortDirection As SortDirection)
Try
Select Case SortField
Case SortField.FirstName
Dim comp As New Comparer.FirstName(SortDirection)
Me.InnerList.Sort(comp)
Case SortField.UserName
Dim comp As New Comparer.UserName(SortDirection)
Me.InnerList.Sort(comp)
End Select
Catch ex As Exception
Throw ex
End Try
End Sub
C#: public void Sort(UserCollection.SortField SortField, SortDirection SortDirection)
{ try
{ switch (SortField)
{ case UserCollection.SortField.LastName:
{ UserCollection.Comparer.LastName comp =
new UserCollection.Comparer.LastName();
InnerList.Sort(comp);
break;
}
case UserCollection.SortField.UserName:
{ UserCollection.Comparer.UserName comp =
new UserCollection.Comparer.UserName();
InnerList.Sort(comp);
break;
}
}
}
catch (Exception ex)
{ throw ex; }
}
Create a Data Access Layer (DAL) Project (This layer is the only one that links to db)
1. Right-click on "Solution | Add | New Project..."
2. In "Project Types" pane, select language of choice (e.g. Visual C#)
3. In "Templates" pane, select "Class Library"
4. Give the Project a Name (e.g. Dave.Data)
5. Choose a location other than the solution (e.g. C:\DaveApp\Model)
6. Click "OK"
7. The project should open up with "Class1" as the default class object
8. Right-click on "DALProject | Add Reference..."
9. Click on the Browse Tab
10. Navigate to where your Microsoft Enterprise Library dll's are located...
(e.g. C:\Program Files\Microsoft Enterprise Library Januaray 2006\bin)
NOTE: If you don't have Enterprise Library, you'll have to download and install.
11. Select the following two items to add:
a. Microsoft.Practices.EnterpriseLibrary.Data.dll
b. Microsoft.Practices.EnterpriseLibrary.Common.dll
12. Click "OK"
13. Right-click on "DALProject | Add Reference..."
14. Click on the Projects tab
15. Select your Object Layer Project (e.g. Dave.Object)
16. Click "OK"
17. If System.Data is not a reference, add that, as well
Prior to creating any objects to the Data Access Layer, we create a basic website, so we
can set configurations in the Web.Config file
Create a Data Access Layer class for each Object/Model
1. Rename the "Class1.cs" or "Class1.vb" object as "User"
or
Add a new Class object to the Data Project
2. Add the Microsoft.Practices.EnterpriseLibrary.Data Namespace
VB: a. Open the properties for the DAL project
b. Got to "references" tab
c. Place a check in the "Microsoft.Practices.EntLib.Data" block
inside the Imported Namespaces pane
C# a. Add a using statement for the EntLibrary.Data object
e.g. using Microsoft.Practices.EnterpriseLibrary.Data;
3. Add the using/import line for System.Data to each class you build
VB: should not have to do anything
C# using System.Data;
4. Add the Create Method to the class
VB: Public Function Create(ByVal FirstName As String, _
ByVal LastName As String, ByVal UserName As String _
ByVal Password As String) As Integer
Dim db As Database
Dim cmd As Common.DbCommand
Dim sprocName As String = String.Empty
Try
db = DatabaseFactory.CreateDatabase("DavesDBSQL")
sprocName = "dbo.usp_User_Insert"
cmd = db.GetStoredProcCommand(sprocName, FirstName, _
LastName, UserName, Password)
' Add Transaction Context Handling
db.ExecuteNonQuery(cmd)
Return CInt(cmd.Parameters("@RETURN_VALUE").Value)
Finally
If Not cmd Is Nothing Then
cmd.Dispose()
cmd = Nothing
End If
db = Nothing
sprocName = Nothing
End Try
End Function
C#: public int Create(string FirstName, string LastName, string UserName, string Password)
{
Database db;
System.Data.Common.DbCommand cmd;
string sprocName = string.Empty;
try
{
db = DatabaseFactory.CreateDatabase("DavesDBSQL");
sprocName = "dbo.usp_User_Insert";
cmd = db.GetStoredProcCommand(sprocName, FirstName, LastName, UserName, Password);
try
{
// TODO: Put in Transaction Context Handling
db.ExecuteNonQuery(cmd);
return (int)cmd.Parameters["@RETURN_VALUE"].Value;
}
finally
{
if (!(cmd == null))
{
cmd.Dispose();
cmd = null;
}
}
}
finally
{
db = null;
sprocName = null;
}
}
SQL: Go To your database and create a stored procedure for above code
CREATE PROCEDURE [dbo].[usp_User_Insert]
@FirstName char(50),
@LastName char(50),
@UserName char(50),
@Password char(50)
AS
Declare @ID int
If Not Exists(Select UserID FROM tblUser WHERE UserName = @UserName)
Begin
INSERT INTO tblUser
(FirstName, LastName, UserName, Password, Active)
VALUES
(@FirstName, @LastName, @UserName, @Password, 1)
SET @ID = scope_identity()
End
Else
Set @ID = -1 --User already exists
Return @ID
5. Add the Delete object to the Data Project
VB:
C#:
6. Add the Update object to the Data Project
VB:
C#:
7. Add the Read object to the Data Project
VB: BUILD FUNCTION BASED ON USERNAME
Public Function GetByUserName(ByVal UserName As String, _
ByVal Company As String) As Dave.Object.User
Dim db As Database
Dim cmd As Common.DbCommand
Dim dr As IDataReader
Dim sprocName As String = String.Empty
Try
db = DatabaseFactory.CreateDatabase("DavesDBSQL")
sprocName = "dbo.usp_User_GetByUserName"
cmd = db.GetStoredProcCommand(sprocName, UserName, Company)
dr = db.ExecuteReader(cmd)
Return BuildUser(dr)
Finally
If Not dr Is Nothing Then
If Not dr.IsClosed Then dr.Close()
dr = Nothing
End If
If Not cmd Is Nothing Then
cmd.Dispose()
cmd = Nothing
End If
db = Nothing
sprocName = Nothing
End Try
End Function
BUILD FUNCTION TO GET ALL USERS
Public Function GetAll() As Dave.Object.UserCollection
Dim dr As IDataReader
Dim db As SqlDatabase
Dim cmd As Common.DbCommand
Dim sprocName As String = String.Empty
Try
db = DatabaseFactory.CreateDatabase("DavesDBSQL")
sprocName = "dbo.usp_User_GetAll"
cmd = db.GetStoredProcCommand(sprocName)
dr = db.ExecuteReader(cmd)
Return BuildUserCollection(dr)
Finally
If Not dr Is Nothing Then
If Not dr.IsClosed Then dr.Close()
dr = Nothing
End If
If Not cmd Is Nothing Then
cmd.Dispose()
cmd = Nothing
End If
db = Nothing
sprocName = Nothing
End Try
End Function
BUILD USER FROM DATAREADER
Private Function BuildUser(ByVal dr As IDataReader) As Dave.Object.User
Dim user As New Dim user As New Dave.Object.User
Try
If dr.Read Then
user.ID = CInt(IIf(dr("UserID") Is DBNull.Value, 0, dr("UserID")))
user.FirstName = Trim(CStr(IIf(dr("FirstName") Is DBNull.Value, String.Empty, dr("FirstName"))))
user.LastName = Trim(CStr(IIf(dr("LastName") Is DBNull.Value, String.Empty, dr("LastName"))))
user.UserName = Trim(CStr(IIf(dr("UserName") Is DBNull.Value, String.Empty, dr("UserName"))))
user.Password = Trim(CStr(IIf(dr("Password") Is DBNull.Value, String.Empty, dr("Password"))))
user.Active = CByte(IIf(dr("Active") Is DBNull.Value, 0, dr("Active")))
End If
Return user
Catch ex As Exception
Throw ex
End Try
End Function
BUILD USERCOLLECTION FROM DATAREADER
Private Function BuildUserCollection(ByVal dr As IDataReader) As Dave.Object.UserCollection
Dim ucol As New Dave.Object.UserCollection
Dim user As New Dave.Object.User
Try
While dr.Read
user = New Dave.Object.User
With user
user.ID = CInt(IIf(dr("UserID") Is DBNull.Value, 0, dr("UserID")))
user.FirstName = Trim(CStr(IIf(dr("FirstName") Is DBNull.Value, String.Empty, dr("FirstName"))))
user.LastName = Trim(CStr(IIf(dr("LastName") Is DBNull.Value, String.Empty, dr("LastName"))))
user.UserName = Trim(CStr(IIf(dr("UserName") Is DBNull.Value, String.Empty, dr("UserName"))))
user.Password = Trim(CStr(IIf(dr("Password") Is DBNull.Value, String.Empty, dr("Password"))))
user.Active = CByte(IIf(dr("Active") Is DBNull.Value, 0, dr("Active")))
End With
ucol.Add(user)
user = nothing
End While
Return ucol
Catch ex As Exception
Throw ex
Finally
ucol = Nothing
user = Nothing
End Try
End Function
C#: BUILD FUNCTION BASED ON USERNAME
public Dave.Object.User GetByUserName(string UserName)
{ Database db;
System.Data.Common.DbCommand cmd;
IDataReader dr;
string sprocName = string.Empty;
try
{ db = DatabaseFactory.CreateDatabase("DavesDBSQL");
sprocName = "dbo.usp_User_GetByUserName";
cmd = db.GetStoredProcCommand(sprocName, UserName);
try
{ // TODO: Put in Transaction Context Handling
dr = db.ExecuteReader(cmd);
try
{
return BuildUser(dr);
}
finally
{ if (!(dr == null))
{ if (dr.IsClosed)
{ dr.Close();
}
dr = null;
}
}
}
finally
{ if (!(cmd == null))
{ cmd.Dispose();
cmd = null;
}
}
}
finally
{ db = null;
sprocName = null;
}
}
BUILD FUNCTION TO GET ALL USERS
public Dave.Object.UserCollection GetAll()
{ Database db;
System.Data.Common.DbCommand cmd;
IDataReader dr;
string sprocName = string.Empty;
try
{ db = DatabaseFactory.CreateDatabase("DavesDBSQL");
sprocName = "dbo.usp_User_GetAll";
cmd = db.GetStoredProcCommand(sprocName);
try
{ // TODO: Put in Transaction Context Handling
dr = db.ExecuteReader(cmd);
try
{
return BuildUserCollection(dr);
}
finally
{ if (!(dr == null))
{ if (dr.IsClosed)
{ dr.Close();
}
dr = null;
}
}
}
finally
{ if (!(cmd == null))
{ cmd.Dispose();
cmd = null;
}
}
}
finally
{ db = null;
sprocName = null;
}
}
BUILD USER FROM DATAREADER
private Dave.Object.User BuildUser(IDataReader dr)
{ Dave.Object.User user = new Dave.Object.User();
try
{ if (dr.Read())
{ user.ID = (int)(dr["UserID"] is DBNull ? 0 : dr["UserID"]);
user.FirstName = (dr["FirstName"] is DBNull ? string.Empty :dr["Firstname"].ToString().Trim() );
user.LastName = (dr["LastName"] is DBNull ? string.Empty : dr["LastName"].ToString().Trim());
user.UserName = (dr["UserName"] is DBNull ? string.Empty : dr["UserName"].ToString().Trim());
user.Password = (dr["Password"] is DBNull ? string.Empty : dr["Password"].ToString().Trim());
user.Active = (bool)(dr["Active"] is DBNull ? 0 : dr["Active"]);
}
return user;
}
catch (Exception ex)
{ throw ex;
}
}
BUILD USERCOLLECTION FROM DATAREADER
private Dave.Object.UserCollection BuildUserCollection(IDataReader dr)
{
Dave.Object.UserCollection ucol = new Dave.Object.UserCollection();
Dave.Object.User user = new Dave.Object.User();
try
{
while (dr.Read())
{
user = new Dave.Object.User();
user.ID = (int)(dr["UserID"] is DBNull ? 0 : dr["UserID"]);
user.FirstName = (dr["FirstName"] is DBNull ? string.Empty : dr["Firstname"].ToString().Trim());
user.LastName = (dr["LastName"] is DBNull ? string.Empty : dr["LastName"].ToString().Trim());
user.UserName = (dr["UserName"] is DBNull ? string.Empty : dr["UserName"].ToString().Trim());
user.Password = (dr["Password"] is DBNull ? string.Empty : dr["Password"].ToString().Trim());
user.Active = (bool)(dr["Active"] is DBNull ? 0 : dr["Active"]);
ucol.Add(user);
user = null;
}
return ucol;
}
catch (Exception ex)
{
throw ex;
}
}
SQL: CREATE STORED PROCEDURE TO GET BY USERNAME
CREATE PROCEDURE [dbo].[usp_User_GetByUserName]
@UserName char(50)
AS
SELECT UserID, FirstName, LastName, UserName, Password, Active
FROM tblUser
WHERE UserName = @UserName AND Active = 1
CREATE STORED PROCEDURE TO GET ALL USERS
CREATE PROCEDURE [dbo].[usp_User_GetAll]
AS
SELECT UserID, FirstName, LastName, UserName, Password, Active
FROM tblUser
WHERE Active = 1
ORDER BY LastName, FirstName
Create a Business Logic Layer (BLL) Project
(This is the only layer that the application should hit)
1. Right-click on "Solution | Add | New Project..."
2. In "Project Types" pane, select language of choice (e.g. Visual C#)
3. In "Templates" pane, select "Class Library"
4. Give the Project a Name (e.g. Dave.Business)
5. Choose a location other than the solution (e.g. C:\DaveApp\BusinessLogic)
6. Click "OK"
7. The project should open up with "Class1" as the default class object
8. Right-click on "DALProject | Add Reference..."
9. Click on the Projects tab
10. Select your Data Layer Project (e.g. Dave.Data)
and your Object Layer Project (e.g. Dave.Object)
11. Click "OK"
Create a User Business Logic Class
1. Rename the "Class1.cs" or "Class1.vb" class to "User"
2. Add the following for your class
VB: Public Class User
C#: public class User
3. Create a class level variable for the User's Data object
VB: Private _udal As Dave.Data.User
C#: private Dave.Data.User _uDAL;
4. Create the "Create" function for the "User" BLL
VB: Public Function Create(ByVal FirstName As String, _
ByVal LastName As String, ByVal UserName As String, _
ByVal Password As String) As Integer
Dim userid As Integer
Try
If _udal Is Nothing Then _udal = New Dave.Data.User
' Add Transaction Context Handling
' Add Password Encryption
userid = _udal.Create(FirstName, LastName, _
UserName, Password)
Return userid
Catch
' Add Transaction Error catching
Throw
Finally
userid = Nothing
End Try
End Function
C#: public int Create(string FirstName, string LastName, string UserName, string Password)
{
int userID;
try
{
if (_uDAL == null)
{
_uDAL = new Dave.Data.User();
}
// TODO: Add Transaction Context Handling
// TODO: Add Password Encryption
userID = _uDAL.Create(FirstName, LastName, UserName, Password);
return userID;
}
catch
{
throw;
}
}
5. Create the "GetAll" Method
VB: Public Function GetAll(ByVal CompanyID As Integer) As Dave.Object.UserCollection
If _udal Is Nothing Then _udal = New Dave.Data.User
Return _udal.GetAll(CompanyID)
End Function
C#: public Dave.Object.UserCollection GetAll()
{
if (_uDAL == null)
{
_uDAL = new Dave.Data.User();
}
return _uDAL.GetAll();
}
6. Create the "GetUser" Method, by username for authenticating the user
VB: Public Function GetUser(ByVal UserName As String, ByVal Company As String) As Dave.Object.User
If _udal Is Nothing Then _udal = New Dave.Data.User
Return _udal.GetByUserName(UserName, Company)
End Function
C#: public Dave.Object.User GetUser(string UserName)
{
if (_uDAL == null)
{
_uDAL = new Dave.Data.User();
}
return _uDAL.GetByUserName(UserName);
}
Create the Web Site
NOTE: Instead of keeping our files under the IIS location, we are going to create a folder that will hold
our web site files
NOTE: You must have IIS set up on your computer before continuing
1. In Explorer, create a folder that will store your Website files. This should be at the same level as the
other folders we have created (e.g. C:\DaveApp\Website)
2. Open IIS to create a Web site
a. Click on "Start | Control Panel"
b. After "Control Panel" opens, double-click on "Administrative Tools"
c. After "Administrative Tools" opens, double-click on "Internet Information Services"
d. Using the left pane, expand the tree to show the "Default Web Site" node
e. Right-click on "Default Web Site" and click "New | Virtual Directory..."
f. Click "Next" at Wizard begin screen.
g. Give your Web site an alias and click "Next". (E.g. DaveApp)
h. Using the "Browse" button, Navigate to the folder we created in step 1 (e.g. "C:\DaveApp\Website")
i. Click "Next" | "Next" (leave settings) | "Finish"
3. Add Web Site in Visual Studio...
a. In "Solution Explorer"
b. Right-click on "Solution | Add | New Web Site..."
c. In the "Templates" pane, select "ASP.NET Web Site"
d. For location, select "HTTP" and browse to the site we created in step 2...
1. In the left pane, select "Local IIS"
2. Click on the site (e.g. "Local Web Servers | Default Web Site | DaveApp")
3. Click "Open"
e. Select your language
f. Click "OK"
Configure Data Access Application Block
1. Open the "Enterprise Library 2.0 Configuration Tool"
Click on "Start | All Programs | Enterprise Library January 2006 | Enterprise Library Configuration"
2. Right-click on "File | Open Application"
3. Navigate to your Web Site (e.g. C:\DaveApp\Website)
4. Double-click on "Web.config"
5. In the right pane, if the "Data Access Application Block" is not visible, do the following...
a. Right click on the our C:\DaveApp\Website node
b. Select "New | Data Access Application Block"
6. If "Connection Strings" is not visible under "Data Access Application Block"
a. Right click on "Data Access Application Block"
b. Select "New | Connection Strings"
7. Do the following to create a new Connection String for our web site
a. Right-click "Connection Strings | New | Connection Strings"
b. Give the Connection String a name in the right pane (e.g. "DavesDBSQL")
NOTE: This will be the same name used in the Data Access Layer
c. Keep ProviderName as "System.Data.SqlClient"
d. Click on the Database node under "DaveAppConnection"
e. In the value field, enter the name of the database (e.g. "DavesDB")
f. Click on the Server node under "DaveAppConnection"
g. In the Value field, enter the name of the provider (e.g. "(local)\SQLExpress" or "MyName")
h. Right-click on the Integrated Security node under "DaveAppConnection" and select "Remove"
i. Right-click on "DaveAppConnection | New | Connection String Parameter"
j. Name the new Parameter "User ID" and give it the value for your database User ID(e.g. "Dave")
k. Right-click on "DaveAppConnection | New | Connection String Parameter"
l. Name the new Parameter "Password" and give it the value for your database password(e.g. "MyPassword45")
m. Click on "File | Save All"
Configure Web Site
1. Right-click on the Web site in "Solution Explorer" and select "Set as Startup Project"
2. In "Solution Explorer", right-click "WebSite | Add Existing Item"
3. Navigate to Website folder, select "Web.config" and click "Add"
4. Right-click on "Defualt.aspx" and select "Set as Start Page"
5. Add references to the "Object" and "BusinessLogic" projects
6. Open up the Web.Config file
7. Within the <system.web> elements, add the following...
<authentication mode="Forms">
<forms name="Logon" loginUrl="Login.aspx" path="/" protection="All" timeout="90"></forms>
</authentication>
<authorization>
<deny users="?"/>
<allow users="*"/>
</authorization>
8. If you get an "Unable to Start debugging" Error, do the following...
a. Open IIS
b. Right-click on your web site and select "Properties"
c. Select "Direct Security" tab
d. In the Anonymous access and authentication section, click the Edit button.
e. In the Authentication Methods dialog box, under Authenticated access, select Integrated Windows authentication.
f. Click OK to close the Authentication Methods dialog box.
g. Click OK to close the Properties dialog box.
Add a Login.aspx page to your website
1. Right-click on the Web site in "Solution Explorer" and select "Add New Item"
2. Select "Web Form" in the templates pane
3. Select your language
4. Give your page a name (e.g. Login.aspx) and click "Add"
5. In "Source" view for the page, change your title (e.g. "Dave's Login Page")
6. Change view to "Design" view
7. From the toolbox, drag a Login control to the page designer
8. To prevent cookie stealing, set the "DisplayRememberMe" property of the "Login" control to "false"
9. Double click on the "Login" button
10. Add the following variable to the class:
VB: Private _uBLL As Dave.BusinessLogic.User
C#: private Dave.BusinessLogic.User _uBLL;
11. Add the following code to the Login1_Authenticate method
VB: Private Sub Login1_Authenticate(ByVal sender As Object, ByVal e As AuthenticateEventArgs)
Dim user Dave.Object.User
Try
If _uBLL Is Nothing Then _uBLL = New Dave.BusinessLogic.User()
user = _uBLL.GetUser(Login1.UserName)
If user.ID > 0 Then
If user.Password = Login1.Password Then
Session.Add("User", user)
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(user.UserName, False)
Else
e.Authenticated = False
End If
End If
Catch (ex As Exception)
' Add Exception Reporting
Finally
user = nothing
End Try
C#: protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
Dave.Object.User user;
try
{
if (_uBLL == null)
{
_uBLL = new Dave.BusinessLogic.User();
}
user = _uBLL.GetUser(Login1.UserName);
if (user.ID > 0)
{
if (user.Password == Login1.Password)
{
Session.Add("User", user);
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(user.UserName, false);
}
else
{
e.Authenticated = false;
}
}
}
catch (Exception ex)
{
// Add Exception Reporting
}
finally
{
user = null;
}
}
Modify the Default.aspx page to show a collection of users in the system
1. View the code section for the Default.aspx page
2. Add the following class level variable
VB: Private _userBLL As Dave.BusinessLogic.User
C#: private Dave.BusinessLogic.User _userBLL;
3. Double click on the designer to get to the Page_Load method
4. Add the following code to the Page_Load event
VB: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim uCol As Dave.Object.UserCollection
Dim userBLL As New Dave.BusinessLogic.User
Dim userSession As Dave.Object.User
If Not IsPostBack Then
If _userBLL Is Nothing Then _userBLL = New Dave.BusinessLogic.User()
uCol = _UserBLL.GetAll()
userSession = CType(Session("User"), Dave.Object.User)
Response.Write("Hello " & userSession.FirstName & "<br/>")
Response.Write("Total Number of users in System: " & uCol.Count.ToString() + "<br/>")
Foreach user As Dave.Object.User In uCol
Response.write(user.FirstName & " " & user.LastName & " " & user.UserName & "<br/>")
Next
End If
End Sub
C#: protected void Page_Load(object sender, EventArgs e)
{
Dave.Object.UserCollection uCol;
Dave.BusinessLogic.User userBLL = new Dave.BusinessLogic.User();
Dave.Object.User userSession;
if (!IsPostBack)
{
if (_userBLL == null)
{
_userBLL = new Dave.BusinessLogic.User();
}
uCol = _userBLL.GetAll();
userSession = (Dave.Object.User)Session["User"];
Response.Write("Hello " + userSession.FirstName + "<br/>");
Response.Write("Total Number of users in system: " + uCol.Count.ToString() + "<br/>");
foreach (Dave.Object.User user in uCol)
{
Response.Write(user.FirstName + " " + user.LastName + " " + user.UserName + "<br/>");
}
}
}
Run the Application to test the functionality