Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

How can i connect LDAP and query AD to authorize Domain User Name and Password?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hughs Man - 25 Mar 2008 10:24 GMT
I want to develop a C# Web Base Logon to connect a Novell LDAP Server with
SSL and then authorize the AD Domain User Name and Password

I have written the following source code to make a connection to Novell LDAP
Server, but i do not know how to authorize AD User Name and Password. And
can it connect to Novell LDAP?

Here are my source code:

   public class LdapAuthentication
   {
       private string _path;
       private string _filterAttribute;

       public LdapAuthentication(string path)
       {
           _path = path;
       }

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

           DirectoryEntry entry = new DirectoryEntry(_path, username, 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;
       }

       public string GetGroups()
       {
           DirectorySearcher search = new DirectorySearcher(_path);
           search.Filter = "(cn=" + _filterAttribute + ")";
           search.PropertiesToLoad.Add("memberOf");
           StringBuilder groupNames = new StringBuilder();

           try
           {
               SearchResult result = search.FindOne();
               int propertyCount = result.Properties["memberOf"].Count;
               string dn;
               int equalsIndex, commaIndex;

               for (int propertyCounter = 0; propertyCounter <
propertyCount; propertyCounter++)
               {
                   dn =
(string)result.Properties["memberOf"][propertyCounter];
                   equalsIndex = dn.IndexOf("=", 1);
                   commaIndex = dn.IndexOf(",", 1);
                   if (-1 == equalsIndex)
                   {
                       return null;
                   }
                   groupNames.Append(dn.Substring((equalsIndex + 1),
(commaIndex - equalsIndex) - 1));
                   groupNames.Append("|");
               }
           }
           catch (Exception ex)
           {
               throw new Exception("Error obtaining group names. " +
ex.Message);
           }
           return groupNames.ToString();
       }
   }

===========================================================================================================================================

string adPath = "LDAP://ldap.abc.com:636/ou=Users,o=AUTH";

LdapAuthentication adAuth = new LdapAuthentication(adPath);

if (true == adAuth.IsAuthenticated("ad.abc.com", "cn=TVAT,ou=eSERV,o=AUTH",
"123456789"))
{
     string groups = adAuth.GetGroups();

     //Create the ticket, and add the groups.
     bool isCookiePersistent = true;
     FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, "UserNameHERE", DateTime.Now,
DateTime.Now.AddMinutes(60), isCookiePersistent, groups);

     //Encrypt the ticket.
     Create a cookie, and then add the encrypted ticket to the cookie as
data.
     HttpCookie authCookie = new
HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

     if (true == isCookiePersistent)
           authCookie.Expires = authTicket.Expiration;

     //Add the cookie to the outgoing cookies collection.
     Response.Cookies.Add(authCookie);

     Response.Redirect("urlHERE");
}
else
{
      Message.Text = "Authentication did not succeed. Check user name and
password.";

      return;
}
Matteo Migliore - 25 Mar 2008 10:28 GMT
> I want to develop a C# Web Base Logon to connect a Novell LDAP Server with
> SSL and then authorize the AD Domain User Name and Password

This is an italian news group, so you have to post in
italian language.

Thanks.

Signature

Matteo Migliore
Blog: http://blogs.ugidotnet.org/matteomigliore

"E' più facile unire le stupidità
che far collaborare le intelligenze."

Kbalz - 25 Mar 2008 14:11 GMT
On Mar 25, 5:28 am, "Matteo Migliore" <matteo.migliore....@gmail.com>
wrote:
> > I want to develop a C# Web Base Logon to connect a Novell LDAP Server with
> > SSL and then authorize the AD Domain User Name and Password
[quoted text clipped - 10 lines]
> "E' più facile unire le stupidità
> che far collaborare le intelligenze."

Uhh? No its not.
Bjorn Brox - 25 Mar 2008 15:12 GMT
Kbalz skrev:
> On Mar 25, 5:28 am, "Matteo Migliore" <matteo.migliore....@gmail.com>
> wrote:
[quoted text clipped - 13 lines]
>
> Uhh? No its not.

It's a multi-post where the group microsoft.public.it.dotnet.csharp is
included.

Signature

Bjorn Brox


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.