我的Webapp正在从LDAP服务器获取信息。用户需要使用用户名和密码登录到LDAP服务器。我想将LDAP-Client保存在会话中,这样用户每次进行查询时都无需输入密码。
我正在使用express和express-session进行会话处理。我正在使用ldapjs-client https://www.npmjs.com/package/ldapjs-client,这实际上是ldapjs的Promise包装器。我已经使用普通的ldapjs库进行了尝试,但是它也不起作用。
在同一请求中使用已保存的客户端时,它将起作用。但是,如果它是从另一个请求的会话中加载的,则它将不再起作用。
显示以下错误消息:
TypeError: client.search is not a function
我试图将客户端包装在一个对象中并保存该对象,但是它不起作用。
保存客户端的代码:
//Client initialattion.
const LdapClient = require("ldapjs-client");
req.session.client = new LdapClient({ url: "ldap://Ip-To-Ldap:389" });
await req.session.client.bind(req.body.name, req.body.password);
第一个请求之前的客户端对象:
Client {
url: 'ldap://Ip-To-Server:389',
protocol: 'ldap:',
slashes: true,
auth: null,
host: 'Ip-To-Server',
port: 389,
hostname: 'Ip-To-Server',
hash: null,
query: null,
pathname: null,
path: null,
href: 'ldap://Ip-To-Server:389',
secure: false,
_queue: Map {},
_parser:
Parser {
domain: null,
_events:
[Object: null prototype] { error: [Function], message: [Function] },
_eventsCount: 2,
_maxListeners: undefined,
buffer: null },
_socket:
Socket {
connecting: false,
_hadError: false,
_handle:
TCP {
reading: true,
onread: [Function: onStreamRead],
onconnection: null,
[Symbol(owner)]: [Circular] },
_parent: null,
_host: 'ldap.zhaw.ch',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
paused: false,
emitClose: false,
autoDestroy: false,
destroyed: false,
defaultEncoding: 'utf8',
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events:
[Object: null prototype] {
end: [Function: onReadableStreamEnd],
close: [Function: destroy],
error: [Function],
data: [Function] },
_eventsCount: 4,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: false,
autoDestroy: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
[Symbol(asyncId)]: 91,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0 } }
在另一个请求中加载后:
{ url: 'ldap://ldap.zhaw.ch:389',
protocol: 'ldap:',
slashes: true,
auth: null,
host: 'ldap.zhaw.ch',
port: 389,
hostname: 'ldap.zhaw.ch',
hash: null,
query: null,
pathname: null,
path: null,
href: 'ldap://ldap.zhaw.ch:389',
secure: false,
_queue: {},
_parser: { domain: null, _events: {}, _eventsCount: 2, buffer: null },
_socket:
{ connecting: false,
_hadError: false,
_handle: { reading: true, onconnection: null },
_parent: null,
_host: 'ldap.zhaw.ch',
_readableState:
{ objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
paused: false,
emitClose: false,
autoDestroy: false,
destroyed: false,
defaultEncoding: 'utf8',
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events: {},
_eventsCount: 4,
_writableState:
{ objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: false,
autoDestroy: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null } }