使用 passport-azure-ad-oauth2 访问 azure web-app

Access to azure web-app using passport-azure-ad-oauth2

我正在尝试使用 express

在 Node JS 中使用 passport-azure-ad-oauth2 对我的 Azure Web 应用程序进行身份验证

我已尝试按照此处找到的文档进行操作:https://github.com/auth0/passport-azure-ad-oauth2。我相信我已经获得正确的客户端 ID、密码和回调 URI...

当我转到 localhost:3000 时,它成功重定向到 Office365 登录。当我选择预 selected 帐户时,它只是不断循环回到“select帐户

在 Chrome 中尝试使用隐身 window 登录时出现错误:请求中指定的回复 URL 与回复 [=29= 不匹配]s 为应用程序配置:'***appID'。

我的代码显然是错误的,我希望有人能够帮助我正确设置它。

提前致谢!!

我的代码在这里

const express = require("express");
const bodyParser = require("body-parser")
const session = require('express-session');
const passport = require("passport");
const ejs = require("ejs");
const jwt = require("jwt-simple")


const AzureAdOAuth2Strategy = require('passport-azure-ad-oauth2').Strategy;


const app = express();

app.use(express.static("public"));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
  extended: true
}));

app.use(passport.initialize());
app.use(bodyParser.urlencoded({ extended: false }));



passport.use(new AzureAdOAuth2Strategy({
    clientID: 'azure client ID',
    clientSecret: 'secret',
    callbackURL: 'http://localhost:3000/auth/aad/callback',
    // resource: '00000002-0000-0000-c000-000000000000',
    // tenant: 'contoso.onmicrosoft.com'
  },
  function (accessToken, refresh_token, params, profile, done) {
    var waadProfile = profile || jwt.decode(params.id_token, '', true);
    console.log(waadProfile);
   
    User.findOrCreate({ id: waadProfile.upn }, function (err, user) {
      done(err, user);
    });
  }));


  app.get("/",passport.authenticate('azure_ad_oauth2'));

  app.get('/auth/aad/callback', 
  passport.authenticate('azure_ad_oauth2', { failureRedirect: '/login' }),
  function (req, res) {
    console.log(req);
    console.log(res);
    res.render('index');
  });



app.listen(process.env.PORT || 3000, function() {
    console.log("Server started on Port 3000");
  });

您代码中的回调 URL 与 Azure 上设置的不一样。

这就是它说的原因:

The reply URL specified in the request does not match the reply URLs configured for the application: '***appID'.

在 Azure 上设置正确的 URL 来解决这个问题。

对于无限重定向问题,请清除浏览器中的缓存和 cookie,它应该会起作用。

但是,如果您不更正回调 URL,它将再次发生。