Error when checking the user name and password

Friends, I need to make the software that needs to check the validity of user login to use the software. I tried this:

bool valid = false; using (PrincipalContext context = new PrincipalContext(ContextType.Domain,domainname)) { valid = context.ValidateCredentials( username, password ); } 

I'm using .net framework 4. In this code above, how to get the domain name of the computer. I tried SystemInformation.ComputerName , SystemInformation.UserDomainName but I got an error like:

With the server can not be contacted

. And if we can get the current user name and password, using either a header file in C #? Answer please. Edit: I use it to log on locally on my computer or in AD.

+4
source share
3 answers

There is no way to get the user's password. This can lead to different safety issues. And if you want to check the credentials, in excess of that machine already knows from the Windows logon, you still need to ask the user for a password.

The following code works for me in the forest with many domains. If you do not specify a domain in the constructor PrincipalContext , it is connected to the computer domain on which it is running. In my testing, it does not matter whether you are a user is checking in another domain, if there are appropriate trust relationships between domains in the forest.

 bool valid = false; using (var context = new PrincipalContext(ContextType.Domain)) { valid = context.ValidateCredentials(username, password); } ContextType.Domain)) bool valid = false; using (var context = new PrincipalContext(ContextType.Domain)) { valid = context.ValidateCredentials(username, password); } ; bool valid = false; using (var context = new PrincipalContext(ContextType.Domain)) { valid = context.ValidateCredentials(username, password); } 

You can get an object representing the current user, like this:

 var user = System.Security.Principal.WindowsIdentity.GetCurrent(); 

Update

If you want to check the credentials in a local database on your computer, just change the type PrincipalContext :

 bool valid = false; using (var context = new PrincipalContext(ContextType.Machine)) { valid = context.ValidateCredentials(username, password); } ContextType.Machine)) bool valid = false; using (var context = new PrincipalContext(ContextType.Machine)) { valid = context.ValidateCredentials(username, password); } ; bool valid = false; using (var context = new PrincipalContext(ContextType.Machine)) { valid = context.ValidateCredentials(username, password); } 
+7
source

Make sure that you have installed .NET 3.5

 bool valid = false; using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) { valid = context.ValidateCredentials( username, password ); } ContextType.Domain)) bool valid = false; using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) { valid = context.ValidateCredentials( username, password ); } ; bool valid = false; using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) { valid = context.ValidateCredentials( username, password ); } 
+1
source

Firstly, you do not want to use the domain name of the computer. You want to use the domain name for a user logs on to the computer. This is due to the fact that some networks can have confidence when the computer is in the same domain, but users from another domain can also log in.

So, to get a domain name of the user logon in Windows, you can use this (example VB.NET, but is easy enough to hide to C #):

 Dim arr As String() = My.User.Name.Split("\") If (arr.Length > 0) Then Dim domainName As String = arr(0).ToString Dim userName As String = arr(1).ToString.ToLower End If ( "\") Dim arr As String() = My.User.Name.Split("\") If (arr.Length > 0) Then Dim domainName As String = arr(0).ToString Dim userName As String = arr(1).ToString.ToLower End If 

Now, to again check the Windows user account before you run the application, you must call the call Windows API:

 Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, _ ByVal lpszDomain As String, _ ByVal lpszPassword As String, _ ByVal dwLogonType As Integer, _ ByVal dwLogonProvider As Integer, _ ByRef phToken As IntPtr) As Boolean Const LOGON32_LOGON_INTERACTIVE As Long = 2 Const LOGON32_LOGON_NETWORK As Long = 3 Public Function ValidateLogin(ByVal domainName As String, ByVal uid As String, ByVal pwd As String) As Boolean Dim token As IntPtr Return LogonUser(uid, domainName, pwd, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token) End Function " (ByVal lpszUsername As String, _ Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, _ ByVal lpszDomain As String, _ ByVal lpszPassword As String, _ ByVal dwLogonType As Integer, _ ByVal dwLogonProvider As Integer, _ ByRef phToken As IntPtr) As Boolean Const LOGON32_LOGON_INTERACTIVE As Long = 2 Const LOGON32_LOGON_NETWORK As Long = 3 Public Function ValidateLogin(ByVal domainName As String, ByVal uid As String, ByVal pwd As String) As Boolean Dim token As IntPtr Return LogonUser(uid, domainName, pwd, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token) End Function , ByVal uid As String, ByVal pwd As String) As Boolean Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, _ ByVal lpszDomain As String, _ ByVal lpszPassword As String, _ ByVal dwLogonType As Integer, _ ByVal dwLogonProvider As Integer, _ ByRef phToken As IntPtr) As Boolean Const LOGON32_LOGON_INTERACTIVE As Long = 2 Const LOGON32_LOGON_NETWORK As Long = 3 Public Function ValidateLogin(ByVal domainName As String, ByVal uid As String, ByVal pwd As String) As Boolean Dim token As IntPtr Return LogonUser(uid, domainName, pwd, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token) End Function , LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token) Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, _ ByVal lpszDomain As String, _ ByVal lpszPassword As String, _ ByVal dwLogonType As Integer, _ ByVal dwLogonProvider As Integer, _ ByRef phToken As IntPtr) As Boolean Const LOGON32_LOGON_INTERACTIVE As Long = 2 Const LOGON32_LOGON_NETWORK As Long = 3 Public Function ValidateLogin(ByVal domainName As String, ByVal uid As String, ByVal pwd As String) As Boolean Dim token As IntPtr Return LogonUser(uid, domainName, pwd, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token) End Function 
0
source

Source: https://habr.com/ru/post/1391934/


All Articles