LogonUser失败,错误代码:1326

时间:2013-06-27 01:11:25

标签: c# impersonation office365

请任何人帮我这个吗?我试图使用所需的凭据登录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;
     }
  }

3 个答案:

答案 0 :(得分:1)

可能通过代理帐户执行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];

答案 1 :(得分:0)

userNamedomain password需要传递为Windows Wide CharacterWindows Unicode。请确保您以正确的格式传递它们。

答案 2 :(得分:0)

肯·怀特在评论中说的是正确的。如果没有为用户名和密码传递适当的字符串类型,则会得到1326。修改API声明以对字符串使用UnmanagedType.LPStrpinvoke.net具有良好的API调用说明。

[DllImport("advapi32.dll", SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool LogonUser(
  [MarshalAs(UnmanagedType.LPStr)] string pszUserName,
  [MarshalAs(UnmanagedType.LPStr)] string pszDomain,
  [MarshalAs(UnmanagedType.LPStr)] string pszPassword,
  int dwLogonType,
  int dwLogonProvider,
  ref IntPtr phToken);

此外,您可以为LOGON32_LOGON_BATCH = 4尝试LogonType,这对我来说效果最好。

//i cut out the rest of the enum for brevity.
enum LogonType
{
 LOGON32_LOGON_BATCH = 4
}
string sUser="";
string sDomain="";
string sPWD="";
IntPtr token = new IntPtr();
bool bLoginSuccess = LogonUser(sUser, sDomain, sPWD, (int)LogonType.LOGON32_LOGON_BATCH, 0, ref token);