You are here Blog
Sunday, February 05, 2012
Jun 30

Written by: Robert Thomas
6/30/2008 11:08 AM 

Recently I worked on a project where there was a requirement to query an Oracle Internet Directory (OID) server with a user's certificate subject to verifiy if the user existed in the OID directory and return the username to be used as the credential.  This seemed at first like it would be a simple task using the System.DirectoryServices namespace.  Knowing that Oracle OID was LDAP v3 compliant, I developed a prototype on my local machine that queried an Active Directory domain controller.  Everything worked out nicely.  I then moved the code to my development server and pointed to the Oracle LDAP server.  I kept getting an invalid username/password error.  This was interesting because the LDAP server allowed anonymous browsing and did not need credentials.  I scoured the Internet for answers and found many forum posts of people asking how to do this scenario, but no answers.  I wanted to blog about this in case someone else finds themselves in my situation.  The answer turned out to be pretty simple.  The System.DirectoryServices.Protocols namespace had all the classes I needed to talk to a non-AD generic LDAP server.  Here is a snippet of my final code.

string server = "SERVER";
string dn = "DN";
string searchAttribute = "SA";
string usernameAttribute = "UA";

LdapConnection lcon = new LdapConnection(server);
lcon.AuthType = AuthType.Anonymous;
string[] att = new string[1];
att[0] = usernameAttribute;
string filter = "(" + searchAttribute +"=" + criteria.Cert + ")";
SearchRequest sr = new SearchRequest(dn, filter, SearchScope.Subtree,att);
SearchResponse res = (SearchResponse)lcon.SendRequest(sr);
if (res.Entries != null  && res.Entries.Count > 0)
{
DirectoryAttribute da = res.Entries[0].Attributes[usernameAttribute];
this._name = da.GetValues(typeof(string)).GetValue(0).ToString();
if (_name != string.Empty && _name != "")
{
  _isAuthenticated = true;
}
else
{
  _isAuthenticated = false;
}
}
else
{
_isAuthenticated = false;
}

Tags:

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 
Minimize
Blog Search
Minimize
 
Copyright 2008 by Capriccio Software, Inc. Privacy StatementTerms Of Use Xhtml 1.0 CSS 2.0