首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure访问令牌react aad-msal

Azure访问令牌react aad-msal
EN

Stack Overflow用户
提问于 2020-07-11 00:51:21
回答 1查看 4.9K关注 0票数 3

我试图使用下面的库“react aad-msal”来验证Azure中的用户并检索访问令牌。一旦检索,我将把它传递给我的安全api。问题是,每次登录时,我都无法获得访问令牌。不知何故访问令牌没有出现。

这是我的authProvider文件

代码语言:javascript
复制
// authProvider.js
import { MsalAuthProvider, LoginType } from 'react-aad-msal';
    
// Msal Configurations
const config = {
      auth: {
        authority:
        // cannot use https://login.microsoftonline.com/common because my tenant doesnt allow it
          'https://login.microsoftonline.com/b8630d64-c577-438b-9b8a-8c2d51da77d4/',
        clientId: '5ea2d3b2-ea57-4648-b05a-6dea685333f8',
        redirectUri: 'https://myWebsite.azurewebsites.net',
      },
      cache: {
        cacheLocation: 'localStorage',
        storeAuthStateInCookie: true,
      },
    };
    
    // Authentication Parameters
    const authenticationParameters = {
      scopes: ['user.read'],
    };
    
    // Options
    const options = {
      loginType: LoginType.Redirect,
      tokenRefreshUri: window.location.origin + '/auth.html',
    };
    
    export const authProvider = new MsalAuthProvider(
      config,
      authenticationParameters,
      options
    );

在我的App.jsx上我用这个

代码语言:javascript
复制
/* eslint-disable react/jsx-no-comment-textnodes */
import React, { Component } from 'react';

import { AzureAD, AuthenticationState } from 'react-aad-msal';
import { authProvider } from './constant/authProvider';
import Inside from './Inside';

class App extends Component {
  render() {
    return (
      <div className="App">
        <AzureAD provider={authProvider} forceLogin={true}>
          <React.Fragment>
            <AzureAD provider={authProvider} forceLogin={true}>
              <Inside provider={authProvider}></Inside>
              <span>Only authenticated users can see me.</span>
            </AzureAD>

            <AzureAD provider={authProvider} forceLogin={true}>
              {({ login, logout, authenticationState, error, accountInfo }) => {
                // eslint-disable-next-line default-case
                switch (authenticationState) {
                  case AuthenticationState.Authenticated:
                    return (
                      <p>
                        <p>
                          <span style={{ fontWeight: 'bold' }}>ID Token:</span>{' '}
                          {accountInfo.jwtIdToken}
                        </p>
                        <p>
                          <span style={{ fontWeight: 'bold' }}>Username:</span>{' '}
                          {accountInfo.account.userName}
                        </p>
                        <p>
                          <span style={{ fontWeight: 'bold' }}>
                            Access Token:
                          </span>{' '}
                          {accountInfo.jwtAccessToken}
                        </p>
                        <p>
                          <span style={{ fontWeight: 'bold' }}>Name:</span>{' '}
                          {accountInfo.account.name}
                        </p>
                        <button onClick={logout}>Logout</button>
                      </p>
                    );
                  case AuthenticationState.Unauthenticated:
                    return (
                      <div>
                        {error && (
                          <p>
                            <span>
                              An error occured during authentication, please try
                              again!
                            </span>
                          </p>
                        )}
                        <p>
                          <span>Hey stranger, you look new!</span>
                          <button onClick={login}>Login</button>
                        </p>
                      </div>
                    );
                  case AuthenticationState.InProgress:
                    return <p>Authenticating...</p>;
                }
              }}
            </AzureAD>
          </React.Fragment>
        </AzureAD>

        <div> Page Tests</div>
      </div>
      // <div className="App">
      //   <MainTable />
      // </div>
    );
  }
}

export default App;

我能够检索accountInfo.userName,但不能检索accountInfo.jwtAccessToken。对于inside.jsx,我有以下内容

代码语言:javascript
复制
    import React, { useState, useEffect } from 'react';

import PropTypes from 'prop-types';

const Inside = ({ provider }) => {
  useEffect(() => {
    const request = (url) => {
      provider.getAccessToken().then((token) => {
        console.log('token', token);
      });
    };

    request();
  }, []);
  return <div>Inside</div>;
};

Inside.propTypes = {};

export default Inside;
EN

回答 1

Stack Overflow用户

发布于 2022-01-08 09:57:21

您可以以这种方式获得访问令牌,因为authProvider['_accountInfo'].jwtIdToken是一个数组,而_accountInfo是获取jwtIdToken的关键。

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

https://stackoverflow.com/questions/62844059

复制
相关文章

相似问题

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