c# – LogonUser失败,错误代码:1326

请任何人帮我这个吗?我试图使用所需的凭据登录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;
     }
  }
最佳答案
可以通过代理帐户执行xp_cmdshell.检查代理帐户是否具有正确的凭据.

在对象资源管理器中转到:

Security > Credentials > ##xp_cmdshell_proxy_account##

另外,检查用户是否具有sys.xp_cmdshell的执行权限

在对象资源管理器中转到:

Databases > System Databases > master > Security > Users > [user] > Securables

SQL授予权限:

use [master]
grant execute on xp_cmdshell to [domain\user];

转载注明原文:c# – LogonUser失败,错误代码:1326 - 代码日志