gpt4 book ai didi

c# - 用用户名和密码模拟?

转载 作者:可可西里 更新时间:2023-11-01 08:53:29 24 4
gpt4 key购买 nike

WindowsIdentity identity = new WindowsIdentity(accessToken);
WindowsImpersonationContext context = identity.Impersonate();

...
context.Undo();

我在哪里声明管理员用户名和密码?

accessToken 参数对我帮助不大...

我必须为它导入 DLL 吗?

最佳答案

您需要获取用户的 token 。使用 p/invoke LogonUser来自 advapi32.dll:

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

示例:

IntPtr userToken = IntPtr.Zero;

bool success = External.LogonUser(
"john.doe",
"domain.com",
"MyPassword",
(int) AdvApi32Utility.LogonType.LOGON32_LOGON_INTERACTIVE, //2
(int) AdvApi32Utility.LogonProvider.LOGON32_PROVIDER_DEFAULT, //0
out userToken);

if (!success)
{
throw new SecurityException("Logon user failed");
}

using (WindowsIdentity.Impersonate(userToken))
{
// do the stuff with john.doe's credentials
}

关于c# - 用用户名和密码模拟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7710538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com