Logging in through Active Directory using ASP.NET with LDAP

Hello,

I'm creating a script that helps me to log in into active directory on a windows 2000 server using ldap authentication.
But now I've got a little problem. When I want to connect to the server using this script:
LdapAuthentication.cs
[code]
using System;
using System.Text;
using System.Collections;
using System.DirectoryServices;

namespace FormsAuthAd
{
public class LdapAuthentication
{
private String _path;
private String _filterAttribute;


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

public bool isAuthenticated(String domain, String username, String password)
{
String domainAndUsername = domain + @"" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, password);

try
{
// Het koppelen van het AdsObject om authenticatie te forceren
Object obj = entry.NativeObject;

DirectorySearcher search = new DirectorySearcher(entry);

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

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

// Updaten van het nieuwe pad naar de gebruiker in de 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 groupdNames = 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(equalsIndex == -1)
{
return null;
}

// EERSTE APPEND MOET EVEN NAGEKEKEN WORDEN OP HET INTERNET !!!!!!!!!!!!!!!!!!!!!!!
groupdNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) -1));
groupdNames.Append("|");
}
}
catch(Exception ex)
{
throw new Exception("Error obtaining group names. " + ex.Message);
}

return groupdNames.ToString();
}
}
}
[/code]
And the form:
[code]
<%@ Page language="c#" AutoEventWireup="true" %>
<%@ Import Namespace="FormsAuthAd" %>

void Login_Click(Object sender, EventArgs e)
{
String adPath = "LDAP://dc=intern,dc=esloo,dc=nl"; //Path to you LDAP directory server
LdapAuthentication adAuth = new LdapAuthentication(adPath);
try
{
if(true == adAuth.isAuthenticated(txtDomain.Text, txtUsername.Text, txtPassword.Text))
{
String groups = adAuth.getGroups();

//Create the ticket, and add the groups.
bool isCookiePersistent = chkPersist.Checked;
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, "<?xm-deletion_mark author="Administrator" time="20030530T152131+05-30" data="_"?>txtUsername.Text",DateTime.Now, DateTime.Now.AddMinutes(60), isCookiePersistent, groups);

//Encrypt the ticket.
String encryptedTicket = FormsAuthentication.Encrypt(authTicket);

//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);

//You can redirect now.
Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text, false));
}
else
{
errorLabel.Text = "Authentication did not succeed. Check user name and password.";
}
}
catch(Exception ex)
{
errorLabel.Text = "Error authenticating. " + ex.Message;
}
}




Domain:


Username:


Password:










[/code]

I get the error that the domain controller could not be found. Is it because I don't have the rights to search for passwords in the domain controller. I don't think it's the script I'm using because I went over and over it and didn't find anything.

I hope that somebody has the answer. Thanx in advance.

--=][tReShR][=--
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories