473,799 Members | 2,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LDAP issue

I'm trying to port a piece of Java LDAP conneciton code to DOTNET.
I've done LDAP in DOTNET before, but I keep getting a very strange
message. The Java code looks like:

public static boolean authenticate(St ring username, String password)
throws javax.naming.Na mingException {
SearchControls sc;
NamingEnumerati on ne;
Hashtable<Strin g,Stringh = new Hashtable<Strin g,String>();

h.put(Context.I NITIAL_CONTEXT_ FACTORY,
"com.sun.jndi.l dap.LdapCtxFact ory");
h.put(Context.P ROVIDER_URL, "ldap://" + hostname + ":" + port);

if (usessl)
h.put(Context.S ECURITY_PROTOCO L, "ssl");
if (servicedn != null) {
h.put(Context.S ECURITY_AUTHENT ICATION, "simple");
h.put(Context.S ECURITY_PRINCIP AL, servicedn);
h.put(Context.S ECURITY_CREDENT IALS, servicepassword );
}
DirContext ctx = new InitialDirConte xt(h);

String dn = "uid=" + username + ",ou=people ," + base;
ctx.addToEnviro nment(Context.S ECURITY_AUTHENT ICATION, "simple");
ctx.addToEnviro nment(Context.S ECURITY_PRINCIP AL, dn);
ctx.addToEnviro nment(Context.S ECURITY_CREDENT IALS, password);

try {
sc = new SearchControls( );
sc.setSearchSco pe(SearchContro ls.OBJECT_SCOPE );
ne = ctx.search(dn, "(objectClass=* )", sc);
} catch (javax.naming.A uthenticationEx ception e) {
return false;
}
return true;
}

The DOTNET code looks like:

static void Main(string [] args) {

String ldapAuthPath =
"LDAP://ldap.xxx.com/uid=xxx,ou=peop le,dc=xxx,dc=co m";
String userName = "xxx";
String password = "pass";

DirectoryEntry rootEntry = null;
DirectorySearch er searcher = null;
SearchResult searchResult = null;

try {

rootEntry = new DirectoryEntry( );

rootEntry.Path = ldapAuthPath;
rootEntry.Usern ame = userName;
rootEntry.Passw ord = password;
rootEntry.Authe nticationType = AuthenticationT ypes.None;

searcher = new DirectorySearch er(rootEntry);
searcher.Search Scope = SearchScope.One Level;
searchResult = searcher.FindOn e();

// if no exception the user was verified
Console.WriteLi ne("authenticat ed");
} catch (Exception e) {
// if exception user was not authenticated
Console.WriteLi ne(e.ToString() );
}
}

I keep getting a message that the dn syntax is invalid. I've tried
various combinations of things. The Java code does not supply a
userName, but when I try to do this in DOTNET I get a invalid username
error.

Any ideas would be appreciated. It seems that the DOTNET API doesn't
offer the same degree of control.

mb

Aug 25 '06 #1
3 7086
1. DirectoryEntry. UserName and Password are properties used to authenticate
the bind, you pecified an AuthenticationT ype.None that means you don't need
to specify the user credentials to bind.
2. You have (there are other options though) to specify the CN of the object
to bind to, like this:
using(Directory Entry user = new
DirectoryEntry( "LDAP://ldap.xxx.com/CN=xxx,ou=peopl e,DC=....")
{
try
{
PropertyCollect ion pcoll = user.Properties ; // this will effectively
trigger the bind
Console.WriteLi ne(user.Propert ies["cn"].Value); // get a property
}
catch (DirectoryServi cesCOMException ex)
{
Console.WriteLi ne(ex.Message);
}
}
Here you'll bind anonymously against the cn=xxxx, ou=people object in the
directory on ldap.xxx.com

Willy.
<mb******@gmail .comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
| I'm trying to port a piece of Java LDAP conneciton code to DOTNET.
| I've done LDAP in DOTNET before, but I keep getting a very strange
| message. The Java code looks like:
|
| public static boolean authenticate(St ring username, String password)
| throws javax.naming.Na mingException {
| SearchControls sc;
| NamingEnumerati on ne;
| Hashtable<Strin g,Stringh = new Hashtable<Strin g,String>();
|
| h.put(Context.I NITIAL_CONTEXT_ FACTORY,
| "com.sun.jndi.l dap.LdapCtxFact ory");
| h.put(Context.P ROVIDER_URL, "ldap://" + hostname + ":" + port);
|
| if (usessl)
| h.put(Context.S ECURITY_PROTOCO L, "ssl");
| if (servicedn != null) {
| h.put(Context.S ECURITY_AUTHENT ICATION, "simple");
| h.put(Context.S ECURITY_PRINCIP AL, servicedn);
| h.put(Context.S ECURITY_CREDENT IALS, servicepassword );
| }
| DirContext ctx = new InitialDirConte xt(h);
|
| String dn = "uid=" + username + ",ou=people ," + base;
| ctx.addToEnviro nment(Context.S ECURITY_AUTHENT ICATION, "simple");
| ctx.addToEnviro nment(Context.S ECURITY_PRINCIP AL, dn);
| ctx.addToEnviro nment(Context.S ECURITY_CREDENT IALS, password);
|
| try {
| sc = new SearchControls( );
| sc.setSearchSco pe(SearchContro ls.OBJECT_SCOPE );
| ne = ctx.search(dn, "(objectClass=* )", sc);
| } catch (javax.naming.A uthenticationEx ception e) {
| return false;
| }
| return true;
| }
|
| The DOTNET code looks like:
|
| static void Main(string [] args) {
|
| String ldapAuthPath =
| "LDAP://ldap.xxx.com/uid=xxx,ou=peop le,dc=xxx,dc=co m";
| String userName = "xxx";
| String password = "pass";
|
| DirectoryEntry rootEntry = null;
| DirectorySearch er searcher = null;
| SearchResult searchResult = null;
|
| try {
|
| rootEntry = new DirectoryEntry( );
|
| rootEntry.Path = ldapAuthPath;
| rootEntry.Usern ame = userName;
| rootEntry.Passw ord = password;
| rootEntry.Authe nticationType = AuthenticationT ypes.None;
|
| searcher = new DirectorySearch er(rootEntry);
| searcher.Search Scope = SearchScope.One Level;
| searchResult = searcher.FindOn e();
|
| // if no exception the user was verified
| Console.WriteLi ne("authenticat ed");
| } catch (Exception e) {
| // if exception user was not authenticated
| Console.WriteLi ne(e.ToString() );
| }
| }
|
| I keep getting a message that the dn syntax is invalid. I've tried
| various combinations of things. The Java code does not supply a
| userName, but when I try to do this in DOTNET I get a invalid username
| error.
|
| Any ideas would be appreciated. It seems that the DOTNET API doesn't
| offer the same degree of control.
|
| mb
|
Aug 25 '06 #2
I did a network trace and I think I see the issue. The Java code
switches over to SSLv3, whereas the DOTNET code does not. Anyone know
how to set that?

mb

Willy Denoyette [MVP] wrote:
1. DirectoryEntry. UserName and Password are properties used to authenticate
the bind, you pecified an AuthenticationT ype.None that means you don't need
to specify the user credentials to bind.
2. You have (there are other options though) to specify the CN of the object
to bind to, like this:
using(Directory Entry user = new
DirectoryEntry( "LDAP://ldap.xxx.com/CN=xxx,ou=peopl e,DC=....")
{
try
{
PropertyCollect ion pcoll = user.Properties ; // this will effectively
trigger the bind
Console.WriteLi ne(user.Propert ies["cn"].Value); // get a property
}
catch (DirectoryServi cesCOMException ex)
{
Console.WriteLi ne(ex.Message);
}
}
Here you'll bind anonymously against the cn=xxxx, ou=people object in the
directory on ldap.xxx.com

Willy.
<mb******@gmail .comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
| I'm trying to port a piece of Java LDAP conneciton code to DOTNET.
| I've done LDAP in DOTNET before, but I keep getting a very strange
| message. The Java code looks like:
|
| public static boolean authenticate(St ring username, String password)
| throws javax.naming.Na mingException {
| SearchControls sc;
| NamingEnumerati on ne;
| Hashtable<Strin g,Stringh = new Hashtable<Strin g,String>();
|
| h.put(Context.I NITIAL_CONTEXT_ FACTORY,
| "com.sun.jndi.l dap.LdapCtxFact ory");
| h.put(Context.P ROVIDER_URL, "ldap://" + hostname + ":" + port);
|
| if (usessl)
| h.put(Context.S ECURITY_PROTOCO L, "ssl");
| if (servicedn != null) {
| h.put(Context.S ECURITY_AUTHENT ICATION, "simple");
| h.put(Context.S ECURITY_PRINCIP AL, servicedn);
| h.put(Context.S ECURITY_CREDENT IALS, servicepassword );
| }
| DirContext ctx = new InitialDirConte xt(h);
|
| String dn = "uid=" + username + ",ou=people ," + base;
| ctx.addToEnviro nment(Context.S ECURITY_AUTHENT ICATION, "simple");
| ctx.addToEnviro nment(Context.S ECURITY_PRINCIP AL, dn);
| ctx.addToEnviro nment(Context.S ECURITY_CREDENT IALS, password);
|
| try {
| sc = new SearchControls( );
| sc.setSearchSco pe(SearchContro ls.OBJECT_SCOPE );
| ne = ctx.search(dn, "(objectClass=* )", sc);
| } catch (javax.naming.A uthenticationEx ception e) {
| return false;
| }
| return true;
| }
|
| The DOTNET code looks like:
|
| static void Main(string [] args) {
|
| String ldapAuthPath =
| "LDAP://ldap.xxx.com/uid=xxx,ou=peop le,dc=xxx,dc=co m";
| String userName = "xxx";
| String password = "pass";
|
| DirectoryEntry rootEntry = null;
| DirectorySearch er searcher = null;
| SearchResult searchResult = null;
|
| try {
|
| rootEntry = new DirectoryEntry( );
|
| rootEntry.Path = ldapAuthPath;
| rootEntry.Usern ame = userName;
| rootEntry.Passw ord = password;
| rootEntry.Authe nticationType = AuthenticationT ypes.None;
|
| searcher = new DirectorySearch er(rootEntry);
| searcher.Search Scope = SearchScope.One Level;
| searchResult = searcher.FindOn e();
|
| // if no exception the user was verified
| Console.WriteLi ne("authenticat ed");
| } catch (Exception e) {
| // if exception user was not authenticated
| Console.WriteLi ne(e.ToString() );
| }
| }
|
| I keep getting a message that the dn syntax is invalid. I've tried
| various combinations of things. The Java code does not supply a
| userName, but when I try to do this in DOTNET I get a invalid username
| error.
|
| Any ideas would be appreciated. It seems that the DOTNET API doesn't
| offer the same degree of control.
|
| mb
|
Aug 25 '06 #3

<mb******@gmail .comwrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.com.. .
|I did a network trace and I think I see the issue. The Java code
| switches over to SSLv3, whereas the DOTNET code does not. Anyone know
| how to set that?
|
It will save you a lot of time if you would start reading the doc's on MSDN,
that said, ff you need to bind using SSL you'll have to set the
AuthenticationT ype.SecureSocke tsLayer when creating an instance of
DirectoryEntry. Note that this requires a Certificate Server running on the
AD server, but I guess you aren't even connecting to a Windows LDAP server
(Active Directory server), so I can't guarantee this will even work in your
environment. Note that simple bind should work also, what happens when you
run the sample I posted?
Willy.

Aug 26 '06 #4

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

Similar topics

0
2676
by: Durairaj Avasi | last post by:
Here is my prg:::: use Net::LDAP qw(LDAP_SUCCESS LDAP_PROTOCOL_ERROR); use Authen::SASL; use Net::LDAP::Util qw(ldap_error_name ldap_error_text); sub lConnect { my $server = shift; print " the server name is $server\n"; my $ldap = Net::LDAP->new($server, port=> 389, version => 3);
3
3783
by: Fie Fie Niles | last post by:
We need to use LDAP in conjunction with our ASP pages. Are there LDAP API that can be used either from VB or VBScript ? Where can I find sample codes for it ? Thank you very much.
7
6816
by: Amar | last post by:
I am trying to connect to my college LDAP directory using ASP.NET. This LDap does not have security as it returns only user demographic information. i do not need to bind with a username or credentials. What i am trying to do is, i am trying to look up all the information for the user with user id 'testuser'. The following is the Vb.net code for my aspx page: Dim oRoot As DirectoryEntry = New...
1
4760
by: Andrew | last post by:
Hey all, Working on revamping our Intranet here and making use of the LDPA, Active Directory, Directory Services, etc. that .Net provides. I am still fairly new on this subject, so the problem I have run into I am not sure how to fix, and really not sure what is causing it. Here's what is going on (test server - Windows 2003 Server): I have a page in a folder (under anonymous authentication in IIS6) that has a link on it that...
5
2345
by: Bryan | last post by:
Hello, I have a asp.net app working with directory services on my Windows XP development machine. However when I moved the application over to our production server (Win 2000 Server) it no longer works. I use this code to try to connect to ldap: System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry(LDAP://corp.mydomain.com,user,pass); I tried using my user name and password, the domain admin...
4
2037
by: h2so4 | last post by:
I want to write a program that will query an ldap directory. can I use adsi or ado to do that, If yes how ? tx -- h2so4
3
5510
by: sallas | last post by:
Hi, I have a simple LDAPS script: #!/usr/bin/python2.3 import sys import ldap if __name__ == '__main__': ldap.set_option(ldap.OPT_DEBUG_LEVEL,255)
0
1866
by: Sells, Fred | last post by:
I'm running python 2.5 (or 2.4) in an XP environment. I downloaded and installed the .dll's from OpenLDAP-2.4.8+OpenSSL-0.9.8g-Win32.zip and copied the .dll's in c:/windows/system32 as instructed now I get this error. Is there anyway to avoid building the python_ldap binaries? Apart from being lazy, I've got a secure system policy issue if I start compiling apps. I could give up and just start running in linux, but myxp environment is...
1
2112
by: =?Utf-8?B?SHV0dHk=?= | last post by:
I am new at trying to authenticate users against LDAP. I am getting the error "Unable to establish secure connection with the server" with my current code. Here's what I have thus far. <add name="ADConnectionString" connectionString="LDAP://ldap-r.hutty.com:389/cn=finmed,ou=roles,dc=hutty,dc=com"/> <add name="MyADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0,...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10473
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10025
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.