Javascript 无法读取用户信息-Adal错误无法登录
我正在尝试使用ADAL(在react应用程序中)实现身份验证,因此我遵循Azure示例,下面是我的代码:Javascript 无法读取用户信息-Adal错误无法登录,javascript,azure,reactjs,adal,Javascript,Azure,Reactjs,Adal,我正在尝试使用ADAL(在react应用程序中)实现身份验证,因此我遵循Azure示例,下面是我的代码: var AuthenticationContext = require('../../node_modules/adal-angular/lib/adal.js'); var adalInstance = new AuthenticationContext({ instance: 'https://login.microsoftonline.com/', clientId: '98
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错误:需要用户登录
对于用户,我得到null
目前,我可以转到microsoft登录页面(从登录组件调用),登录并返回应用程序。我有会话存储集:
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.js
程序包版本中的adal.js
库:adaljsv1.0.13
编辑:
现在我使用的是一个特定的adal.js
from
我将代码更改为:
var AuthenticationContext = require('./adal.js');
当我编译它时(使用带有babel的webpack来传输es6代码),但给了我一个关于日志记录未定义的错误,所以我添加了:
var Logging = {
level: 3,
log: function (message) {
console.log(message);
}
}
到adal
库
现在,我进入浏览器(firefox,因为chromes似乎没有显示任何日志,我不知道为什么)控制台:
很抱歉评论的字符限制,我在这里写下我的故障排除建议作为答案
事实上,您的代码片段在我这方面运行得很好。对于故障排除和向下箭头指示问题,您可以尝试以下选项
- 目前,浏览器上的javascript语法似乎不支持
export
,require
关键字。因此,您可以检查捆绑脚本,检查全局变量是否有任何错误。您可以使用adal.js中的纯javascript测试AAD应用程序,正如您所引用的示例所示
- 您可以尝试添加全局变量
logging
,以跟踪身份验证流程中的所有信息。例如,在js脚本中添加以下代码。
日志记录={
级别:3,
日志:功能(消息){
控制台日志(消息);
}
}
在与PowerBi端点集成期间,我遇到了相同的错误。经过一些研究得出了这个结果。下面的代码可以自动登录。替换方法yourLogic以将令牌用于api请求
从“adal/lib/adal”导入AuthenticationContext;
window.AuthenticationContext=AuthenticationContext;
让authenticationContext=new window.authenticationContext({
实例:“https://login.microsoftonline.com/",
客户ID:“XXXXXXX”,
postLogoutRedirectUri:window.location.origin,
cacheLocation:“localStorage”//为IE启用此功能,因为sessionStorage不适用于localhost。
}),
资源=”https://analysis.windows.net/powerbi/api",
isCallback=authenticationContext.isCallback(window.location.hash);
authenticationContext.HandleIndowCallback();
让user=authenticationContext.getCachedUser(),
令牌=authenticationContext.getCachedToken(资源),
loginActive=isCallback&&user&&!代币
renewActive=isCallback&&user&&!!代币
如果(用户){
如果(!续订活动){
authenticationContext.acquireToken(资源,(errorDesc,token,error)=>{
如果(!错误){
你的逻辑(令牌);
}否则{
authenticationContext.error(“为资源获取令牌时出错:“+资源,错误”);
}
});
}
}否则{
authenticationContext.login();
}
感谢您的帮助,我更新了我的帖子,但仍然无法使用YetAlinstance.login()将获得访问令牌,AdalinInstance.acquireToken将获得端点/资源令牌,因此我不明白您为什么要将clientId传递到acquireToken中。
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