【问题标题】:React-add-msal getAccessToken unable to load from cacheReact-add-msal getAccessToken 无法从缓存中加载
【发布时间】: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
    })
}

【问题讨论】:

    标签: reactjs msal.js


    【解决方案1】:

    我终于成功了。在我的 authProvider.getAccessToken() 中,我从 2 个不同的 API 请求 2 个作用域,似乎 AzureAD 不喜欢这样。我删除了一个,瞧。

    我仍然想知道为什么。可能是 Azure 中某处的错误配置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 2018-09-25
      • 2017-12-29
      • 1970-01-01
      相关资源
      最近更新 更多