We help IT Professionals succeed at work.

A/D/ Authentication for Windows Based App

smaguire
smaguire asked
on
hi,

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?

Thanks
Comment
Watch Question

Vikram Singh SainiSoftware Engineer cum AD Developer

Commented:
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.

Author

Commented:
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
Commented:
I found this code which which works fine, credit to:
http://stackoverflow.com/questions/11665204/how-to-login-to-active-directory


//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)";
        try
        {
            SearchResult _sr = _searcher.FindOne();
            string _name = _sr.Properties["displayname"][0].ToString();
            result = true;
        }
        catch
        { /* Error handling omitted to keep code short: remember to handle exceptions !*/ }
    }

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

Commented:
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?

-saige-
bbaoIT Consultant

Commented:
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

Commented:
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
https://msdn.microsoft.com/en-us/library/ff650308.aspx

Author

Commented:
got it working myself