简体   繁体   English

如何在Windows应用程序中使用Windows身份验证?

[英]How to use Windows Authentication in Windows Application?

How to use windows authentication (local machine administrator user) in windows application written in C#. 如何在用C#编写的Windows应用程序中使用Windows身份验证(本地机器管理员用户)。

Need is whenever user opens my windows application GUI, it should authenticate local administrator credentials even if User is logged in as Administrator there. 需要的是每当用户打开我的Windows应用程序GUI时,即使用户以管理员身份登录,它也应该验证本地管理员凭据。

Is this windows Impersonation? 这个窗户是假冒吗?

You can call the LogonUser API method to check a username and password. 您可以调用LogonUser API方法来检查用户名和密码。
You can see the [DllImport] here . 你可以在这里看到[DllImport]

If you want to show a standard username/password prompt, you can call the CredUIPromptForCredentials API function; 如果要显示标准用户名/密码提示,可以调用CredUIPromptForCredentials API函数; see also here 另见这里

EDIT 编辑

To check whether the user is an administrator, you can call CheckTokenMembership and check whether the user is in the Administrators group. 要检查用户是否是管理员,您可以调用CheckTokenMembership并检查该用户是否在Administrators组中。

Alternatively, you can call NetUserGetInfo level 1 and check whether usri1_priv is USER_PRIV_ADMIN . 或者,您可以调用NetUserGetInfo级别1并检查usri1_priv是否为USER_PRIV_ADMIN

You can also use WMI or DirectoryServices. 您还可以使用WMI或DirectoryServices。

May be a bit late but to achieve Window Authentication Functionality to a C# Desktop Application, there are two steps accomplish with below steps. 可能有点晚,但要实现C#桌面应用程序的窗口身份验证功能 ,下面的步骤有两个步骤。

Step 1: Get currently logged in user details: 第1步:获取当前登录的用户详细信息:

This is pretty straight forward. 这很简单。 we can achieve this by using the WindowsIdentity class of System.Security.Principal namespace. 我们可以通过使用System.Security.Principal命名空间的WindowsIdentity类来实现这一点。 This class provides a static method, getCurrent() , which return a object of WindowsIdentity. 这个类提供了一个静态方法getCurrent() ,它返回一个WindowsIdentity对象。 Bellow is the code you can use to get the current logged in user details. Bellow是可用于获取当前登录用户详细信息的代码。

Step 2: Validate windows credentials provided by user: 第2步:验证用户提供的Windows凭据:

Need to ask domain name, user name, password from user to pass these values to interop service. 需要从用户询问域名,用户名,密码才能将这些值传递给互操作服务。 This is little complex compared to above as we need to call a windows API using IntropServices. 与上面相比,这有点复杂,因为我们需要使用IntropServices调用Windows API。 To accomplish this we need to add a extern function declaration, and then call the function. 为此,我们需要添加一个extern函数声明,然后调用该函数。 Following code will help you to understand this better. 以下代码将帮助您更好地理解这一点。

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

One way is if your users will run as standard account, if you set your manifest file to be run as administrator, then it will prompt for an admin username and password always. 一种方法是,如果您的用户将作为标准帐户运行,如果您将清单文件设置为以管理员身份运行,那么它将始终提示输入管理员用户名和密码。

What you're probably looking for though is the LogonUser Win32 API to validate the auth info: 您可能正在寻找的是用于验证身份验证信息的LogonUser Win32 API:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改Web应用程序身份验证以使用Windows身份验证 - change web application authentication to use Windows Authentication 使用Windows身份验证的WPF应用程序 - WPF application with Windows authentication 如何在Windows应用程序中使用HttpContext? - How to use HttpContext in a Windows application? 如何在 SignalR javascript 客户端上使用 Windows 身份验证 - How to use Windows Authentication on the SignalR javascript client .net Web应用程序是否可以在“表单”和“ Windows”中使用身份验证 - Can we use Authentication in 'Forms' and as well in 'Windows' for a .net web application SharePoint在线:Windows客户端应用程序可以使用OAuth进行身份验证吗? - SharePoint online: can a Windows Client application use OAuth for authentication? 使用Windows身份验证的Windows平板电脑应用程序 - Application for Windows tablet using Windows Authentication 用于 Windows 窗体应用程序的基于 VPN 的 Windows 身份验证 - Windows Authentication over VPN for Windows Form Application 使用 Windows 网络身份验证登录应用程序 - Log into application with Windows Network Authentication 域上的.NET应用程序中的Windows身份验证 - Windows Authentication in .NET application on domain
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM