【问题标题】:Validating domain user credentials验证域用户凭据
【发布时间】:2011-05-16 14:46:07
【问题描述】:

我需要一种方法来验证 Windows 上本机 C++ 的用户/密码对。 输入用户名和密码,用户名可以是DOMAIN\user格式。

基本上我需要编写一个函数: 如果用户/密码是有效的本地帐户,则返回 true。 (第1部分) 如果用户/密码在给定域上有效,则也返回 true。 (第2部分) 否则返回 false。

使用 KB180548 我解决了(第 1 部分)(但我还必须检查用户名是否是有效用户,因为对于使用空白密码的用户会失败 - 丑陋的解决方法,但它有效)

但是,对于除“.”之外的任何域,上述 KB 示例代码(错误地)适用于任何用户/通行证对。

我尝试过使用 ldap_bind_s,但是对于不正确的用户/密码对(可怕的访客帐户?),它会成功。此外,对于“。”域,使用 LDAP_SERVER_DOWN 的有效用户/密码失败(可能是因为本地主机不是域控制器?)

也许其中一些概念我不清楚。我希望至少我的问题得到了清楚的解释。 我不拘泥于任何方法,因为它可以在 C++ 本机代码中实现。

这个问题C#: How to validate domain credentials? 似乎已经弄清楚了(除非没有接受的答案)。唉,它是用 C# 编写的。

编辑:来吧,Stack Overflow,你从来没有让我失望过......

【问题讨论】:

    标签: c++ windows authentication active-directory


    【解决方案1】:

    如果你的意思是“。”域,不“受信任”的域与运行代码的域失败,那么这是设计使然。

    几年前,当我们使用支持票时,Microsoft 对此的最佳回答是使用 WNetUseConnection()。

    【讨论】:

    • 我认为这是最接近的答案。我发现在加入域的 PC 上我的代码有效。在测试 PC 上(通过 VPN 连接到与域控制器相同的 LAN)代码失败。
    【解决方案2】:

    旧代码和平,我无法测试,所以按原样:

    //---------------------------------------------------------
    // quick ADSI sample - binding to a user 
    //---------------------------------------------------------
    
    //---------------------------------------------------------
    // should use unicode - saves a lot of conversion work
    //---------------------------------------------------------
    #define _UNICODE
    
    //---------------------------------------------------------
    // libraries needed to use ADSI
    //---------------------------------------------------------
    #pragma comment( lib, "Activeds.lib" )
    #pragma comment( lib, "Adsiid.lib" )
    
    //---------------------------------------------------------
    // ADSI header
    //---------------------------------------------------------
    #include <activeds.h>
    
    int wmain( int argc, wchar_t *argv[] )
    {
      //-----------------------------------------------------
      // HRESULT hr is the return code value from all ADSI
      // calls - using the SUCCEEDED MACRO to check for 
      // success
      //-----------------------------------------------------
      HRESULT hr;
    
      //-----------------------------------------------------
      // pointer to our IADsUser object
      //-----------------------------------------------------
      IADsUser *pUser = NULL;
    
      //-----------------------------------------------------
      // path to the user we are going to try to update
      // make sure you replace this with something
      // specific to your environment
      // Form : WinNT://<domain name>/<object name>,<object class>
      //-----------------------------------------------------
      LPWSTR pszADsPath = L"WinNT://yourdomain/object name,user";
      // 
      // See available forms :
      // http://msdn.microsoft.com/en-us/library/aa746534(v=VS.85).aspx
    
      //-----------------------------------------------------
      // intialize the COM subsystem before doing any work
      //-----------------------------------------------------
      CoInitialize(NULL);
    
      //-----------------------------------------------------
      // try to get the user
      // http://msdn.microsoft.com/en-us/library/aa772184(v=VS.85).aspx
      //-----------------------------------------------------
      hr = ADsGetObject(pszADsPath, IID_IADsUser,(void**)&pUser);
    
      // Here Test hr
      //http://msdn.microsoft.com/en-us/library/aa772195(v=VS.85).aspx
    
      //-----------------------------------------------------
      // kill the COM subsystem we were using
      //-----------------------------------------------------
      CoUninitialize();
    
      return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 2010-09-24
      • 2021-08-10
      • 1970-01-01
      • 2016-12-15
      • 2012-02-07
      • 1970-01-01
      相关资源
      最近更新 更多