【问题标题】:Passport-ldapauth fails to execute verify callbackPassport-ldapauth 无法执行验证回调
【发布时间】:2016-01-22 12:37:45
【问题描述】:

请,我已经设置了护照 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 消息让用户知道发生了什么

【问题讨论】:

    标签: node.js ldap passport.js


    【解决方案1】:

    passport-ldapauth 如果没有要验证的内容,则不会执行验证回调,如果凭据不正确且未接收到用户,则会出现这种情况。这通常是策略倾向于起作用的方式,例如如果username or password is missingpassport-local 不会执行验证回调。

    策略,包括passport-ldapauth,通常还包括一个(可配置的)失败闪存消息。列出了passport-ldapauth 的一般可配置登录失败消息in the documentation。每条消息也有一个默认值,因此即使未配置,也会设置故障闪烁消息(当然,假设您有flash middleware in use

    另外,您不应该在验证函数的回调中使用req.flash(),而是使用supply an info message

    【讨论】:

      猜你喜欢
      • 2015-08-22
      • 2018-01-29
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 2016-08-10
      • 2018-07-25
      • 2018-01-31
      • 2019-03-02
      相关资源
      最近更新 更多