【发布时间】:2020-07-10 18:38:47
【问题描述】:
我正在尝试使用 react-aad-msal。一切正常(我可以从 Azure 获取我的令牌),直到我尝试从缓存中获取它。 当我使用 getAccessToken() 方法时,页面会一次又一次地重新加载。 msal 似乎无法从缓存中加载令牌。我尝试了这些解决方案(Application does not fetch access token from cache using MSAL (react-aad-msal)),但没有奏效。 你有什么想法吗?
这是我的配置:
import jwt from 'jwt-decode'
import { MsalAuthProvider, LoginType } from 'react-aad-msal';
// Msal Configurations
const config: any = {
auth: {
authority: 'https://login.microsoftonline.com/<tenantID>',
clientId: '<frontEndClientID>',
redirectUri: 'http://localhost:3000/signin-oidc'
},
cache: {
cacheLocation: "localStorage",
storeAuthStateInCookie: false
}
};
// Authentication Parameters
const authenticationParameters = {
scopes: [
"https://graph.microsoft.com/User.Read",
"api://<backEndAPIClientID>/Room.ReadWrite"
]
}
// Options
const options = {
loginType: LoginType.Redirect,
tokenRefreshUri: window.location.origin + '/auth.html'
}
export const authProvider = new MsalAuthProvider(config, authenticationParameters, options)
export const authenticateRequest = async (headers: any) => {
// Get the authentication token
const token = await authProvider.getAccessToken();
console.log(jwt(token.accessToken));
return {
headers: {
...headers,
authorization: token.accessToken ? `Bearer ${token.accessToken}` : "",
}
}
};
这是我的要求:
export async function getAllRooms(options: object) {
let queryOptions = "?";
Object.entries(options).forEach(
([key, value]) => {
queryOptions = queryOptions.concat(key).concat("=").concat(value).concat("&");
}
);
return axios.get(endpoint + queryOptions, await authenticateRequest({}))
.then(response => {
...
})
.catch(err => {
throw err
})
}
【问题讨论】: