May be a bit late but to achieve Window Authentication Functionality to a C# Desktop Application, there are two steps accomplish with below steps.
Step 1: Get currently logged in user details:
This is pretty straight forward. we can achieve this by using the WindowsIdentity class of System.Security.Principal
namespace. This class provides a static method, getCurrent()
, which return a object of WindowsIdentity.
Bellow is the code you can use to get the current logged in user details.
Step 2: Validate windows credentials provided by user:
Need to ask domain name, user name, password from user to pass these values to interop service.
This is little complex compared to above as we need to call a windows API using IntropServices. To accomplish this we need to add a extern function declaration, and then call the function. Following code will help you to understand this better.
bool issuccess = false;
string username = GetloggedinUserName();
if (username.ToLowerInvariant().Contains(txtUserName.Text.Trim().ToLowerInvariant()) && username.ToLowerInvariant().Contains(txtDomain.Text.Trim().ToLowerInvariant()))
{
issuccess = IsValidateCredentials(txtUserName.Text.Trim(), txtPwd.Text.Trim(), txtDomain.Text.Trim());
}
if (issuccess)
MessageBox.Show("Successfuly Login !!!");
else
MessageBox.Show("User Name / Password / Domain is invalid !!!");