SSO登录使用React JS与Azure AD

huangapple go评论82阅读模式
英文:

SSO login with Azure AD using React JS

问题

我在开发单页面应用(SPA)时遇到了跨域令牌兑换的问题。我收到了以下错误消息:

"跨域令牌兑换仅允许用于 '单页面应用程序' 客户端类型。请求来源:'http://localhost:3000'。"

我的 React JS 应用程序代码如下:

App.js

import logo from './logo.svg';
import './App.css';
import { config } from './Config';
import { PublicClientApplication } from '@azure/msal-browser';
import { Component } from 'react';
import { render } from 'react-dom';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      error: null,
      isAuthenticated: false,
      user: {}
    };
    this.login = this.login.bind(this);
    this.publicClientApplication = new PublicClientApplication({
      auth: {
        clientId: config.appId,
        redirectUri: config.redirectUri,
        authority: config.authority
      }
    });
  }
  async login() {
    try {
      await this.publicClientApplication.loginPopup({
        scopes: config.scopes,
        prompt: "select_account"
      });
      this.setState({ isAuthenticated: true });
    } catch (err) {
      this.setState({
        isAuthenticated: false,
        user: {},
        error: err
      });
    }
  }

  logout() {
    this.publicClientApplication.logout();
  }

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
          </a>
          {this.state.isAuthenticated ? <p>Learn React </p> :
            <p><button onClick={() => this.login()}>Login In</button></p>}
        </header>
      </div>
    );
  }
}

export default App;

config.js

export const config = {
  appId: My_APP_ID,
  redirectUri: "http://localhost:3000",
  scopes: [
    'user.read'
  ],
  authority: "https://login.microsoftonline.com/My_APP_TENT_ID"
}

登录后输入用户名密码令牌 API 显示 400 错误请求。

请建议我上述代码有什么问题。如果有任何错误,请提出建议。先谢谢您。

英文:

I'm facing an issue with Cross-origin token redemption while developing a Single-Page Application (SPA). I'm getting the following error:

"Cross-origin token redemption is permitted only for the 'Single-Page Application' client-type. Request origin: 'http://localhost:3000'."

My React JS App code

App.js

import logo from &#39;./logo.svg&#39;;
import &#39;./App.css&#39;;
import { config } from &#39;./Config&#39;;
import { PublicClientApplication } from &#39;@azure/msal-browser&#39;;
import { Component } from &#39;react&#39;;
import { render } from &#39;react-dom&#39;;
class App extends Component {
constructor(props) {
super(props);
this. state = {
error: null,
isAuthenticated: false,
user:{}
}
this.login = this.login.bind(this);
this.publicClientApplication = new PublicClientApplication({
auth: {
clientId: config.appId,
redirectUri: config.redirectUri,
authority: config.authority
}
})
}
async login() {
try {
await this.publicClientApplication.loginPopup({
scopes: config.scopes,
prompt: &quot;select_account&quot;
});
this.setState({isAuthenticated:true})
} catch (err) {
this.setState({
isAuthenticated: false,
user: {},
error: err
})
}
}
logout() {
this.publicClientApplication.logout()
}
render() {
return (
&lt;div className=&quot;App&quot;&gt;
&lt;header className=&quot;App-header&quot;&gt;
&lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; /&gt;
&lt;p&gt;
Edit &lt;code&gt;src/App.js&lt;/code&gt; and save to reload.
&lt;/p&gt;
&lt;a
className=&quot;App-link&quot;
href=&quot;https://reactjs.org&quot;
target=&quot;_blank&quot;
rel=&quot;noopener noreferrer&quot;
&gt;
&lt;/a&gt;
{this.state.isAuthenticated ? &lt;p&gt;Learn React &lt;/p&gt;:
&lt;p&gt;&lt;button onClick={() =&gt; this.login()}&gt;Login In&lt;/button&gt;&lt;/p&gt; }
&lt;/header&gt;
&lt;/div&gt;
);
}
}
export default App;

config.js

export const config = {
appId:My_APP_ID,
redirectUri: &quot;http://localhost:3000&quot;,
scopes: [
&#39;user.read&#39;
],
authority:&quot;https://login.microsoftonline.com/My_APP_TENT_ID&quot;
}

`

after login enter username password token api show 400 bad request

enter image description here

Please suggest me what's wrong above code. If did any mistake suggest me. Thanks in advance.

答案1

得分: 0

When you create an application, it's under redirect URI, I'm looking to see where I can find it for an existing application.

You can also edit your manifest file for your application directly by adding redirect URIs:

"replyUrlsWithType": [
	{
		"url": "http://localhost:3000",
		"type": "Spa"
	},
    ...other urls
],

SSO登录使用React JS与Azure AD

SSO登录使用React JS与Azure AD

英文:

When you create an application, it's under redirect URI, I'm looking to see where I can find it for an existing application

SSO登录使用React JS与Azure AD

You can also edit your manifest file for your application directly by adding redirect URIs:

SSO登录使用React JS与Azure AD

	&quot;replyUrlsWithType&quot;: [
{
&quot;url&quot;: &quot;http://localhost:3000&quot;,
&quot;type&quot;: &quot;Spa&quot;
},
...other urls
],

huangapple
  • 本文由 发表于 2023年6月13日 18:19:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定