我正在 React 中制作一个应用程序并使用“react-aad-msal”库(https://www.npmjs.com/package/react-aad-msal),该库使用 AzureAD 进行身份验证和授权,然后接收数据(从 API 列出任务)(正如我所举的例子https://github.com/Azure-Samples/ms-identity-javascript-angular-spa-dotnetcore-webapi-roles-groups/tree/master/chapter2/TodoListAPI)。我有以下组件:authProvider.js、TodoConstants.js、TodoActions.js、TodoReducer.js、TodoContainer.js、TodoSaga.js。当我添加 const token = wait authProvider.getAccessToken () 来从 TodoSaga.js 中的 API 请求数据时,我开始收到错误:未处理的拒绝 (ClientAuthError):需要用户登录。对于静默调用,请求必须包含 sid 或 login_hint。如果您在authProvider.js的authenticationParameters中添加loginHint: "myMail@domain.com",那么它就会开始正常工作并直接进入身份验证阶段,但如何正确指定loginHint或sid尚不清楚。请告诉我在这种情况下如何正确获取loginHint或sid?哪个参数更可取?
authProvider.js
import { MsalAuthProvider, LoginType } from "react-aad-msal";
import { Logger, LogLevel } from "msal";
const config = {
auth: {
clientId: "xxxx-xxxx-xxxx-xxxx",
authority:
"https://login.microsoftonline.com/yyyy-yyyy-yyyy-yyyy",
validateAuthority: true,
redirectUri: "http://localhost:3000",
postLogoutRedirectUri: "http://localhost:3000",
navigateToLoginRequestUrl: true,
},
cache: {
cacheLocation: "localStorage",
storeAuthStateInCookie: true,
},
};
const authenticationParameters = {
scopes: [
"openid",
"profile",
"api://iiii-iiii-iiii-iiii/access_as_user",
],
// …
Run Code Online (Sandbox Code Playgroud) 我需要通过模拟 react-aad-msal 为下面的类编写单元测试用例。我怎么能嘲笑它?我已经阅读了该酶模拟组件,但我收到错误消息
console.error node_modules/react-aad-msal/dist/commonjs/Logger.js:69
[ERROR] ClientAuthError: User login is required.
Run Code Online (Sandbox Code Playgroud)
我认为这与单元测试无关
import React, { Component } from "react";
import "./App.css";
import { authProvider } from "./authProvider";
import { AzureAD, AuthenticationState, IAzureADFunctionProps } from "react-aad-msal";
import Home from "./pages/Home";
import { ThemeProvider } from "styled-components";
import { lightTheme } from "./themes";
export default class App extends Component<{}, { theme: any }> {
static displayName = App.name;
constructor(props: any) {
super(props);
this.state = {
theme: lightTheme
};
}
render() {
return …
Run Code Online (Sandbox Code Playgroud) 我已使用连接到 Azure Active Directory B2C 的 MSAL 将身份验证添加到我的 Web 应用程序。
当我已连接并可以访问 Microsoft 站点时,此功能运行良好。然而,当我不存在时它就会失败。
例如在飞机上或在有防火墙的构建服务器中
如何添加模拟认证才能离线开发?