Windows user login validation
using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
namespace VQP.BLL
{
public class LdapAuthentication
{
private string _path;
private string _filterAttribute = string.Empty;
public LdapAuthentication()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
///
/// </summary>
/// <param name="path"></param>
public LdapAuthentication(string path)
{
_path = path;
}
/// <summary>
///
/// </summary>
/// <param name="domain"></param>
/// <param name="username"></param>
/// <param name="pwd"></param>
/// <returns></returns>
public bool IsAuthenticated(string username, string
pwd)
{
return IsAuthenticated(false, "", username, pwd);
}
/// <summary>
///
/// </summary>
/// <param name="domain"></param>
/// <param name="username"></param>
/// <param name="pwd"></param>
/// <returns></returns>
public bool IsAuthenticated(bool includeDomain, string domain, string username, string pwd)
{
bool bResult = false;
string domainAndUsername = username;
if (includeDomain)
domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path,
domainAndUsername,
pwd);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
bResult = true;
// DirectorySearcher search = new DirectorySearcher(entry);
//
// search.Filter = "(SAMAccountName=" + username + ")";
//
// search.PropertiesToLoad.Add("cn");
//
// SearchResult result = search.FindOne();
//
// // Update the new path to the user in the directory
// if ( result != null)
// {
// _path = result.Path;
//
// _filterAttribute = (String)result.Properties["cn"][0];
//
// bResult = true;
// }
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.ToString());
}
return bResult;
}
public string GetGroups()
{
DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(cn=" + _filterAttribute + ")";
search.PropertiesToLoad.Add("memberOf");
StringBuilder groupNames = new StringBuilder();
try
{
SearchResult result = search.FindOne();
int propertyCount = result.Properties["memberOf"].Count;
string dn;
int equalsIndex, commaIndex;
for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
{
dn = (string)result.Properties["memberOf"][propertyCounter];
equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if (-1 == equalsIndex)
{
return null;
}
groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
groupNames.Append("|");
}
}
catch (Exception ex)
{
throw new Exception("Error obtaining group names. " + ex.Message);
}
return groupNames.ToString();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="strUserID"></param>
/// <param name="strPassword"></param>
/// <returns></returns>
public string ValidUserLogin( string strUserID, string strPassword)
{
string strMessage = string.Empty;
// CORP user has 8 characters, MATPARTNERS user must not be 8 characters.
string strPath = "LDAP://MATPARTNERS";
if (strUserID.Length == 8)
strPath = "LDAP://CORP";
string domain = "MATPARTNERS";
if (strUserID.Length == 8)
domain = "CORP";
try
{
LdapAuthentication objBLL = new LdapAuthentication(strPath);
if (!objBLL.IsAuthenticated(true, domain, strUserID, strPassword))
{
strMessage += "Please check your name or password!";
}
}
catch (Exception ex)
{
strMessage += "Please check your name or password!";
}
return strMessage;
}
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
namespace VQP.BLL
{
public class LdapAuthentication
{
private string _path;
private string _filterAttribute = string.Empty;
public LdapAuthentication()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
///
/// </summary>
/// <param name="path"></param>
public LdapAuthentication(string path)
{
_path = path;
}
/// <summary>
///
/// </summary>
/// <param name="domain"></param>
/// <param name="username"></param>
/// <param name="pwd"></param>
/// <returns></returns>
public bool IsAuthenticated(string username, string
pwd)
{
return IsAuthenticated(false, "", username, pwd);
}
/// <summary>
///
/// </summary>
/// <param name="domain"></param>
/// <param name="username"></param>
/// <param name="pwd"></param>
/// <returns></returns>
public bool IsAuthenticated(bool includeDomain, string domain, string username, string pwd)
{
bool bResult = false;
string domainAndUsername = username;
if (includeDomain)
domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path,
domainAndUsername,
pwd);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
bResult = true;
// DirectorySearcher search = new DirectorySearcher(entry);
//
// search.Filter = "(SAMAccountName=" + username + ")";
//
// search.PropertiesToLoad.Add("cn");
//
// SearchResult result = search.FindOne();
//
// // Update the new path to the user in the directory
// if ( result != null)
// {
// _path = result.Path;
//
// _filterAttribute = (String)result.Properties["cn"][0];
//
// bResult = true;
// }
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.ToString());
}
return bResult;
}
public string GetGroups()
{
DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(cn=" + _filterAttribute + ")";
search.PropertiesToLoad.Add("memberOf");
StringBuilder groupNames = new StringBuilder();
try
{
SearchResult result = search.FindOne();
int propertyCount = result.Properties["memberOf"].Count;
string dn;
int equalsIndex, commaIndex;
for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
{
dn = (string)result.Properties["memberOf"][propertyCounter];
equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if (-1 == equalsIndex)
{
return null;
}
groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
groupNames.Append("|");
}
}
catch (Exception ex)
{
throw new Exception("Error obtaining group names. " + ex.Message);
}
return groupNames.ToString();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="strUserID"></param>
/// <param name="strPassword"></param>
/// <returns></returns>
public string ValidUserLogin( string strUserID, string strPassword)
{
string strMessage = string.Empty;
// CORP user has 8 characters, MATPARTNERS user must not be 8 characters.
string strPath = "LDAP://MATPARTNERS";
if (strUserID.Length == 8)
strPath = "LDAP://CORP";
string domain = "MATPARTNERS";
if (strUserID.Length == 8)
domain = "CORP";
try
{
LdapAuthentication objBLL = new LdapAuthentication(strPath);
if (!objBLL.IsAuthenticated(true, domain, strUserID, strPassword))
{
strMessage += "Please check your name or password!";
}
}
catch (Exception ex)
{
strMessage += "Please check your name or password!";
}
return strMessage;
}