首页 > 解决方案 > 登录失败。登录来自不受信任的域,并且不能与 winforms 登录时的 Windows 身份验证错误一起使用

问题描述

再会。
我一直被困在一个特别烦人的问题上。
我已经设置了一个域并添加了两台机器(一个数据库服务器和一个接口服务器),它们都在运行 windows server 2012。

我有一个应用程序,它利用模拟通过 Windows 身份验证连接到数据库服务器,并利用用户在应用程序上提供的输入登录详细信息。

我遇到的问题是,每当我使用用户输入的凭据单击提交按钮时,我都会收到“登录失败。登录来自不受信任的域,不能用于 Windows 身份验证。 ”虽然我的模拟类是 [如下所示,非常简单,它仍然给我错误。

public class ImpersonateUser
{
   [DllImport("advapi32.dll", SetLastError = true)]
   public static extern bool LogonUser(
      String lpszUsername,
      String lpszDomain,
      String lpszPassword,
      int dwLogonType,
      int dwLogonProvider,
      ref IntPtr phToken);

   [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
   public extern static bool CloseHandle(IntPtr handle);

   private static IntPtr tokenHandle = new IntPtr(0);
   private static WindowsImpersonationContext impersonatedUser;

   // If you incorporate this code into a DLL, be sure to demand that it
   // runs with FullTrust.
   [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
   public void Impersonate(string domainName, string userName, string password) 
   {
     try {

        // Use the unmanaged LogonUser function to get the user token for
        // the specified user, domain, and password.
        // Xc: ---- For const descriptions see
        // ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.WIN32COM.v10.en/secauthn/security/logonuser.htm
        const int LOGON32_PROVIDER_DEFAULT = 0;
        // Passing this parameter causes LogonUser to create a primary token.
        const int LOGON32_LOGON_INTERACTIVE = 2;
        // const int LOGON32_LOGON_NETWORK = 3; 
        tokenHandle = IntPtr.Zero;

        // Call  LogonUser to obtain a handle to an access token.
        // Xc: ---- LogonUser documentation
        // ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.WIN32COM.v10.en/secauthn/security/logonuser.htm
        bool returnValue = LogonUser(
            userName,
            domainName,
            password,
            LOGON32_LOGON_INTERACTIVE, // Xc:-- will cache logon information for disconnected operations
            /* LOGON32_LOGON_NETWORK, // Xc:-- This type of access is faster */
            LOGON32_PROVIDER_DEFAULT,
            ref tokenHandle);         // tokenHandle - new security token


        if (false == returnValue) {
           int ret = Marshal.GetLastWin32Error();
           Console.WriteLine("LogonUser call failed with error code : " +
               ret);
           throw new System.ComponentModel.Win32Exception(ret);
        }

        // Xc: ---- Assume new identity
        WindowsIdentity newId = new WindowsIdentity(tokenHandle);
        impersonatedUser = newId.Impersonate();

     }
     catch (Exception ex) {
        Console.WriteLine("Exception occurred. " + ex.Message);
     }
  }

   // Stops impersonation
   public void Undo() {
      impersonatedUser.Undo();
      // Free the tokens.
      if (tokenHandle != IntPtr.Zero)
         CloseHandle(tokenHandle);
   }
}

有谁知道这可能是什么原因造成的?我已经黔驴技穷了。

标签: c#winforms

解决方案


推荐阅读