tags:

views:

1227

answers:

3

I would like to authenticate username and passwords for my application on a windows operating system with any directory service. For example it could be microsoft active directory, Novell eDirecotry, or SunOne. I already know how to do this code natively for Microsoft Active Direcotry with c#. ( I totally gave up using ADSI and creating a low level com component)

The way im attempting to authenticate with Novel eDirecotory is i have installed the Mono project. Inside the mono project they provide you with Novell.Directory.ldap.dll The code looks somewhat the same as for Microsoft Active Directory.(http://www.novell.com/coolsolutions/feature/11204.html)

For SunOne, i have been told to use the same code as active direcotry, but the ldap connecton string is a little different.(http://forums.asp.net/t/354314.aspx) (http://technet.microsoft.com/en-us/library/cc720649.aspx)

To complicate my project, most customers use a "Service account:" which means i need to bind with an administrative username and password before i can authenticate a regular username and password. My questions is in 2 parts.

1) From what I have explained above, is this the correct direction I should be going to authenticate against each individual direcotory service?

2) I feel that i dont not need to do any of this code at all. I also feel the stipulation of using a service account is not imporant at all. If all I care about is authenticating a username and password on a windows machine why do i even need to use ldap? I mean think about it. When you login to your machine in the morning, you do not have to provide a service account just to login. I can easily authenticate a username and password at a DOS prompt by using the runas feature and i will be denied or not and could parse the text file. Im sure there are other ways i could pass a username and password to the windows operating system that i am on and will tell me if a username and password is valid for the domain that it is on. Am i right? If so what suggested ways do you guys have?

Michael Evanchik www.MikeEvanchik.com

+4  A: 

I'm not sure I entirely understand the question, but in some situations I've found it easy to authenticate a user by simply doing a search for their account and using their credentials as the username and password.

A successful query means everything provided was correct, not finding the account means something was wrong.

//use the users credentials for the query
DirectoryEntry root = new DirectoryEntry(
    "LDAP://dc=domain,dc=com", 
    loginUser, 
    loginPassword
    );

//query for the username provided
DirectorySearcher searcher = new DirectorySearcher(
    root, 
    "(sAMAccountName=" + loginUser + ")"
    );    

//a success means the password was right
bool success = false; 
try {
    searcher.FindOne();
    success = true;
}
catch {
    success = false;
}

Probably not "best practice", but might get around your issue you are having...

Hugoware
im assuming above is microsoft AD only, and is using LDAP. Thanks for the code. My question is more of a way to universally, possibly not using LDAP since some LDAP servers require a Service account username and password, just to bind , so you can even try to authenticate a regular user.
mcbain942
That's what I'm suggesting though - use the credentials of the logged in user (assuming you're asking for their username and password) as the service account. The should at the very least be able to query and see their own account. If that works then you know the authentication works. That seems like a fairly universal way to check if a password is correct.
Hugoware
+1  A: 

We had a website that needed to authenticate a username and password against domain credentials and used the LogonUser API function. Use it for a network logon (one of its arguments is logon type) and all it does is validate credentials, it doesn't do things like load the users profile that runas would. Only caveat is that the service account does require sufficient access to call LogonUser. I suggest you check the MSDN documentation for what that access is though because it varies by OS.

pipTheGeek
thank you for leading me down the right path. although your right , u will need local admin in order to make this api call, googling things, if u read this thread they say there is an api call that doesnt not require thishttp://www.vbforums.com/showthread.php?t=240277AcquireCredentialsHandleNT with is in security.dll
mcbain942
If you are on 2003 then LogonUser doesn't require SE_TCB_NAME. I don't think just calling AcquireCredentialsHandle will check that they are valid. The code is simulating a full client/server authentication check. Can't see anything wrong with that though, other than the obvious loads of code.
pipTheGeek
A: 

All this can be done with System.DirectoryServices.Protocols. If you create an LdapConnection to the directory you can use the service account to bind with, and then make a subsequent bind to authenticate the credentials.

The service account is generally used to limit access to the authentication mechanism of the server. This way no random person on the street can try to auth with your LDAP server.

Also, do you expect that each user will provide their distinguished name when logging in? With Active Directory, only the sAMAccountName is required, yet other providers like eDirectory and SunONE require the distinguished name for authentication.

To perform this type of authentication, you would need to use the service account that is provided to authenticate to the server, perform a search for a user with the given username, and grab that users distinguished name. You can then authenticate using that distinguished name and the password that was provided.

This will work for all LDAP systems, with the exception of Active Directory which will be happy with just the sAMAccountName.

Jared