首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React-add- getAccessToken无法从缓存加载

React-add- getAccessToken无法从缓存加载
EN

Stack Overflow用户
提问于 2020-03-30 19:41:55
回答 1查看 573关注 0票数 0

我正在尝试使用react-aad-msal。一切运行正常(我可以从Azure中获取令牌),直到我尝试从缓存中获取它。当我使用getAccessToken()方法时,页面一次又一次地重新加载。msal似乎无法从缓存中加载令牌。我尝试了这些解决方案(Application does not fetch access token from cache using MSAL (react-aad-msal)),但它不起作用。你有什么想法吗?

下面是我的配置:

代码语言:javascript
复制
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}` : "",
    }
  } 
};

下面是我的请求:

代码语言:javascript
复制
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
    })
}
EN

回答 1

Stack Overflow用户

发布于 2020-03-31 22:25:18

我终于设法让它工作起来了。在我的authProvider.getAccessToken()中,我从两个不同的API请求两个作用域,似乎AzureAD不喜欢这样。我已经移除了一个,瞧。

我仍然想知道为什么。也许是Azure中某个地方的配置错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60929788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档