When we develope a new application, it may happen that we need to understand if the current user is the real owner of the used account. The only way to do that is to ask for the access credentials and check their validity via OS.
In this article we are going to see how easy it is to check the username and password of a Windows user using C#.
Let’s Import Assemblies
The first thing that we have to do is to import 2 basic assemblies:
Normally the first one is already present among the references of any .NET Framework project, speaking about the second is necessary to open the Reference Manager window, select the specific assembly and press OK.
Once we’ve imported the correct assemblies, it’s already the time to show the source code.
Let’s start writing down the correct directives:
Now we can show the code that does the trick:
As you would have noticed, in order to specify the correct username we have used the native class WindowsIdentity that gathers any information about domain and username. This way we are sure that the first parameter passed as input to ValidateCredentials is referring to the correct user that is currently executing the application.
Leave A Comment