How to use Windows authentication in a Windows application?

How to use Windows authentication (local computer administrator) in a Windows application written in C #.

Need - whenever a user opens the graphical interface of Windows applications, he must authenticate the credentials of the local administrator, even if the user is registered as an administrator.

Is this a personification of Windows?

+6
source share
3 answers

You can call the LogonUser API method to verify the username and password.
You can see [DllImport] here .

If you want to show the standard username / password hint, you can call the CredUIPromptForCredentials API function ; see also here

EDIT

To check if the user is an administrator, you can call CheckTokenMembership and check if the user is a member of the Administrators group.

Alternatively, you can call NetUserGetInfo level 1 and check if usri1_priv USER_PRIV_ADMIN .

You can also use WMI or DirectoryServices.

+8
source

One way is that your users will be launched as a standard account, if you set the manifest file to run as administrator, then it will always ask for the administrator username and password.

What you're probably looking for is the Win32 LogonUser API for checking author information:

 [DllImport("advapi32.dll", SetLastError=true)] public static extern bool LogonUser( string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken ); 
+2
source

It may be a bit late, but in order to achieve Windows Authentication Functionality in a C # desktop application, there are two steps that are performed using the following steps.

Step 1: Get the user data currently:

This is pretty straight forward. we can achieve this using the WindowsIdentity class of the System.Security.Principal . This class provides a static getCurrent() method that returns a WindowsIdentity object. Below is the code you can use to get current user information.

Step 2: Verify the Windows credentials provided by the user:

You must request the domain name, username, password from the user in order to pass these values ​​for interaction. This is a bit complicated compared to the previous one, since we need to call the Windows API using IntropServices. To do this, we need to add an extern function declaration, and then call the function. The following code will help you better understand this.

 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 !!!"); 
+2
source

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


All Articles