We help IT Professionals succeed at work.

A/D/ Authentication for Windows Based App

smaguire asked

I have a Windows based application (C# - VS2010) and I would like to authenticate users against active directory. My application talks to MS SQL Server on the back-end and the followings are required when user(s) make any changes to the database:
CreatedBy ==> User name
CreatedByUserId ==> S-1-5-91-4072499873-234284423-3289563812-2580
ModifiedByUserId  ==> looks like something like this from A/D (K-1-5-21-4072499873-234384823-3289563812-2580)

I have not been able to find anything, all examples seems to be for ASP.NET and nothing for Windows based app.

does anyone have a good example on how to build a logon page form using Visual Studio 2010 and .NET framework 4.0?

Watch Question

Vikram Singh SainiSoftware Engineer cum AD Developer

I have not been able to find anything, all examples seems to be for ASP.NET and nothing for Windows based app.

I am not clear about your question. What I am able to interpret is that you are trying to authenticate user from AD. Please share more details how.


I want to have a logon screen where users enter their domain\username and domain password in order to gain access to my application.

once successful logon, users will be able to add/delete or modify records, each modified record must have the logged on user name saved in it for tracking purposes.
I am using Windows forms application and not web based application

thank you
I found this code which which works fine, credit to:

//NOTE: This can be made static with no modifications
public bool ActiveDirectoryAuthenticate(string username, string password)
    bool result = false;
    using (DirectoryEntry _entry = new DirectoryEntry())
        _entry.Username = username;
        _entry.Password = password;
        DirectorySearcher _searcher = new DirectorySearcher(_entry);
        _searcher.Filter = "(objectclass=user)";
            SearchResult _sr = _searcher.FindOne();
            string _name = _sr.Properties["displayname"][0].ToString();
            result = true;
        { /* Error handling omitted to keep code short: remember to handle exceptions !*/ }

    return result; //true = user authenticated!
Distinguished Expert 2019

Are you wanting to impersonate or login as the user for anything or just be able to confirm that their credentials are correct?

It sounds like you need their credentials so that you can have their domain attributes/properties available for recording purposes.  If that's the case, is there any reason to believe that you just can't use the current user identity?

bbaoIT Consultant

better to post your answer and accept it as the solution. you may also assign points to the comments that ever helped you in anyway.
bbaoIT Consultant

BTW, below is an official how-to article about ASP.NET authentication form against AD. example codes are included.

How To: Use Forms Authentication with Active Directory in ASP.NET 2.0


got it working myself