Jill,
I hope this helps in C#:
FileName: Authentication.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.DirectoryServices;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Principal;
using System.Web.Security;
using Microsoft.ApplicationBlocks.ExceptionManagement;
public static bool AdAuthenticate(string username, string password,string path)
{
bool isAuthenticated = false;
DirectoryEntry userEntry = new DirectoryEntry(path,username,password);
try
{
DirectorySearcher dirSearcher = new DirectorySearcher(userEntry);
dirSearcher.Filter = "(SAMAccount=" + username + ")";
dirSearcher.PropertiesToLoad.Add("cn");
SearchResult searchResult = dirSearcher.FindOne();
isAuthenticated = true;
}
catch (Exception ex)
{
isAuthenticated = false;
// ignore logon failures do to incorrect password
// publish all other errors to the event log
if (ex.Message.Trim() != "Logon failure: unknown user name or bad password")
{
ExceptionManager.Publish(ex);
}
}
return isAuthenticated;
}
Now change the Signin.aspx.cs
Filename: Signin.aspx.cs
Change the LoginBtn_Click method:
....
private void LoginBtn_Click(Object sender, ImageClickEventArgs e) {
// Attempt to Validate User Credentials using UsersDB
UsersDB accountSystem = new UsersDB();
// Adding Active Directory Connectivity
string userId = "";
if (email.Text.ToLower() == "guest" || email.Text.ToLower() == "admin")
{
userId = accountSystem.Login(email.Text, password.Text);
}
else
{
userId = accountSystem.Login(email.Text,password.Text,true);
}
if ((userId != null) && (userId != "")) {
// Use security system to set the UserID within a client-side Cookie
FormsAuthentication.SetAuthCookie(email.Text, RememberCheckbox.Checked);
// Redirect browser back to originating page
Response.Redirect(Request.ApplicationPath);
}
else {
Message.Text = "<" + "br" + ">Login Failed!" + "<" + "br" + ">";
}
}
Note: I allow the guest and admin account to authenticate through the standard portal forms authentication.
You will also need the Microsoft Application Blocks Exception Management Patterns and Practices reference from their site to use ExceptionManager.Publish(ex).
Ryan Lee
[email protected]