代码之家  ›  专栏  ›  技术社区  ›  akaparadox

使用Microsoft Entra(Azure AD)获取SPA应用程序的刷新令牌

  •  0
  • akaparadox  · 技术社区  · 9 月前

    我已在Microsoft Entra中注册了一个应用程序作为 SPA多租户 具有“文件”权限的应用程序。读写、离线访问和用户。读。

    我在我的前端使用了STRATlibrary。我可以通过以下方式获得访问令牌 登录页面 库提供的方法。

    const microsoftLogin=async ()=>
      {
        const loginResponse=await instance.loginPopup(loginRequest).catch((e) => {
          console.log(e);
        });
       console.log(JSON.stringify(loginResponse));
      } 
    

    现在我也需要得到 refreshToken 以便我可以在以后的任何时间点使用它来获取新的访问令牌。该方法在响应中不提供任何authCode或refreshToken。 我正在通过sessionStorage检索访问令牌,Microsoft将值与密钥一起保存为

    UNIQUEID+“.”+loginResponse.tenantId+“-login.windows.net刷新令牌-”+MICROSOFT_CLIENT_ID+“----”

    但是,我也无法使用它获得accessToken。我通过邮递员尝试了api。我没有客户的秘密。 getting accessToken via postman

    我需要做什么才能获得refresh_token,通过SPA配置的应用程序通过refresh_toke获得访问令牌。我需要更改我的Entra申请表中的任何内容吗?

    1 回复  |  直到 9 月前
        1
  •  1
  •   Rukmini    9 月前

    错误 “AADSTS9002327:为‘单页应用程序’客户端类型发行的代币只能通过跨源请求兑换” 如果您没有在请求中传递origin作为标头,则通常会发生这种情况。

    要为SPA应用程序生成访问和刷新令牌,请检查以下内容:

    创建了一个 Microsoft Entra ID应用程序 并将重定向URL配置为SPA:

    enter image description here

    使用以下端点登录用户并生成 代码 :

    https://login.microsoftonline.com/common/oauth2/v2.0/authorize? 
    response_type=code  
    &client_id=ClientID
    &scope=Files.ReadWrite offline_access User.Read
    &redirect_uri=https://jwt.ms
    &code_challenge=XXX
    &code_challenge_method=S256
    

    enter image description here

    enter image description here

    生成 访问和刷新令牌 通过传递以下参数:

    https://login.microsoftonline.com/common/oauth2/v2.0/token
    
    client_id : ClientID
    grant_type : authorization_code
    code : code
    redirect_uri : https://jwt.ms
    code_verifier : S256
    scope : Files.ReadWrite offline_access User.Read
    

    确保通过 origin 头球 (值为重定向URL) :

    enter image description here

    enter image description here

    要刷新访问令牌,请使用以下参数:

    https://login.microsoftonline.com/common/oauth2/v2.0/token
    client_id:appID
    grant_type:refresh_token
    refresh_token: xxx //paste the refresh token that you got above
    

    确保通过 起源 头球 (值为重定向URL) :

    enter image description here

    我能够成功刷新访问令牌:

    enter image description here

    推荐文章