Indulge in Technology

  Home  |   Contact  |   Syndication    |   Login
  29 Posts | 1 Stories | 39 Comments | 0 Trackbacks

News

Twitter








Tag Cloud


Archives

Post Categories

Today one of my friend asked me how we can perform validating user windows login credentials. In past also i saw in many forums people asking similar questions.
actaully there are 2 different scenarios
1) Get the currently logged in windows user name.
2) Prompt the user for user and password, and validate the provided user name and password are a valid user credentials
following is the c# code which will answer these scenarios.
1)  Get currently logged in user details: This is pretty straight forward approach. 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.

 public string GetloggedinUserName()
{
 
System.Security.Principal.WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent();
return currentUser.Name;
}
2) Validate windows credentials provided by user: 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.

class Program
    {
                     
        [System.Runtime.InteropServices.DllImport("advapi32.dll")]
        public static extern bool LogonUser(string userName, string domainName, string password, int LogonType, int LogonProvider,ref IntPtr phToken);
 
        static void Main(string[] args)
        {
            Program obj = new Program();
            bool isValid = obj.IsValidateCredentials("myUserName","MyPassword","MyDomain");
            Console.WriteLine(isValid == true ? "Valid User details" : "Invalid User Details");
            Console.Read();
        }
               
        public bool IsValidateCredentials(string userName, string password, string domain)
        {
            IntPtr tokenHandler = IntPtr.Zero;
            bool isValid = LogonUser(userName, domain, password, 2, 0, ref tokenHandler);
            return isValid;
        }
    }
 Hope this will be useful for people who want to perform windows authentication in winform applications...
Happy coding 
 
 
posted on Tuesday, July 14, 2009 6:08 AM

Feedback

# re: Windows Authentication in winform Application 11/1/2011 2:52 PM Mohit
Exactly what I was looking for.
Many thanks.

# re: Windows Authentication in winform Application 10/17/2014 4:50 PM asim
hy
i developing android app for remotely logout and login windows 8. there is programm one in android like clint and one in pc or window for window login and logout. both are connected by bluetooth. so have a problem in server program that is who to login window from server code. the logout task is done. so please some one help me
for how to login window from a program that is running in background..
thanx in advance.

Post A Comment
Title:
Name:
Email:
Comment:
Verification: