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

AspNetBoilerPlate集成Azure AD B2C登录跳转问题求助

集成Azure AD B2C到AspNetBoilerPlate (.NET Core + Angular) 时登录跳转失败的排查请求

我目前在基于AspNetBoilerPlate(.NET Core + Angular)框架开发项目,正尝试集成Azure Active Directory B2C实现外部认证,但遇到了登录页无法跳转到Azure B2C实例的问题。我们希望尽量不重写ABP的基础代码,而且已经按照官方文档配置了相关内容,但还是没成功。下面是我们已经实现的代码片段(敏感信息已移除),恳请大家帮忙排查问题,找到可行的集成方案:

.NET Core 后端:Azure AD B2C 认证提供者类

在Portal.Web.Core项目的Authentication目录下,创建了继承自ExternalAuthProviderApiBase的AzureActiveDirectoryB2CAuthProvider类,目前暂时硬编码用户信息,后续会替换为从Azure AD B2C获取的真实数据:

public class AzureActiveDirectoryB2CAuthProvider : ExternalAuthProviderApiBase 
{ 
    public const string Name = "AzureB2C"; 
    public AzureActiveDirectoryB2CAuthProvider() { } 

    public override Task<ExternalAuthUserInfo> GetUserInfo(string accessCode) 
    { 
        ExternalAuthUserInfo user = new ExternalAuthUserInfo(); 
        user.EmailAddress = "admin@aol.com"; 
        user.Name = "Administrator"; 
        user.Provider = this.ProviderInfo.Name; 
        user.ProviderKey = "12345"; 
        user.Surname = "Cool Dude"; 
        return Task.FromResult(user); 
    } 
}

.NET Core 后端:Startup.cs 配置外部认证提供者

在Portal.Web.Host的Startup.cs的Configure方法中,在app.UseAuthentication()之后注册了外部认证提供者:

var externalAuthConfiguration = app.ApplicationServices.GetRequiredService<ExternalAuthConfiguration>(); 
externalAuthConfiguration.Providers.Add( 
    new ExternalLoginProviderInfo( 
        AzureActiveDirectoryB2CAuthProvider.Name, 
        string.Empty, 
        string.Empty, 
        typeof(AzureActiveDirectoryB2CAuthProvider) 
    ) 
);

Angular 前端:MSAL.js 集成

我们已经引入了Microsoft的MSAL.js库来处理认证,对以下文件做了修改:

1. auth-route-guard.ts

修改了canActivate方法,判断用户是否已认证,未认证时尝试跳转到微软登录页(注释掉了原有的ABP登录页跳转):

if (!this._sessionService.user) { 
    //this._router.navigate(['/account/login']); 
    this._loginService.updateUser(); 
    return false; 
}

2. login.service.ts(部分代码)

移除了敏感的Azure AD B2C细节,修改后的代码片段如下:

import { Injectable } from '@angular/core'; 
import { Router } from '@angular/router'; 
import { TokenAuthServiceProxy, AuthenticateModel, AuthenticateResultModel, ExternalLoginProviderInfoModel, ExternalAuthenticateModel, ExternalAuthenticateResultModel } from '@shared/service-proxies/service-proxies'; 
import { UrlHelper } from '@shared/helpers/UrlHelper'; 
import { AppConsts } from '@shared/AppConsts'; 
import { MessageService } from '@abp/message/message.service'; 
import { LogService } from '@abp/log/log.service'; 
import { TokenService } from '@abp/auth/token.service'; 
import { UtilsService } from '@abp/utils/utils.service'; 

declare var Msal: any; 

@Injectable() 
export class LoginService { 
    static readonly twoFactorRememberClientTokenName = 'TwoFactorRememberClientToken'; 
    private clientApplication: any; 
    authenticateModel: AuthenticateModel; 
    authenticateResult: AuthenticateResultModel; 
    externalAuthenticateModel: ExternalAuthenticateModel; 
    externalAuthenticateResult: ExternalAuthenticateResultModel; 
    rememberMe: boolean; 
    B2CTodoAccessTokenKey = "msal.idtoken"; 

    tenantConfig = { 
        tenant: "", 
        clientID: '', 
        signUpSignInPolicy: "", 
        b2cScopes: ["openid"] 
    }; 

    // Configure the authority for Azure AD B2C 
    authority = "https://login.microsoftonline.com/tfp/" + this.tenantConfig.tenant + "/" + this.tenantConfig.signUpSignInPolicy; 

    /* 
     * B2C SignIn SignUp Policy Configuration 
     */ 
    //clientApplication = new Msal.UserAgentApplication( 
    // this.tenantConfig.clientID, this.authority, 
    // function (errorDesc: any, token: any, error: any, tokenType: any) { 
    // // Called after loginRedirect or acquireTokenPopup 
    // } 
    //); 

    constructor( 
        private _tokenAuthService: TokenAuthServiceProxy, 
        private _router: Router, 
        private _utilsService: UtilsService, 
        private _messageService: MessageService, 
        private _tokenService: TokenService, 
        private _logService: LogService, 
    ) { 
        this.clear(); 
        this.clientApplication = new Msal.UserAgentApplication( 
            this.tenantConfig.clientID, this.authority, this.authCallback); 
    } 

    authenticate(finallyCallback?: () => void): void { 
        finallyCallback = finallyCallback || (() => { }); 
        var model = new ExternalAuthenticateModel; 
        model.authProvider = "AzureB2C"; 
        model.providerAccessCode = this.getAccessToken(); 
        model.providerKey = "12345"; 
        this._tokenAuthService 
            .externalAuthenticate(m

我们现在的核心问题是无法触发跳转到Azure B2C的登录页面,希望能得到帮助排查这个问题,找到一种无需大量重写ABP基础代码的Azure AD B2C集成方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:03:03