请问有人可以帮我吗?我试图使用所需的凭据登录到 portal.microsoftonline.com,但它让我遇到了那个错误。我的网址是错误的还是什么?因为我试图模仿并赋予用户角色。谢谢你,顺便说一句,我是新来的,请原谅我发布问题的方式。请查看错误所在的评论。
class SecurityHelpers
{
private SecurityHelpers() { }
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser(string lpszUsername,
string lpszDomain, string lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private extern static bool CloseHandle(IntPtr handle);
public static WindowsIdentity CreateIdentity(
string userName, string domain, string password)
{
IntPtr tokenHandle = new IntPtr(0);
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_NETWORK_CLEARTEXT = 3;
tokenHandle = IntPtr.Zero;
bool returnValue = LogonUser(userName, domain, password,
LOGON32_LOGON_NETWORK_CLEARTEXT,
LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
// THIS WHERE THE ERROR IS - "LogonUser failed with error code: 1326"
throw new Exception("LogonUser failed with error code: " + ret);
}
WindowsIdentity id = new WindowsIdentity(tokenHandle);
CloseHandle(tokenHandle);
return id;
}
}