我正在使用重定向流程来执行身份验证流程,但有时在登录重定向后会遇到一个取消的过程,当 acquireTokenSilent
这是我的登录过程
public async componentDidMount() {
msalApp.handleRedirectCallback(error => {
if (error) {
const errorMessage = error.errorMessage ? error.errorMessage : "Unable to acquire access token.";
// setState works as long as navigateToLoginRequestUrl: false
}
});
if (!msalApp.getAccount()) {
if (isMobile) {
msalApp.loginRedirect(GRAPH_REQUESTS.CDA);
}
else {
const response = await msalApp
.loginPopup(GRAPH_REQUESTS.CDA)
.catch(error => {
;
});
console.log("after login" + response);
this.props.onFetchTemplate();
}
} else { this.props.onFetchTemplate(); }
}
获取访问令牌功能
export const getTokenRedirect = (): Promise<string> => {
return new Promise((resolve, reject) => {
msalApp.acquireTokenSilent({
scopes: [GRAPH_SCOPES.SITE_TEMPLATE]//,loginHint: JSON.parse((JSON.stringify(msalApp.getAccount(), null, 4))).name
})
.then(resp => resolve(resp.accessToken)).catch(error => {
if (requiresInteraction(error.errorCode)) {
msalApp.acquireTokenRedirect(GRAPH_REQUESTS.CDA);
}
});
});
};
还有我的配置
export const authRedirect = {
clientId: "****",
authority: "https://login.microsoftonline.com/tennatid",
validateAuthority: true,
postLogoutRedirectUri: "https://myweburl",
navigateToLoginRequestUrl: false
};
export const msalApp = new UserAgentApplication({
auth: isMobile == true ? authRedirect : auth,
cache: {
cacheLocation: "sessionStorage",
storeAuthStateInCookie: isIE()
},
system: {
navigateFrameWait: 0
}
});