2011-05-16 158 views
2

我需要一种方法来在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#中。

编辑:来吧,堆栈溢出,你从来没有让我失望之前...

回答

1

如果你的意思是“。”域名,不具有“可信”的域名/域名运行代码失败,那么这是设计。

几年前,当我们使用支持票时,Microsoft最好的答案是使用WNetUseConnection()。

+0

我认为这是最接近的答案。我发现在加入域的PC上我的代码工作。在测试PC上(通过VPN连接到与域控制器相同的LAN),代码失败。 – user581243 2011-05-23 09:35:37

1

老和平的代码,我'无法测试,所以给出的作为是:

//--------------------------------------------------------- 
// 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; 
}