使用passport-windowsauth时无法读取未定义错误的属性“用户名”

时间:2019-01-16 18:05:39

标签: node.js express passport.js

我正在尝试将passportjsjs与passport-windowsauth结合使用,以对Active Directory进行身份验证。但是,护照似乎无法在我的username正文中的passwordpost字段中找到登录路线。我收到以下错误:

TypeError: Cannot read property 'username' of undefined

app.js

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var passport = require('passport');
var WindowsStrategy = require('passport-windowsauth');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(passport.initialize());

app.use('/', indexRouter);
app.use('/users', usersRouter);

passport.use(new WindowsStrategy({
ldap: {
    url: 'ldap://example.com/DC=example,DC=com',
    base: 'DC=example,DC=com',
    bindDN: 'someuser',
    bindCredentials: 'somepassword'
},
integrated: false
}, function(user, done){
    if (user) {
        var user = user
        return done(null, user);
    } else {
        return done(null, false);
    }
}));

module.exports = app;

./ routes / index.js

var express = require('express');
var router = express.Router();
var passport = require('passport');

router.get('/login', function(req, res, next) {
  res.send('<form action="/login" method="post"><div><label>Username:</label><input type="text" name="username"/></div>' +
          '<div><label>Password:</label><input type="password" name="password"/></div><div><input type="submit" value="Log In"/></div></form>')
});

router.post('/login', passport.authenticate('WindowsAuthentication', {session: false, successRedirect: '/test'})
);

router.get('/test', function(req, res, next) {
  res.send("Yay!");
});

module.exports = router;

我已经检查了用于绑定的用户帐户,并且凭据绝对有效。

任何人的帮助都将不胜感激,因为我挠头想知道我必须缺少什么简单的东西。

0 个答案:

没有答案