msal - 等待 acquireTokenSilent
我正在我的 Angular 7
应用程序中实现 msal-v1
,并且我想实现我的自己的拦截器
,通过调用 获取访问令牌code>acquireTokenSilent
然后将令牌附加到 http 标头。
由于 acquireTokenSilent 是异步的,http 标头中的令牌始终为空,我需要应用程序等待检索令牌,然后设置标头>。
第一种方法:
const acquireToken = async () => {
let token = "";
const authRequest = {
scopes: ["user.read"],
};
try {
const resp = await this.authService.acquireTokenSilent(authRequest);
token = resp.accessToken;
} catch (err) {
if (err instanceof InteractionRequiredAuthError) {
await this.authService.acquireTokenRedirect(authRequest);
}
}
return token;
};
return next.handle(
httpRequest.clone({
setHeaders: {
Authorization: `Bearer ${this.token}`,
},
})
);
第二种方法:
const authRequest = {
scopes: ["clientID/.default"],
};
var token = this.getAuthenticationToken(authRequest);
return next.handle(
httpRequest.clone({
setHeaders: {
Authorization: `Bearer ${token}`,
},
})
);
}
------------------
public getAuthenticationToken(authRequest): Promise<string> {
return this.authService
.acquireTokenSilent(authRequest)
.then((token) => {
console.log("Got silent access token: ", token);
return token.accessToken;
})
.catch((error) => {
console.log("Could not silently retrieve token from storage.", error);
return this.authService
.acquireTokenPopup(authRequest)
.then((token) => {
console.log("Got popup access token: ", token);
return token.accessToken;
})
.catch((error) => {
console.log("Could not retrieve token from popup.", error);
this.authService.acquireTokenRedirect(authRequest);
return Promise.resolve("");
});
});
}
但似乎没有任何效果,任何人都可以帮助我使 acquireTokenSilent 同步。
我采用这种方法的原因是因为我需要让用户在后台保持静默更新令牌的状态。
I'm implementing msal-v1
in my angular 7
application and I would like to implement my own interceptor
where I get access token by calling acquireTokenSilent
and then attaching the token to the http headers.
as the acquireTokenSilent is asynchronous
, the token in the http headers is always empty
, I need the application to wait until retrieve token and then set the headers
.
First approach:
const acquireToken = async () => {
let token = "";
const authRequest = {
scopes: ["user.read"],
};
try {
const resp = await this.authService.acquireTokenSilent(authRequest);
token = resp.accessToken;
} catch (err) {
if (err instanceof InteractionRequiredAuthError) {
await this.authService.acquireTokenRedirect(authRequest);
}
}
return token;
};
return next.handle(
httpRequest.clone({
setHeaders: {
Authorization: `Bearer ${this.token}`,
},
})
);
Second Approach:
const authRequest = {
scopes: ["clientID/.default"],
};
var token = this.getAuthenticationToken(authRequest);
return next.handle(
httpRequest.clone({
setHeaders: {
Authorization: `Bearer ${token}`,
},
})
);
}
------------------
public getAuthenticationToken(authRequest): Promise<string> {
return this.authService
.acquireTokenSilent(authRequest)
.then((token) => {
console.log("Got silent access token: ", token);
return token.accessToken;
})
.catch((error) => {
console.log("Could not silently retrieve token from storage.", error);
return this.authService
.acquireTokenPopup(authRequest)
.then((token) => {
console.log("Got popup access token: ", token);
return token.accessToken;
})
.catch((error) => {
console.log("Could not retrieve token from popup.", error);
this.authService.acquireTokenRedirect(authRequest);
return Promise.resolve("");
});
});
}
but nothing seems to work, can anyone help me to make the acquireTokenSilent as synchronous.
The reason I'm doing this approach is because I need to make the user to stay active with silent renew token in the background.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题,它对我有用,问题本身是拦截器调用是异步调用承诺的同步过程。
此处代码
I went through the same problem, it worked for me, the problem itself is that the interceptor call being a synchronous process calling the promise asynchronously.
code here