You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React应用中实现Azure AD无微软登录页认证方案咨询

React自定义登录页对接Azure AD认证实现方案

前置准备

  1. Azure AD应用配置
    • 在Azure门户注册SPA应用,添加匹配的重定向URI(如http://localhost:3000/)
    • 进入应用「身份验证」→「高级设置」,勾选「允许使用资源所有者密码凭证(ROPC)流」
    • 配置必要API权限(如User.Read、offline_access)并完成管理员授权
  2. 安装依赖
    npm install @azure/msal-browser @azure/msal-react
    

核心实现逻辑

通过Azure AD的资源所有者密码凭证(ROPC)流直接提交自定义页面收集的账号密码获取令牌;若用户开启MFA,ROPC请求会返回MFA要求错误,此时触发交互式弹窗完成MFA验证,最终获取有效令牌。

详细代码实现

1. MSAL实例初始化(src/authConfig.js)

import { PublicClientApplication } from "@azure/msal-browser";

export const msalConfig = {
  auth: {
    clientId: "你的Azure AD应用Client ID",
    authority: "https://login.microsoftonline.com/你的租户ID",
    redirectUri: "http://localhost:3000/", // 与Azure配置一致
  },
  cache: {
    cacheLocation: "sessionStorage",
    storeAuthStateInCookie: false,
  },
};

export const msalInstance = new PublicClientApplication(msalConfig);

// 定义请求权限范围
export const loginRequest = {
  scopes: ["User.Read", "offline_access"],
};

2. 自定义登录组件(src/components/CustomLogin.js)

import React, { useState } from "react";
import { msalInstance, loginRequest } from "../authConfig";

const CustomLogin = () => {
  const [username, setUsername] = useState("");
  const [password, setPassword] = useState("");
  const [errorMessage, setErrorMessage] = useState("");

  const handleLogin = async (e) => {
    e.preventDefault();
    setErrorMessage("");

    try {
      // 尝试通过ROPC流获取令牌
      const response = await msalInstance.acquireTokenByUsernamePassword({
        ...loginRequest,
        username,
        password,
      });

      // 认证成功,设置活跃账号并处理后续逻辑
      const account = msalInstance.getAccountByUsername(username);
      if (account) {
        msalInstance.setActiveAccount(account);
        console.log("认证成功,令牌信息:", response);
        // 可添加页面跳转、用户信息展示等逻辑
      }
    } catch (error) {
      console.error("认证错误:", error);
      // 检测是否为MFA要求错误
      if (error.errorCode === "AADSTS50076") {
        try {
          // 触发MFA验证弹窗,传递用户名跳过账号输入
          const mfaResponse = await msalInstance.loginPopup({
            ...loginRequest,
            loginHint: username,
            prompt: "select_account",
          });
          msalInstance.setActiveAccount(mfaResponse.account);
          console.log("MFA验证成功:", mfaResponse);
          // 处理后续逻辑
        } catch (mfaError) {
          setErrorMessage("MFA验证失败,请重试");
        }
      } else {
        setErrorMessage(`登录失败: ${error.errorMessage || "未知错误"}`);
      }
    }
  };

  return (
    <div style={{ maxWidth: "400px", margin: "2rem auto" }}>
      <h2>自定义登录</h2>
      {errorMessage && <p style={{ color: "red" }}>{errorMessage}</p>}
      <form onSubmit={handleLogin}>
        <div style={{ marginBottom: "1rem" }}>
          <label>账号:</label>
          <input
            type="email"
            value={username}
            onChange={(e) => setUsername(e.target.value)}
            required
            style={{ width: "100%", padding: "0.5rem", marginTop: "0.25rem" }}
          />
        </div>
        <div style={{ marginBottom: "1rem" }}>
          <label>密码:</label>
          <input
            type="password"
            value={password}
            onChange={(e) => setPassword(e.target.value)}
            required
            style={{ width: "100%", padding: "0.5rem", marginTop: "0.25rem" }}
          />
        </div>
        <button
          type="submit"
          style={{ width: "100%", padding: "0.75rem", backgroundColor: "#0078d4", color: "white", border: "none", borderRadius: "4px" }}
        >
          登录
        </button>
      </form>
    </div>
  );
};

export default CustomLogin;

3. 应用入口集成(src/App.js)

import React from "react";
import { MsalProvider } from "@azure/msal-react";
import { msalInstance } from "./authConfig";
import CustomLogin from "./components/CustomLogin";

function App() {
  return (
    <MsalProvider instance={msalInstance}>
      <div className="App">
        <CustomLogin />
      </div>
    </MsalProvider>
  );
}

export default App;

关键注意事项

  • ROPC流限制:仅支持Azure AD本地用户,不支持联邦身份(如ADFS、第三方联合登录)用户;不支持无密码登录方式
  • 安全性要求:必须通过HTTPS部署应用,防止密码传输泄露;仅在可信内部应用场景使用ROPC流
  • MFA处理:用户开启MFA时,ROPC流返回AADSTS50076错误,需通过交互式弹窗完成验证,代码中通过loginHint参数跳过账号输入步骤
  • 令牌管理:MSAL库自动处理令牌缓存、刷新逻辑,无需手动存储和维护令牌

内容的提问来源于stack exchange,提问作者Avanish Srivastava

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 06:55:37