![](/img/trans.png)
[英]Example of fetch() method to API with Azure AD B2C authorization
[英]Azure B2C - MSAL callback method doesn't call the method which return the promise?
在 authCallback() 的 Azure B2C 中,未调用 ItWillCallPromise。 是按照设计还是我做错了什么。 此外,如果按照设计不可能,是否有一种解决方法来调用 ItWillCallPromise 方法?
function initializeAuthAgent() {
var authConfig = Config();
authAgent = new Msal.UserAgentApplication({
auth: {
clientId: authConfig.clientId,
authority: authConfig.authority,
validateAuthority: false,
postLogoutRedirectUri: '/index.html'
},
cache: {
cacheLocation: 'sessionStorage'
}
});
authAgent.handleRedirectCallback(authCallback);
authCallback(){
ItWillCallPromise.then(function(ID){ console.log(ID)};
}
ItWillCallPromise = function(){
return fetch(url).then(function(a){ return a.ID;}
}
不确定这是否是您的代码片段中的错字。 但据此,它不会被调用,因为你没有调用:)。 ()
丢失。
ItWillCallPromise().then(function(ID){ console.log(ID)};
在 MSAL.js v1 中, handleRedirectCallback
仅在 url 中存在 hash 响应时才会调用(调用loginRedirect
或acquireTokenRedirect
的结果)。 Depending on the business logic of your app, you can first check if there is a hash in the url (before instantiating UserAgentApplication, which will clear the hash if one was present) and then check if a user is logged in, and then invoke ItWillCallPromise
直接地。
例如:
function initializeAuthAgent() {
var authConfig = Config();
var hasHash = !!window.location.hash;
authAgent = new Msal.UserAgentApplication({
auth: {
clientId: authConfig.clientId,
authority: authConfig.authority,
validateAuthority: false,
postLogoutRedirectUri: '/index.html'
},
cache: {
cacheLocation: 'sessionStorage'
}
});
var isLoggedIn = !!authAgent.getAccount();
// If there is already a logged in user and were not returning from a redirect operation, immediately call ItWillCallPromise.
if (isLoggedIn && !hasHash) {
ItWillCallPromise()
}
authAgent.handleRedirectCallback(authCallback);
authCallback(){
ItWillCallPromise.then(function(ID){ console.log(ID)};
}
ItWillCallPromise = function(){
return fetch(url).then(function(a){ return a.ID;}
}
MSAL.js v2(在 npm 上又名@azure/msal-browser
)使这更容易,因为在每次页面加载时都会调用handleRedirectPromise
(它替换handleRedirectCallback
)。 更多详细信息: https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/v1-migration.md
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.