4

请问有人可以帮我吗?我试图使用所需的凭据登录到 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;
     }
  }
4

3 回答 3

2

可以 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];
于 2013-07-26T00:39:15.097 回答
0

userName,domain需要password作为Windows Wide Characteror传递Windows Unicode。请确保您以正确的格式传递它们。

于 2017-01-12T12:54:00.537 回答
0

肯怀特在评论中所说的是正确的。如果您没有为您的用户名和密码传递适当的字符串类型,您将收到 1326。修改您的 API 声明以UnmanagedType.LPStr用于您的字符串。 pinvoke.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 = 4for 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);
于 2018-10-16T04:08:41.243 回答