如何使用LDAP对用户进行密码身份验证
How to do password authentication for a user using LDAP?
我正在编写一个客户端应用程序(使用OpenLDAP库),用户通过LDAP服务器获得身份验证。
下面是一个硬编码的示例程序,它无法比较用户的userPassword。
#include <stdio.h>
#include <ldap.h>
#define LDAP_SERVER "ldap://192.168.1.95:389"
int main( int argc, char **argv ){
LDAP *ld;
int rc;
char bind_dn[100];
LDAPMessage *result, *e;
char *dn;
int has_value;
sprintf( bind_dn, "cn=%s,dc=ashwin,dc=com", "manager" );
printf( "Connecting as %s...n", bind_dn );
if( ldap_initialize( &ld, LDAP_SERVER ) )
{
perror( "ldap_initialize" );
return( 1 );
}
rc = ldap_simple_bind_s( ld, bind_dn, "ashwin" );
if( rc != LDAP_SUCCESS )
{
fprintf(stderr, "ldap_simple_bind_s: %sn", ldap_err2string(rc) );
return( 1 );
}
printf( "Successful authenticationn" );
rc = ldap_search_ext_s(ld, "dc=ashwin,dc=com", LDAP_SCOPE_SUBTREE, "sn=ashwin kumar", NULL, 0, NULL, NULL, NULL, 0, &result);
if ( rc != LDAP_SUCCESS ) {
fprintf(stderr, "ldap_search_ext_s: %sn", ldap_err2string(rc));
}
for ( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) {
if ( (dn = ldap_get_dn( ld, e )) != NULL ) {
printf( "dn: %sn", dn );
has_value = ldap_compare_s( ld, dn, "userPassword", "secret" );
switch ( has_value ) {
case LDAP_COMPARE_TRUE:
printf( "Works.n");
break;
case LDAP_COMPARE_FALSE:
printf( "Failed.n");
break;
default:
ldap_perror( ld, "ldap_compare_s" );
return( 1 );
}
ldap_memfree( dn );
}
}
ldap_msgfree( result );
ldap_unbind( ld );
return( 0 );
}
userPassword如果在LDAP服务器中为plain,则有效。如果是MD5加密,ldap_compare_s失败。这是因为我传递了一个明文密码来进行比较。
我如何让这个示例程序工作?
我这样做对吗?使用ldap_compare_s
通过LDAP对用户进行身份验证是否正确?
p。S:这是我第一次使用LDAP。
这并不是在LDAP上执行密码检查的正确方法,您应该尝试使用从第一次搜索中获得的dn
和提供的密码进行绑定。
。执行第二次绑定来验证密码。如果绑定失败,则密码不正确。
类似于:
if ( (dn = ldap_get_dn( ld, e )) != NULL ) {
printf( "dn: %sn", dn );
/* rebind */
ldap_initialize(&ld2, LDAP_SERVER);
rc = ldap_simple_bind_s(ld2, dn, "secret");
printf("%dn", rc);
if (rc != 0) {
printf("Failed.n");
} else {
printf("Works.n");
ldap_unbind(ld2);
}
ldap_memfree( dn );
}
出于安全考虑,表明用户名不正确(即搜索用户帐户失败)通常被认为是过度披露,应避免。
相关文章:
- LibGit2 SSH身份验证失败
- 密码长度验证 (c++)
- 为什么 WinInet 在通过 FQDN 连接时无法通过协商自动进行身份验证,但如果通过 IP 连接则成功?
- 如何通过NetHTTPClient在HTTPS中进行身份验证
- 处理身份验证弹出窗口在wxWebView
- 创建加密安全密码.并验证它是否有效
- 如何测试我的谷歌身份验证器实现?
- 通过 Boost 和 C++ 进行 SMTP 身份验证
- libtorrent是否支持带摘要身份验证的http web种子
- 将Windows身份验证与cpprestsdk一起使用
- OCCI:使用SSL钱包身份验证连接到OracleDB
- 为什么我不能使用外围设备进行身份验证
- 无法使用C++代码向MongoDB进行身份验证
- 模拟的 HTTP 身份验证仅在本地主机上工作
- 使用OpenSSL的SHA 512 HMAC消息身份验证的问题
- 当我的数据库身份验证需要密码时,如何使PQXX(Postgres的C API)工作
- 使用加密哈希密码进行身份验证
- postgres/libpqxx:拒绝通过密码进行身份验证
- 带有用户名和密码的C++/.net(Framework 2.0)MS SQL数据库windows身份验证
- 如何使用LDAP对用户进行密码身份验证