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

使用Azure AD B2C登录后无法访问页面,出现ERR_EMPTY_RESPONSE错误

问题

点击注册按钮后,按配置应重定向至http://localhost:7225/home(已在Azure门户中设置该地址为Redirect URL),但仍出现错误提示:

localhost didn’t send any data. ERR_EMPTY_RESPONSE

当前使用MSAL.js实现认证,相关代码如下:

MSAL配置代码

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

export const b2cPolicies = {
  names: {
    signUpSignIn: "B2C_1_susi",
    resetPassword: "B2C_1_Reset",
  },
  authorities: {
    signUpSignIn: {
      authority: "https://abc.b2clogin.com/xyz.onmicrosoft.com/B2C_1_susi",
    }
  },
};

export const msalConfig = {
  auth: {
    clientId: "00000000000000",
    authority: b2cPolicies.authorities.signUpSignIn.authority,
    knownAuthorities: ["xyz.b2clogin.com"],
     redirectUri: "http://localhost:7225/home",
     postLogoutRedirectUri: "https://localhost:7225/",
    navigateToLoginRequestUrl: false,
  },
  cache: {
    cacheLocation: "sessionStorage",
    storeAuthStateInCookie: false,
  },
  system: {
    loggerOptions: {
      loggerCallback: (level: any, message: any, containsPii: any) => {
        if (containsPii) return;
        switch (level) {
          case LogLevel.Error:
            console.error(`MSAL Error: ${message}`);
            break;
          case LogLevel.Warning:
            console.warn(`MSAL Warning: ${message}`);
            break;
          case LogLevel.Info:
            console.info(`MSAL Info: ${message}`);
            break;
          case LogLevel.Verbose:
            console.debug(`MSAL Verbose: ${message}`);
            break;
          default:
            console.log(`MSAL: ${message}`);
            break;
        }
      },
      piiLoggingEnabled: false, 
      logLevel: LogLevel.Verbose, 
    },
  },
};
export const loginRequest = {
  scopes: ["openid", "offline_access"],
};
export const msalInstance = new PublicClientApplication(msalConfig);

Index.tsx代码

root.render(
  <React.StrictMode>
     <Provider store={store}>
      <MsalProvider instance={msalInstance}>
        <ApolloProvider client={graphqlclient}>
           <BrowserRouter>
            <App />
            </BrowserRouter>
        </ApolloProvider>
        </MsalProvider>
     </Provider>
  </React.StrictMode>
);

reportWebVitals();

App.tsx代码(调用Azure AD B2C登录页逻辑)

useEffect(() => {
    if (accounts.length === 0) {
            msalInstance.loginRedirect({
              scopes: ["openid"],
              authority: b2cPolicies.authorities.signUpSignIn.authority,
              redirectUri: "http://localhost:7225/home",
            });
          } 

      const callbackId = instance.addEventCallback((event:any) => {
      if (
        (event.eventType === EventType.LOGIN_SUCCESS || event.eventType === EventType.ACQUIRE_TOKEN_SUCCESS) &&
        event.payload?.account
      ) {
        const account = event.payload.account;
        instance.setActiveAccount(account); 
      }

      if (event.eventType === EventType.LOGIN_FAILURE) {
        if (event.error && event.error.errorMessage.includes('AADB2C90118')) {
          const resetPasswordRequest = {
            authority: b2cPolicies.authorities.resetPassword.authority,
            scopes: [], 
          };
          instance.loginRedirect(resetPasswordRequest);
        }
      } 
    });
    return () => {
      if (callbackId) { 
        instance.removeEventCallback(callbackId);
      }
    };
  }, [instance]);

排查与解决步骤

  1. 统一协议配置
    注意到postLogoutRedirectUri使用https协议,但redirectUri用的是http,需确保本地服务运行协议和重定向URL一致。如果本地服务是http,将postLogoutRedirectUri修改为http://localhost:7225/,避免协议不匹配导致跳转异常。

  2. 验证Azure门户重定向URI

    • 确认Azure AD B2C应用注册的重定向URI已准确添加http://localhost:7225/home,且类型为「单页应用(SPA)」。
    • 检查URI是否存在拼写错误,比如端口号、路径是否完全匹配。
  3. 简化MSAL重定向配置
    移除loginRedirect调用中重复的redirectUri参数,直接使用msalConfig全局配置的地址,避免配置冲突。修改后代码:

    msalInstance.loginRedirect({
      scopes: ["openid"],
      authority: b2cPolicies.authorities.signUpSignIn.authority
    });
    
  4. 检查路由与页面可用性
    确认/home路径已在BrowserRouter中配置对应页面组件,直接访问http://localhost:7225/home验证页面是否能正常加载,排除服务未启动或页面不存在导致的空响应。

  5. 分析MSAL日志
    已开启Verbose级别日志,查看浏览器控制台的MSAL日志信息,重点关注重定向前后的请求细节,定位授权码获取、状态校验等环节是否存在异常。

内容的提问来源于stack exchange,提问作者Ankit Podal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 20:40:23