如何在 Windows 应用程序中使用 Windows 身份验证?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2905407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Windows Authentication in Windows Application?
提问by Jango
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?
这是 Windows 模拟吗?
回答by SLaks
You can call the LogonUser
API method to check a username and password.
You can see the [DllImport]
here.
您可以调用LogonUser
API 方法来检查用户名和密码。
你可以看到[DllImport]
这里。
If you want to show a standard username/password prompt, you can call the CredUIPromptForCredentialsAPI function; see also here
如果要显示标准的用户名/密码提示,可以调用CredUIPromptForCredentialsAPI 函数;另见此处
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。
回答by Ali
May be a bit late but to achieve Window Authentication Functionalityto 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. 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.
这是非常直接的。我们可以通过使用System.Security.Principal
命名空间的 WindowsIdentity 类来实现这一点。此类提供了一个静态方法 ,getCurrent()
该方法返回 WindowsIdentity 的对象。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. 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.
需要向用户询问域名、用户名、密码才能将这些值传递给互操作服务。与上面相比,这并不复杂,因为我们需要使用 IntropServices 调用 Windows API。为此,我们需要添加一个 extern 函数声明,然后调用该函数。以下代码将帮助您更好地理解这一点。
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 !!!");
回答by Brian R. Bondy
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 LogonUserWin32 API to validate the auth info:
您可能正在寻找的是LogonUserWin32 API 来验证身份验证信息:
[DllImport("advapi32.dll", SetLastError=true)]
public static extern bool LogonUser(
string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
out IntPtr phToken
);