注销Azure Passport身份验证节点js [英] Loging out of Azure Passport authentication Node js
问题描述
我有一个节点js应用程序,其中我们使用了带Passport身份验证的Azure登录。
我已成功使用Azure登录,应用程序运行正常。
但是,当我注销并将URL提供给页面时,它会检查身份验证并自动转到该页面,而不要求登录。
登录后,我的URL包含以下查询字符串 1.会话状态 2.代码 3.国家 4.令牌登录代码:
app.get('/login', passport.authenticate('azuread-openidconnect', { failureRedirect: '/' }), function (req, res) {
res.sendFile(path.join(__dirname+'/index.html'));
});
注销代码:
app.get('/logout', function (req, res) {
req.session.destroy();
req.logout();
res.redirect('/');
});
当我注销时,页面重定向到我的索引页。然后,当我将‘/登录’赋予URL时,它会将我带到页面,而不会转到登录页面
请帮帮忙……
推荐答案
此问题是由OAuth2.0的授权码授予流引起的。在Azure AD OAuth 2.0服务上有任何会话。这不是passportjs
或expressjs
的问题。
我们可以进行以下简单的测试,在浏览器中访问身份验证端点,https://login.microsoftonline.com/common/oauth2/authorize?response_type=id_token%20code&client_id=<client_id>&redirect_uri=<redirect_uri>&response_mode=query&scope=openid
您首先需要填写电子邮件和密码,完成登录流程后,第二次访问终端时,您将不再需要填写电子邮件或密码。
我们可以将授权端点中的url参数prompt
设置为login
,强制用户每次重新进行身份验证。
有关详细信息,请参阅https://msdn.microsoft.com/en-us/library/azure/dn645542.aspx#code-snippet-3。
但在Azure Passport oidc策略中,我们应该修改将参数添加到终结点的源代码。
安装passport-azure-ad
模块后,打开文件/node_modules/passport-azure-ad/lib/passport-azure-ad/oidcstrategy.js
,在第545行(或多或少),您可以找到以下代码片段:
var params = {};
if (self.authorizationParams) { params = self.authorizationParams(options); }
params['response_type'] = config.responseType;
log.info('We are sending the response_type: ', params['response_type']);
params['client_id'] = config.clientID;
params['redirect_uri'] = callbackURL;
...
我们可以在代码片段后面添加句子params['prompt'] = 'login';
以添加支持。
如有任何进一步的问题,请随时通知我。
编辑
是否有办法仅在我注销时才提示登录...
我不确定您的意思是,当用户访问login
路由时,您想检查他是否通过了身份验证,如果是,不提示登录流程?
如果是这样的话,您可以定制一个中间件来检查经过身份验证的。例如:
function checkAuthenticatedOnLogin(req,res,next){
if (!req.isAuthenticated()) {
return next();
}else{
res.send('do not need login');
}
}
app.get('/login',checkAuthenticatedOnLogin,
passport.authenticate('azuread-openidconnect',{ failureRedirect: '/login' }),
function(req, res) {
log.info('Login was called in the Sample');
res.redirect('/');
});
这篇关于注销Azure Passport身份验证节点js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!