Geeks With Blogs
Indulge in Technology
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
        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");
        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 .Net | Back to top

Comments on this post: Windows Authentication in winform Application

# re: Windows Authentication in winform Application
Requesting Gravatar...
Exactly what I was looking for.
Many thanks.
Left by Mohit on Nov 01, 2011 2:52 PM

# re: Windows Authentication in winform Application
Requesting Gravatar...
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.
Left by asim on Oct 17, 2014 4:50 PM

# re: Windows Authentication in winform Application
Requesting Gravatar...
Hi !! Thanks for this code but can you take me through all the steps I needs to make it happen, like where should I put this code or how the user gonna write ...

Thank you very much I really need your help !!
Left by Oum on May 28, 2015 1:16 AM

Your comment:
 (will show your gravatar)

Copyright © Ramaraju | Powered by: