我正在使用React,我必须通过Azure B2C
让我的用户登录,所以我正在尝试这样做,但我无法找出如何做到这一点以及什么是.
我尝试过的
- 我从 Microsoft 网站获得了这个示例,该示例是使用纯 JavaScript(普通)完成的,我不知道如何在我的 React 代码中实现它。
所以我尝试使用一些 react 库,我四处搜索并发现 This library
我遵循了他们编写的相同代码,但是当我点击登录按钮时,它会带我进入azure的登录页面,所以在我的app.js
中我正在做console.log (authentication.getAccessToken());
登录后抛出null,我不知道为什么
我的代码
authentication.initialize({
// optional, will default to this
instance: 'https://login.microsoftonline.com/tfp',
// My B2C tenant
tenant: 'mytenant.onmicrosoft.com',
// the policy to use to sign in, can also be a sign up or sign in policy
signInPolicy: 'B2c_signupsignin',
// the the B2C application you want to authenticate with (that's just a random GUID - get yours from the portal)
clientId: 'fdfsds5-5222-ss522-a659-ada22',
// where MSAL will store state - localStorage or sessionStorage
cacheLocation: 'sessionStorage',
// the scopes you want included in the access token
scopes: ['https://mytenant.onmicrosoft.com/api/test.read'],
// optional, the redirect URI - if not specified MSAL will pick up the location from window.href
redirectUri: 'http://localhost:3000',
});
然后点击登录我就会这样做
const Log_in = () => {
authentication.run(() => {});
};
在我的 app.js 中,我的做法如下
import authentication from 'react-azure-b2c';
function App() {
console.log(authentication.getAccessToken());
}
所以最初它显示 null 这很好,但登录后它也只抛出错误。
所以我无法解决这个问题,这就是为什么我转移到另一个与此几乎类似的库
因此,当我单击登录按钮时,我收到错误如下
我从 Microsoft 使用 valina Javascript 获得的示例,我认为这是完美的方法,但我不知道如何通过 React 来实现这一点
我已经在这里呆了很长时间了,我不知道现在该怎么办,无法在谷歌上找到很好的例子来用react来实现它
PS:我正在使用 React hooks 功能组件来编写我的代码,请指导我完成此操作
我只是想以正确的方式使用 React 来实现这个,我知道很多人已经在使用这个,所以我只想看一个很好的例子。
编辑/更新
我尝试这样做
b2cauth.initialize({
instance: 'https://mylogin.b2clogin.com/tfp',
tenant: 'mylogin.b2clogin.com',
signInPolicy: 'B2C_1_SigninSignupUsername',
clientId: 'fc3081ec-504a-4be3-a659-951a9408e248',
cacheLocation: 'sessionStorage',
scopes: ['https://mylogin.b2clogin.com/api/demo.read'],
redirectUri: 'http://localhost:3000',
});
b2cauth.run(() => {
ReactDOM.render(<App />, document.getElementById('root'));
serviceWorker.unregister();
});
我检查了作为答案粘贴的 Microsoft 链接,并更改了实例:
实例:'https://mylogin.b2clogin.com/tfp',
至
实例:'https://my-tanent-name.b2clogin.com/tenent-id/oauth2/authresp',
但我收到错误请求
我检查网络选项卡,检查它正在点击的网址,它正在下面点击
https://login.microsoftonline.com/common/discovery/instance?api-version=1.0&authorization_endpoint=https://my-tenatnt-name.b2clogin.com/tenant-id/oauth2/authrespmy-tenant-name.b2clogin.com/b2c_1_signinsignupusername/oauth2/v2.0/authorize
我尝试从实例中删除 https 并像这样点击它
//mytenant.b2clogin.com/tenant-id/oauth2/authresp
它抛出错误为Uncaught AuthorityUriInsecure
我认为它去错了地方
最佳答案
您的 b2c 实例坐标不正确(请参阅注释)。您可以找到更多详细信息:https://learn.microsoft.com/en-us/azure/active-directory-b2c/b2clogin
如果您愿意,可以使用此 sample ,它展示了如何使用 oidc-client.js
库从 React 应用程序使用 B2C 策略。默认情况下,它配置为使用 PKCE,但如果需要,您可以将其配置为使用隐式流(不推荐)。
git 存储库中提供了完整的说明,但这里是高级概述。
- 您需要首先在 B2C 中创建应用程序以及策略(未显示)。您应该添加两个重定向 uri -
https://localhost:3000
和https://localhost:3000/callback.html
- 如果您希望除了
id_token
之外还接收access_token
,您还可以添加权限。
- 您的 list 应类似于:
{
"id": "443ca8db-7bd1-4ebd-9671-ce94e006a18a",
"acceptMappedClaims": null,
"accessTokenAcceptedVersion": 2,
"addIns": [],
"allowPublicClient": null,
"appId": "50d2c416-a5ad-4c5c-b36a-0d1ac5b48167",
"appRoles": [],
"oauth2AllowUrlPathMatching": false,
"createdDateTime": "2020-09-02T00:11:35Z",
"groupMembershipClaims": null,
"identifierUris": [],
"informationalUrls": {
"termsOfService": null,
"support": null,
"privacy": null,
"marketing": null
},
"keyCredentials": [],
"knownClientApplications": [],
"logoUrl": null,
"logoutUrl": null,
"name": "OIDC-Test-APP",
"oauth2AllowIdTokenImplicitFlow": false,
"oauth2AllowImplicitFlow": false,
"oauth2Permissions": [],
"oauth2RequirePostResponse": false,
"optionalClaims": null,
"orgRestrictions": [],
"parentalControlSettings": {
"countriesBlockedForMinors": [],
"legalAgeGroupRule": "Allow"
},
"passwordCredentials": [],
"preAuthorizedApplications": [],
"publisherDomain": "contoso.onmicrosoft.com",
"replyUrlsWithType": [
{
"url": "http://localhost:3000/signin-callback.html",
"type": "Spa"
},
{
"url": "http://localhost:3000/",
"type": "Spa"
}
],
"requiredResourceAccess": [
{
"resourceAppId": "00000003-0000-0000-c000-000000000000",
"resourceAccess": [
{
"id": "37f7f235-527c-4136-accd-4a02d197296e",
"type": "Scope"
},
{
"id": "7427e0e9-2fba-42fe-b0c0-848c9e6a8182",
"type": "Scope"
}
]
},
{
"resourceAppId": "18ac2afe-2c1f-4ea8-8d63-14dd50ee4f85",
"resourceAccess": [
{
"id": "d5515006-5646-4398-ad59-fffc357f3423",
"type": "Scope"
}
]
}
],
"samlMetadataUrl": null,
"signInUrl": null,
"signInAudience": "AzureADandPersonalMicrosoftAccount",
"tags": [],
"tokenEncryptionKeyId": null
}
- 克隆存储库并更新 AuthSettings.ts 中的设置以匹配您的租户。您必须更新
client_id
和contoso
(租户名称)。
const settings = {
// This is the metadata endpoint
authority: 'https://contoso.b2clogin.com/contoso.onmicrosoft.com/v2.0/.well-known/openid-configuration?p=B2C_1A_signup_signin',
// Turn off calls to user info since CORS will block it
loadUserInfo: false,
// The URL where the Web UI receives the login result
redirect_uri: 'http://localhost:3000/signin-callback.html',
// The no longer recommended implicit flow must be used if CORS is disabled
// If you want to use impicit flow use id_token instead of code for the return type.
response_type: 'code',
// Other OAuth settings
client_id: '18ac2afe-2c1f-4ea8-8d63-14dd50ee4f85',
// openid is required, remove https://contoso.onmicrosoft.com/test/Read if access_token is not required.
scope: 'openid https://contoso.onmicrosoft.com/test/Read',
// Supply these details explicitly. Directly copied from azure ad b2c policy metadata endpoint.
metadata: {
issuer: 'https://contoso.b2clogin.com/9859cd0c-9d99-4683-abcc-87462f67a0bc/v2.0/',
authorization_endpoint: 'https://contoso.b2clogin.com/contoso.onmicrosoft.com/oauth2/v2.0/authorize?p=b2c_1a_signup_signin',
token_endpoint: 'https://contoso.b2clogin.com/contoso.onmicrosoft.com/oauth2/v2.0/token?p=b2c_1a_signup_signin',
jwks_uri : 'https://contoso.b2clogin.com/contoso.onmicrosoft.com/discovery/v2.0/keys?p=b2c_1a_signup_signin',
end_session_endpoint: "https://contoso.b2clogin.com/contoso.onmicrosoft.com/oauth2/v2.0/logout?p=b2c_1a_signup_signin&post_logout_redirect_uri=http%3A%2F%2Flocalhost:3000%2F"
},
} as UserManagerSettings;
使用
yarn
或npm
构建并运行应用应用程序默认在
上启动http://localhost:3000
- 点击“登录”,您将进入 B2C 政策以完成整个旅程。
- 完成 B2C 政策中的旅程后,您将被重定向回应用程序。
关于javascript - 如何将 Azure B2C 与 React 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63573283/