请设置护照ldapauth,它可以正常使用所有参数,问题是如果用户名或密码错误,它根本不会执行验证回调函数。它就停止了。由于这个原因,我无法向用户提供反馈,以指出实际上是错误的。我有什么想法吗?这是结构
passport.use('ldapStudent', new LdapStrategy({
usernameField: 'username',
passReqToCallback:true,
server: {
url: '..........',
bindDn: '.............',
bindCredentials: '..........',
searchBase: '..............',
searchFilter: '.............',
searchAttributes: ['givenName','sn'],
tlsOptions: {
ca: [fs.readFileSync('./ssl/server.crt', 'utf8')]
}
}
},
function (req, user, done) {
//now check from the DB if user exist
if(user){
//check if user email exist;
User.findOne({'EmailAddress': user}, function (err, userdata) {
// In case of any error, return using the done method
if (err)
return done(err);
//user exist redirect to home page and send user object to session
if (userdata) {
//userActivity(PostActivity);
console.log(userdata);
return done(null, userdata);
}else {
//new user, add them to the user model
var newUser = new User();
newUser.EmailAddress = req.body.username,
newUser.JoinedDate = Date.now(),
newUser.UserType = 'Student'
newUser.save(function (err, result) {
if (err) {
console.log('Error in Saving NewUser: ' + err);
} else {
console.log(result);
var PostActivity = {
ActivityName: req.res.__('Student Joined'),
ActivityDate: Date.now(),
UserID: result._id,
UserIP: (req.header('x-forwarded-for') || req.connection.remoteAddress ) + ' Port: ' + req.connection.remotePort
};
userActivity(PostActivity);
console.log('User Registration successful');
return done(null, newUser, req.flash('SuccessMessage', req.res.__('You have been successfully Registered')));
}
})
}
});
}else{
return done(null, false, req.flash('ValidationError', req.res.__('Wrong password and/or email address')));
}}));
这是我实际登录的地方
router.post('/login', passport.authenticate('ldapStudent', {
successRedirect: '/',
failureRedirect: '/userlogin',
failureFlash: true
}));
代码运行良好,正如我所期望的那样,有意省略了ldap选项对象的参数。 问题是当用户凭证不正确时,验证回调根本没有执行,因此,我无法返回flash消息以便用户知道发生了什么
答案 0 :(得分:0)
passport-ldapauth
不会执行验证回调。这通常是策略如何起作用,例如如果username or password is missing,则passport-local
不会执行验证回调。
包含passport-ldapauth
的策略通常还包括用于故障闪存的(可配置的)消息。列出passport-ldapauth
的常规可配置登录失败消息in the documentation。每条消息也都有一个默认值,所以即使没有配置,也会设置失败的闪存消息(当然,你有flash middleware in use)
此外,