473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Validating user credentials against active directory

I have following code that validates a given user credentails against a
active directory. The login part works but I can not search as it fails to
return
the record. Does any one have any idea what is wrong?

public void Login(string user, string pwd, string domain)
{
string path = "<<my ldap path>>";

DirectoryEntry domainEntry = new DirectoryEntry(path);
domainEntry.Username = domain + @"\" + user;
domainEntry.Password = pwd;

domainEntry.AuthenticationType = AuthenticationTypes.Secure;

string searchFilter = "sAMAccountName=" + user;

DirectorySearcher searcher = new DirectorySearcher(domainEntry,
searchFilter);

searcher.SearchScope = SearchScope.Subtree;

//The following line throws exception if supplied user
credentails are not valid
SearchResult result = searcher.FindOne();
Console.WriteLine("Login succeeded.");

if (result == null)
{
Console.WriteLine("Search for user failed.");
Console.WriteLine();
}
else
{
Console.WriteLine("Search for user succeeded.");
}
}

Thanks.
Raghu/..
Apr 18 '06 #1
1 3318
Found the answer. Needed to use following filter.

string searchFilter = String.Format("(&(objectClass=user)(sAMAccountName =
{0}))", user);

string searchFilter = String.Format("(&(objectClass=user)(sAMAccountName =
{0}))", user);
"Raghu" <RaghuNoSpam> wrote in message
news:Oi**************@TK2MSFTNGP04.phx.gbl...
I have following code that validates a given user credentails against a
active directory. The login part works but I can not search as it fails to
return
the record. Does any one have any idea what is wrong?

public void Login(string user, string pwd, string domain)
{
string path = "<<my ldap path>>";

DirectoryEntry domainEntry = new DirectoryEntry(path);
domainEntry.Username = domain + @"\" + user;
domainEntry.Password = pwd;

domainEntry.AuthenticationType = AuthenticationTypes.Secure;

string searchFilter = "sAMAccountName=" + user;

DirectorySearcher searcher = new DirectorySearcher(domainEntry,
searchFilter);

searcher.SearchScope = SearchScope.Subtree;

//The following line throws exception if supplied user
credentails are not valid
SearchResult result = searcher.FindOne();
Console.WriteLine("Login succeeded.");

if (result == null)
{
Console.WriteLine("Search for user failed.");
Console.WriteLine();
}
else
{
Console.WriteLine("Search for user succeeded.");
}
}

Thanks.
Raghu/..

Apr 18 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John | last post by:
Hi I have this problem that client users enter emails incorrectly due to type and either the domain name is invalid (Microsot.com instead of Microsoft.com) or the syntax of the email...
18
by: Arthur | last post by:
Hi All, I would like to get the name of the user given their networkID, is this something Active Directory would be useful for?(For intranet users) If so, can you please point me to some sample...
9
by: webrod | last post by:
Hi all, how can I check a user/password in a LDAP ? I don't want to connect with this user, I would like to connect to LDAP with a ADMIN_LOG/ADMIN_PWD, then do a query to find the user and...
3
by: dorrit.Riemenschneider | last post by:
I need to validate a user with username and password against our OpenLDAP active directory. This is my code: Private bool ValidateUser (string username, string password) { DirectoryEntry...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.