我正在尝试使用 ADAL 实现身份验证(在 React 应用程序内),所以我遵循 the example来自 Azure-Samples,这是我的代码:
var AuthenticationContext = require('../../node_modules/adal-angular/lib/adal.js');
var adalInstance = new AuthenticationContext({
instance: 'https://login.microsoftonline.com/',
clientId: '9897809e-XXXX-XXXX-XXXX-3e6de068af92',
postLogoutRedirectUri: window.location.origin,
endpoints: {}
});
export function enter() {
adalInstance.login();
var isCallback = adalInstance.isCallback(window.location.hash);
adalInstance.handleWindowCallback();
if (isCallback && !adalInstance.getLoginError()) {
adalInstance.acquireToken(adalInstance.config.clientId, function (error, token) {
if (error || !token) {
console.log('ADAL Error Occurred: ' + error);
return;
}
});
}
var user = adalInstance.getCachedUser();
console.log(user);
}
export function logout() {
adalInstance.logout();
}
所以我有以下消息:
ADAL Error Occurred: User login is required
对于用户,我得到null
。
目前我可以转到 Microsoft 登录页面(从登录组件调用)、登录并返回应用程序。我设置了 sessionStorage:
Storage { adal.state.login: "73771340-XXXX-XXXX-XXXX-858c9edcba05", adal.nonce.idtoken: "20074445-XXXX-XXXX-XXXX-9f94c9b5cc31", adal.login.error: "", adal.error: "Invalid_state", adal.login.request: "http://localhost:3000/#id_token=eyJ…", adal.error.description: "Invalid_state. state: a2f210a5-e34b…", adal.token.renew.status: "Completed", length: 7 }
我做错了什么?
数据:我正在使用 adal-angular
包版本中的 adal.js
库:AdalJS v1.0.13
编辑:
现在我正在使用 here 中的特定 adal.js
我将代码更改为:
var AuthenticationContext = require('./adal.js');
当我编译它时(使用 webpack 和 babel 来转译 es6 代码),但给了我一个关于 logging is not Defined
的错误,所以我添加了:
var Logging = {
level: 3,
log: function (message) {
console.log(message);
}
}
到 adal
库。
现在,我进入浏览器(firefox,因为 chromes 似乎没有显示任何日志,我不知道为什么)控制台:
Wed, 18 Jan 2017 11:52:51 GMT:1.0.13-VERBOSE: State: d3c2bb0d-XXXX-XXXX-XXXX-e98a460572e2 bundle.js:74558:8
Wed, 18 Jan 2017 11:52:51 GMT:1.0.13-INFO: Returned from redirect url bundle.js:74558:8
Wed, 18 Jan 2017 11:52:51 GMT:1.0.13-INFO: State status:false; Request type:UNKNOWN bundle.js:74558:8
Wed, 18 Jan 2017 11:52:51 GMT:1.0.13-WARNING: User login is required bundle.js:74558:8
ADAL Error Occurred: User login is required bundle.js:74507:10
USER null bundle.js:74513:4
最佳答案
在与 PowerBi 端点集成期间,我遇到了同样的错误。经过一番研究得到了这个结果。所以下面的代码会自动登录。替换 yourLogic 方法以使用 api 请求的 token 。
import AuthenticationContext from "adal-angular/lib/adal";
window.AuthenticationContext = AuthenticationContext;
let authenticationContext = new window.AuthenticationContext({
instance: "https://login.microsoftonline.com/",
clientId: "XXXXXXX",
postLogoutRedirectUri: window.location.origin,
cacheLocation: "localStorage", // enable this for IE, as sessionStorage does not work for localhost.
}),
resource = "https://analysis.windows.net/powerbi/api",
isCallback = authenticationContext.isCallback(window.location.hash);
authenticationContext.handleWindowCallback();
let user = authenticationContext.getCachedUser(),
token = authenticationContext.getCachedToken(resource),
loginActive = isCallback && user && !token,
renewActive = isCallback && user && !!token;
if (user) {
if (!renewActive) {
authenticationContext.acquireToken(resource, (errorDesc, token, error) => {
if (!error) {
yourLogic(token);
} else {
authenticationContext.error("Error when acquiring token for resource: " + resource, error);
}
});
}
} else {
authenticationContext.login();
}
关于javascript - 无法读取用户信息 - Adal 错误无法登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41706996/