views:

98

answers:

3

I have a domain and port number (636) as well as a username and password.

I am trying to figure out a way to connect to this AD via Secure LDAP and get a users 'givenname', 'sn', 'mail', and probably a few custom attributes.

However I have no idea how to do this in C#.

I think that Microsoft may have a method for this available already but I am going to defer to you all.

The final user experience will be: See login screen, enter username and password, those credentials are sent over LDAP and the users info is returned to my web app, then I log them in if it all went well... though I don't know what a failed attempt would look like either so I can deny them. Any ideas?

Please include code samples so I can understand the implementation, thanks!

+3  A: 

Did you even try google?

EDIT

Sorry for the hubub and the snarky response. I think the problem you were having is you didn't quite ask the question right -- either here or on google. Anyhow, you don't need a lick of C# code here. You just need to configure your web app to use AD as a membership provider. You'll need a connection string [getting this right was the hardest part]:

<connectionStrings>
    <add name="MyAd"
         connectionString="LDAP://adserver/OU=Users"
         />
</connectionStrings>

And a membership provider:

<membership defaultProvider="AdProvider">
        <providers>
            <add 
                name="AdProvider"
                type="System.Web.Security.ActiveDirectoryMembershipProvider, 
                    System.Web, Version=2.0.0.0, 
                    Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
                connectionStringName="MyAd"
                applicationName="ItRemoteHelpdesk"
                enablePasswordReset="false"
                 />
        </providers>
    </membership>

Then users can login with their normal username@domain and password.

Wyatt Barnett
@Shogun, seriously!! you just asked us to build a **solution** for you, not answer a *question*. If all you want is a question answered, then yes, it's possible.
Brad
@Brad, I did not, I simply asked for code samples and information on what C# Library to use... and the point of asking on SO is to help add to the site and get advice from the users, for example some pitfalls that people might have run into, real time advice... wow, just.. wow..
shogun
@Shogun, your question is just simply too broad. I wrote an entire Dll to custom-wrap .NET's AD authentication. Would that be suitable to post? Probably not.
Brad
Thanks Wyatt, do you know if it's possible to have this along side of normal login? Most accounts in the web app will login in directly to the web app using normal username/password lookup in our DB, but some special accounts will be flagged to login VIA their AD.
shogun
Never tried it, but you've got the same considerations here as you would with any other multi-membership provider application.
Wyatt Barnett
hmm ok, also the domain information will be stored in the database so that my solution could be used for multiple accounts to different domains, etc, thanks for the input
shogun
+1  A: 

The System.DirectoryServices.AccountManagement is the .NET dll to use for the newer, non-LDAP AD authentication.

Try this website for a good starting point with code examples:

http://www.codeproject.com/KB/system/usingAccountManagement.aspx

Brad
it has to be Secure LDAP.. so I'm guessing 'non-LDAP' is not using Secure LDAP, right?
shogun
@Shogun, you can use the `ContextOptions` enum when creating your `Context` (the link to the DC) to specify that you want SSL http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.contextoptions.aspx
Brad
@Brad, ok thanks.. sorry for being so snappy, it's been a long week..
shogun
all's well that ends well. :)
Brad
@Brad, one more question about this link provided, is this actually using the Secure LDAP protocol? Our partner has only allowed for this protocol from a specific IP address on our side.
shogun
@Shogoun, I cannot help you there. It wasn't required in my implementation.
Brad
A: 

You should definitely check out the .NET 3.5 System.DirectoryServices.AccountManagement namespace as suggested by Brad.

To get a good head start on how to use it, read this MSDN Magazine article: Managing Directory Security Principals in the .NET Framework 3.5

The article does talk several times about how to securely (using SSL) connect to your AD domain, and how to e.g. create users or retrieve user information. I think reading that article closely and trying out the code samples should give you a good idea on how to do what you're looking for.

Update: quite obviously, all those method in S.DS.AM require you to be authenticated against AD. The new classes also provide for pretty simple verification of user credentials (as shown in that article I linked to):

// establish context 
PrincipalContext domain = new PrincipalContext(ContextType.Domain);

// determine whether a user can validate to the directory
bool validated = domain.ValidateCredentials("user1", "Password1");
marc_s
I'm confused. I am trying to have AD users login with their username and password to a client AD through my web app. This looks like I can just look up any user without a password... How then do I authenticate someone? And how am I gaining access, some admin password? More reading I must do... this seem so... overly complicated!
shogun
@Shogun: of course, you can only ever look someone up if you're already authenticated. Read that article I'm linking to!! It also shows you how to easily validate credentials against AD, and how to login a given user. Only when **logged in** can he do all the other stuff, of course!!
marc_s
Oh... the client gave me a test user but no admin account or anything like that, I guess I need to go back and ask for one? Or can I authenticate a normal user? Please clarify, I am very confused here..
shogun
@Shogun: that depends on the Active Directory configuration, but by default, any domain user can at least from the AD (at least parts of it)
marc_s