【发布时间】:2018-01-17 20:24:22
【问题描述】:
我找到了各种使用 MSAL 的示例,甚至发现了将 MSAL 与 SPA 应用程序(通常是 Angular 或 React)一起使用的示例,但我正在努力让它与 Aurelia 一起使用。
我执行了以下操作以确保库实际上按预期工作。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/0.1.3/js/msal.min.js"></script>
<style>
.hidden {
visibility: hidden;
}
</style>
</head>
<body>
<div id="username"></div>
<div id="token"></div>
<button id="login" onclick="loginPopup()">Log In</button>
<button id="logout" class="hidden">Log Out</button>
<script>
var authClient = new Msal.UserAgentApplication("my v2 endpoint client id",null);
function loginPopup(){
authClient.loginPopup(["openid","user.readbasic.all"])
.then(token => {
authClient.acquireTokenSilent(["user.readbasic.all"])
.then(accessToken => {
updateUI(token);
},
error => {
authClient.acquireTokenPopup(["user.readbasic.all"])
.then(accessToken => {
updateUI(token);
},
error => {
console.log("Token Error: " + error)
})
})
},
error =>{
console.log("Login error " + error)
})
}
function updateUI(token){
var loginbutton = document.getElementById("login");
loginbutton.className = "hidden";
let usernamediv = document.getElementById("username");
let tokendiv = document.getElementById("token");
usernamediv.innerText = authClient.getUser().name;
tokendiv.innerText = token;
}
</script>
</body>
</html>
该代码运行良好。单击“登录”按钮,将显示“登录”弹出窗口,选择用户,输入密码,弹出窗口消失,用户界面将使用用户名和令牌进行相应更新。
现在我正在尝试将其添加到我的 Aurelia 应用程序中,如下所示:
main.js
export async function configure(aurelia){
aurelia.use
.standardConfiguration()
.developmentLogging();
let msClient = new Msal.UserAgentApplication('my v2 endpoint client id",null);
aurelia.use.instance("AuthService",msClient);
await aurelia.start();
if(msClient.getUser()) {
await aurelia.setRoot(PLATFORM.moduleName("app"));
else
await aurelia.setRoot(PLATFORM.moduleName("login/login"));
login.js
export class Login {
static inject = [Aurelia, "AuthService"]
constructor(aurelia, auth){
this.authService = auth
this.app = aurelia
}
activate(){
//Also tried this code in constructor
this.authService.loginPopup(["openid","user.readbasic.all"])
.then(token => {
this.app.setRoot(PLATFORM.moduleName("app"))
});
}
}
但是,使用此代码后,应用程序将加载并导航到弹出登录弹出窗口的登录页面。但是,一旦用户输入/选择名称和密码,弹出屏幕不会消失。应用程序(在弹出窗口后面)似乎重新加载并导航到 app.js 视图模型,但弹出窗口仍保留在屏幕上,并且似乎要求用户输入/选择用户名。
我也尝试过使用 loginRedirect 而不是 loginPopup,结果相似,即使在验证后应用程序也会不断重定向到登录页面。
我猜这与 MSAL 尝试响应应用程序的方式以及 Aurelia 尝试处理导航的方式等有关,但我不知道哪里出了问题。
任何帮助将不胜感激!
【问题讨论】:
-
这是一个非常具体的问题,取决于 MSAL 的行为方式。路由器似乎在做它应该做的大部分事情。让我们联系,您可以实时向我展示行为,我们可以看看我们是否可以修复它。