473,386 Members | 1,867 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,386 software developers and data experts.

How to fetch attributes of LDAP entry?

I try to do some authentication with LDAP. But it's a bit different to
the sample in msdn.
I wrote a class LdapAuthentication with a method

public bool IsAuthenticated(String domain, String username, String pwd)
{

String domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd,
AuthenticationTypes.Anonymous);

try
{
Object obj = entry.NativeObject;

DirectorySearcher search = new DirectorySearcher(entry);

search.Filter = "(cn=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();

if ( null == result )
{
return false;
}
_path = result.Path;
result.GetDirectoryEntry();
_filterAttribute = (String)result.Properties["cn"][0];
}
catch(Exception ex)
{
throw new Exception("Error authenticating user: " + ex.Message );
}

return true;
}

The problem is, that the DirectorySearcher doesn't tell me if a user is
authenticated. It only tells me: an entry with (cn="+username+") has
been found.
The new _path shows me the way to my user (ldap://server/cn=user....).
The user has a attribute named userPassword (which is encrypted in
crypt-mode). So I have to compare the user input to the userPassword
stored in LDAP. My problem: I can't fetch out the userPassword! I need
it to get the salt.
I tried it with the following method (where _path is the whole path to
the user information):

public String GetPwd()
{
DirectoryEntry entry = new DirectoryEntry(_path);
DirectorySearcher search = new DirectorySearcher(entry);
search.PropertiesToLoad.Add("userPassword");

String ladpPwd = "";

try
{
SearchResult result = search.FindOne();
ladpPwd = result.Properties["userPassword"][0];

}
catch ( Exception ex )
{
throw new Exception("Could not find password: " + ex.Message );
}

return ldapPwd;
}

Maybe our LDAP is a little bit different or I don't understand the stuff!
Nov 19 '05 #1
3 2505
Hi Dennis:

On Thu, 24 Feb 2005 15:09:27 +0100, Dennis Dobslaf <do*****@kdvz.de>
wrote:
The new _path shows me the way to my user (ldap://server/cn=user....).
The user has a attribute named userPassword (which is encrypted in
crypt-mode). So I have to compare the user input to the userPassword
stored in LDAP. My problem: I can't fetch out the userPassword! I need
it to get the salt.


That feature is by design - if you think about it - you never actually
'see' passwords anywhere in Windows.

When you create the DirectoryEntry object you do so with a username
and password. This call doesn't attach to the AD with a bad username
and password. That's why the user is considered authenticated if
found.

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #2
> That feature is by design - if you think about it - you never actually
'see' passwords anywhere in Windows.

When you create the DirectoryEntry object you do so with a username
and password. This call doesn't attach to the AD with a bad username
and password. That's why the user is considered authenticated if
found.


That's what I thought (at first). I'm not the LDAP guru so I searched in
msdn and found this:
http://msdn.microsoft.com/library/de...m_asp__net.asp

I used the LdapAuthentication class from the link above. The following
method throws an Exception:

public bool IsAuthenticated(String domain, String username, String pwd)
{
String domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new
DirectoryEntry(_path,domainAndUsername, pwd);

try
{
//Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;

DirectorySearcher search = new DirectorySearcher(entry);

search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();

if(null == result)
{
return false;
}

//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}

return true;
}

@ Object obj = entry.NativeObject;

It throws an System.Runtime.InteropServices.COMException with the
message "Der Authentifizierungmethode ist unbekannt" same as "unknown
authentication mode" in english.
I tried to add AuthenticationTypes.Anonymous to the DirectoryEntry
object but then it seems that he doesn't search for any authentication,
but no other AuthType worked.
Also I could not use the Filter "SAMAccountName" (if I uses
'Anonymous'), I have to search for the 'cn'. I don't know if our LDAP is
different to others (it's openldap). If it is so, I have to search for
the solution on other places.
Nov 19 '05 #3
On Fri, 25 Feb 2005 08:30:27 +0100, Dennis Dobslaf <do*****@kdvz.de>
wrote:

Also I could not use the Filter "SAMAccountName" (if I uses
'Anonymous'), I have to search for the 'cn'. I don't know if our LDAP is
different to others (it's openldap). If it is so, I have to search for
the solution on other places.


Oh, that could be. I'd look at the schema and properties available for
openLDAP - it probably does not have a SAMAccountName property as SAM
is Windows terminology. Perhaps you can find an article on
authenticating against openLDAP from another technology like Perl and
port the code.

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #4

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

Similar topics

0
by: jiing | last post by:
<?php $ldapServer="ldap://192.168.1.211"; $ldapPort="389"; $ldapconn=ldap_connect($ldapServer,$ldapPort); $ldaprdn="uid=root, ou=People, dc=aitc, dc=com, dc=tw"; $ldappass="abcd"; ...
7
by: rh0dium | last post by:
Hi all, I believe I am having a fundamental problem with my class and I can't seem to figure out what I am doing wrong. Basically I want a class which can do several specific ldap queries. So...
0
by: for_your_spam_only | last post by:
Hi everybody, please take a look at the "screenshot" (DB2 CLI), Server is V7, FP12 running on WinNT, LDAP service is on AIX, IBM Directory Server 4.1 D:\>db2level DB21085I Instance "DB2"...
7
by: Bernard Lebel | last post by:
Hello, I'm stumbled at a serious problem, and quite frankly getting desparate. This is a rather long-winded one so I'll try to get straight to the point. I have this Python program, that...
1
by: htperth | last post by:
Hi all, Note that I'm new to LDAP and Active Directory and am writing an application that retrieves a simple phone list for all the users in our domain. So far I have the following console app...
1
by: htperth | last post by:
Hi all, Note that I'm new to LDAP and Active Directory and am writing an application that retrieves a simple phone list for all the users in our domain. So far I have the following console app...
0
by: rumich | last post by:
Hi, I have the following problem when trying to retrieve Windows 2003 AD entry (objectclass=person). <?php $ad = ldap_connect($ADhost,$ADldap_port) or die( "Could not connect!" ); $set =...
1
by: Matrixinline | last post by:
Hi I have a application to search an entry in the LDAP server. I tried to run this application and it runs perfectly. But the issue is I am nnot able to Login to the server and searhc for the...
2
by: Lars | last post by:
Hi I got some programming experience and I recently started looking into Python. I've read much of the tutorial from 2.6 documentation. But it was more interesting to get started on something I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.