Windows与VCE上的LDAP绑定

时间:2017-03-30 09:18:26

标签: c# c++ ldap windows-embedded-compact

我已经完成了从C#应用程序中的Windows Compact 7设备对AD进行Windows身份验证的简单任务。现在 - 14天后,我仍然在苦苦挣扎,所以任何帮助都会受到高度赞赏。

无论如何 - 到目前为止我设法提出的是使用wldap32.dll在我的PC上进行Windows身份验证。我远非C ++专家,而且Visual C ++甚至更少,所以请耐心等待。 这是我的C ++代码:

EXPORT_METHOD unsigned long Authenticate(char *userName, char *password, char *domain)
{
    unsigned long result = 0;

    unsigned short *uUserName = (unsigned short *)userName;
    unsigned short *uPassword = (unsigned short *)password;
    unsigned short *uDomain = (unsigned short *)domain;

    PWCHAR hostName = NULL;
    LDAP* pLdapConnection = NULL;
    ULONG version = LDAP_VERSION3;

    size_t origsize = strlen("ELLAB.COM") + 1;
    size_t convertedChars = 0;
    wchar_t wcstring[100];
    mbstowcs_s(&convertedChars, wcstring, origsize, "ELLAB.COM", _TRUNCATE);
    wcscat_s(wcstring, L" (wchar_t *)");
    hostName = wcstring;

    //  Initialize a session. LDAP_PORT is the default port, 389.
    pLdapConnection = ldap_init(hostName, LDAP_PORT);

    if (pLdapConnection == NULL)
    {
        #ifdef DEBUG
        AfxMessageBox(_T("Unable to Init"));
        #endif // DEBUG
        result = 0xff;
    }
    else
    {
        //  Set the version to 3.0 (default is 2.0).
        result = ldap_set_option(pLdapConnection, LDAP_OPT_PROTOCOL_VERSION, (void*)&version);
        if (result != LDAP_SUCCESS)
        {
            #ifdef DEBUG
            AfxMessageBox(_T("Unable to Set Optins"));
            #endif // DEBUG
        }
        else
        {
            // Connect to the server.
            result = ldap_connect(pLdapConnection, NULL);
            if (result != LDAP_SUCCESS)
            {
                #ifdef DEBUG
                AfxMessageBox(_T("Unable to Connect"));
                #endif // DEBUG
            }
            else
            {
                //  Be aware that the password itself is never sent over the network, and encryption is not used.
                SEC_WINNT_AUTH_IDENTITY NtAuthIdentity;
                ZeroMemory(&NtAuthIdentity, sizeof(NtAuthIdentity));
                NtAuthIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;

                NtAuthIdentity.Domain = uDomain;
                NtAuthIdentity.DomainLength = sizeof(uDomain);
                NtAuthIdentity.User = uUserName;
                NtAuthIdentity.UserLength = sizeof(uUserName);
                NtAuthIdentity.Password = uPassword;
                NtAuthIdentity.PasswordLength = sizeof(uPassword);

                //if (ldap_bind_s(pLdapConnection, NULL, NULL, LDAP_AUTH_NEGOTIATE) == LDAP_SUCCESS)
                result = ldap_bind_s(pLdapConnection, NULL, (PWCHAR)&NtAuthIdentity, LDAP_AUTH_NTLM);
                //if (result != LDAP_SUCCESS)
                {
                    #ifdef DEBUG
                    AfxMessageBox(_T("Unable to Bind using ldap_bind_s"));
                    #endif // DEBUG
                    result = ldap_bind(pLdapConnection, NULL, (PWCHAR)&NtAuthIdentity, LDAP_AUTH_NTLM);
                    if (result != LDAP_SUCCESS)
                    {
                        #ifdef DEBUG
                        AfxMessageBox(_T("Unable to Bind using ldap_bind"));
                        #endif // DEBUG
                    }
                }
            }

        }
        //  Normal cleanup and exit.
        ldap_unbind(pLdapConnection);
    }

    return result;
}

我的C#样本包装器:

[DllImport(@"C:\Users\ckbn.ELLAB\Documents\Visual Studio 2015\Projects\WCE7_LDAP\Debug\LDAPHandler.dll", CharSet = CharSet.Unicode)]
    public static extern int Authenticate(string userName, string password, string domain);

    private void buttonAuthenticate_Click(object sender, EventArgs e)
    {
        try
        {
            textBoxResult.Text = "";
            LDAPReturns result = (LDAPReturns)Authenticate(textBoxUserName.Text, textBoxPassword.Text, textBoxDomain.Text);
            switch (result)
            {
                case LDAPReturns.LDAP_SUCCESS:
                    textBoxResult.Text = "User '" + textBoxUserName.Text + "' is authenticated";
                    break;
                default:
                    textBoxResult.Text = result.ToString();
                    break;
            }
        }
        catch (Exception ex) { textBoxResult.Text = "Failed: " + ex.ToString(); }
    }

所以 - 使用上面的代码,我可以在我的电脑上使用我自己的凭据进行身份验证。在WCE设备上运行相同的代码,我可以连接到AD服务器,但是当我尝试ldap_bind_s时,它返回LDAP_AUTH_METHOD_NOT_SUPPORTED。所以我认为wldap32.dll的ARM版本与x86版本不同。

然后我尝试使用ldap_bind(不带'_s')方法。这在我的电脑和WCE设备上都没有工作,并且总是返回0xFFFF_FFFF。

基本上我的问题是:

LDAP_AUTH_METHOD_NOT_SUPPORTED的含义是什么,以及我在WCE7设备上有哪些其他方法?和

当ldap_bind返回0xFFFF_FFFF时,它意味着什么?

但任何意见或建议都会受到高度赞赏。提前谢谢!

1 个答案:

答案 0 :(得分:1)

您的WEC7设备的供应商是否在操作系统构建中包含LDAP功能?

或者,如果您是供应商,您是否在Platform Builder项目中指定了SYSGEN_LDAP?

如果您有权访问该设备,请尝试检查设备\ Windows目录中的ceconfig.h文件。