dasdwqwedwqeqweqweqweqwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

 
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(String username, String password)
throws javax.naming.NamingException {
SearchControls sc;
NamingEnumeration ne;
Hashtable<String,String> h = new Hashtable<String,String>();

h.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
h.put(Context.PROVIDER_URL, "ldap://" + hostname + ":" + port);

if (usessl)
h.put(Context.SECURITY_PROTOCOL, "ssl");
if (servicedn != null) {
h.put(Context.SECURITY_AUTHENTICATION, "simple");
h.put(Context.SECURITY_PRINCIPAL, servicedn);
h.put(Context.SECURITY_CREDENTIALS, servicepassword);
}
DirContext ctx = new InitialDirContext(h);

String dn = "uid=" + username + ",ou=people," + base;
ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);

try {
sc = new SearchControls();
sc.setSearchScope(SearchControls.OBJECT_SCOPE);
ne = ctx.search(dn, "(objectClass=*)", sc);
} catch (javax.naming.AuthenticationException e) {
return false;
}
return true;
}

The DOTNET code looks like:

static void Main(string [] args) {

String ldapAuthPath =
"LDAP://ldap.xxx.com/uid=xxx,ou=people,dc=xxx,dc=com";
String userName = "xxx";
String password = "pass";

DirectoryEntry rootEntry = null;
DirectorySearcher searcher = null;
SearchResult searchResult = null;

try {

rootEntry = new DirectoryEntry();

rootEntry.Path = ldapAuthPath;
rootEntry.Username = userName;
rootEntry.Password = password;
rootEntry.AuthenticationType = AuthenticationTypes.None;

searcher = new DirectorySearcher(rootEntry);
searcher.SearchScope = SearchScope.OneLevel;
searchResult = searcher.FindOne();

// if no exception the user was verified
Console.WriteLine("authenticated");
} catch (Exception e) {
// if exception user was not authenticated
Console.WriteLine(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
 mbasil77 NO[at]SPAM gmail.com
8/25/2006 4:10:27 PM
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

[quoted text, click to view]
Willy Denoyette [MVP] wrote:
> 1. DirectoryEntry.UserName and Password are properties used to authenticate
> the bind, you pecified an AuthenticationType.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(DirectoryEntry user = new
> DirectoryEntry("LDAP://ldap.xxx.com/CN=xxx,ou=people,DC=....")
> {
> try
> {
> PropertyCollection pcoll = user.Properties; // this will effectively
> trigger the bind
> Console.WriteLine(user.Properties["cn"].Value); // get a property
> }
> catch (DirectoryServicesCOMException ex)
> {
> Console.WriteLine(ex.Message);
> }
> }
> Here you'll bind anonymously against the cn=xxxx, ou=people object in the
> directory on ldap.xxx.com
>
> Willy.
>
>
> <mbasil77@gmail.com> wrote in message
> news:1156522673.860368.173300@h48g2000cwc.googlegroups.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(String username, String password)
> | throws javax.naming.NamingException {
> | SearchControls sc;
> | NamingEnumeration ne;
> | Hashtable<String,String> h = new Hashtable<String,String>();
> |
> | h.put(Context.INITIAL_CONTEXT_FACTORY,
> | "com.sun.jndi.ldap.LdapCtxFactory");
> | h.put(Context.PROVIDER_URL, "ldap://" + hostname + ":" + port);
> |
> | if (usessl)
> | h.put(Context.SECURITY_PROTOCOL, "ssl");
> | if (servicedn != null) {
> | h.put(Context.SECURITY_AUTHENTICATION, "simple");
> | h.put(Context.SECURITY_PRINCIPAL, servicedn);
> | h.put(Context.SECURITY_CREDENTIALS, servicepassword);
> | }
> | DirContext ctx = new InitialDirContext(h);
> |
> | String dn = "uid=" + username + ",ou=people," + base;
> | ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
> | ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
> | ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
> |
> | try {
> | sc = new SearchControls();
> | sc.setSearchScope(SearchControls.OBJECT_SCOPE);
> | ne = ctx.search(dn, "(objectClass=*)", sc);
> | } catch (javax.naming.AuthenticationException e) {
> | return false;
> | }
> | return true;
> | }
> |
> | The DOTNET code looks like:
> |
> | static void Main(string [] args) {
> |
> | String ldapAuthPath =
> | "LDAP://ldap.xxx.com/uid=xxx,ou=people,dc=xxx,dc=com";
> | String userName = "xxx";
> | String password = "pass";
> |
> | DirectoryEntry rootEntry = null;
> | DirectorySearcher searcher = null;
> | SearchResult searchResult = null;
> |
> | try {
> |
> | rootEntry = new DirectoryEntry();
> |
> | rootEntry.Path = ldapAuthPath;
> | rootEntry.Username = userName;
> | rootEntry.Password = password;
> | rootEntry.AuthenticationType = AuthenticationTypes.None;
> |
> | searcher = new DirectorySearcher(rootEntry);
> | searcher.SearchScope = SearchScope.OneLevel;
> | searchResult = searcher.FindOne();
> |
> | // if no exception the user was verified
> | Console.WriteLine("authenticated");
> | } catch (Exception e) {
> | // if exception user was not authenticated
> | Console.WriteLine(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
> |
 Willy Denoyette [MVP]
8/25/2006 7:42:15 PM
1. DirectoryEntry.UserName and Password are properties used to authenticate
the bind, you pecified an AuthenticationType.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(DirectoryEntry user = new
DirectoryEntry("LDAP://ldap.xxx.com/CN=xxx,ou=people,DC=....")
{
try
{
PropertyCollection pcoll = user.Properties; // this will effectively
trigger the bind
Console.WriteLine(user.Properties["cn"].Value); // get a property
}
catch (DirectoryServicesCOMException ex)
{
Console.WriteLine(ex.Message);
}
}
Here you'll bind anonymously against the cn=xxxx, ou=people object in the
directory on ldap.xxx.com

Willy.


[quoted text, click to view]
<mbasil77@gmail.com> wrote in message
news:1156522673.860368.173300@h48g2000cwc.googlegroups.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(String username, String password)
| throws javax.naming.NamingException {
| SearchControls sc;
| NamingEnumeration ne;
| Hashtable<String,String> h = new Hashtable<String,String>();
|
| h.put(Context.INITIAL_CONTEXT_FACTORY,
| "com.sun.jndi.ldap.LdapCtxFactory");
| h.put(Context.PROVIDER_URL, "ldap://" + hostname + ":" + port);
|
| if (usessl)
| h.put(Context.SECURITY_PROTOCOL, "ssl");
| if (servicedn != null) {
| h.put(Context.SECURITY_AUTHENTICATION, "simple");
| h.put(Context.SECURITY_PRINCIPAL, servicedn);
| h.put(Context.SECURITY_CREDENTIALS, servicepassword);
| }
| DirContext ctx = new InitialDirContext(h);
|
| String dn = "uid=" + username + ",ou=people," + base;
| ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
| ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
| ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
|
| try {
| sc = new SearchControls();
| sc.setSearchScope(SearchControls.OBJECT_SCOPE);
| ne = ctx.search(dn, "(objectClass=*)", sc);
| } catch (javax.naming.AuthenticationException e) {
| return false;
| }
| return true;
| }
|
| The DOTNET code looks like:
|
| static void Main(string [] args) {
|
| String ldapAuthPath =
| "LDAP://ldap.xxx.com/uid=xxx,ou=people,dc=xxx,dc=com";
| String userName = "xxx";
| String password = "pass";
|
| DirectoryEntry rootEntry = null;
| DirectorySearcher searcher = null;
| SearchResult searchResult = null;
|
| try {
|
| rootEntry = new DirectoryEntry();
|
| rootEntry.Path = ldapAuthPath;
| rootEntry.Username = userName;
| rootEntry.Password = password;
| rootEntry.AuthenticationType = AuthenticationTypes.None;
|
| searcher = new DirectorySearcher(rootEntry);
| searcher.SearchScope = SearchScope.OneLevel;
| searchResult = searcher.FindOne();
|
| // if no exception the user was verified
| Console.WriteLine("authenticated");
| } catch (Exception e) {
| // if exception user was not authenticated
| Console.WriteLine(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
|

 Willy Denoyette [MVP]
8/26/2006 1:39:14 PM

[quoted text, click to view]
<mbasil77@gmail.com> wrote in message
news:1156547427.862872.269100@m79g2000cwm.googlegroups.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
AuthenticationType.SecureSocketsLayer 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.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值