首页 > 解决方案 > Azure AD B2C 在尝试单击链接进行签名时抛出 404

问题描述

我有一个使用 Azure AD B2C 进行身份验证的 asp.netcore 3.1 Web 应用程序,这在本地运行应用程序时非常有效。当用户单击登录链接时,用户将被重定向到 azure Ad B2C 登录/注册页面。在用户进行身份验证后,他们将被重定向回本地应用程序。

下面是代码配置

 public void ConfigureServices(IServiceCollection services)
        {
    services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)
                   .AddAzureADB2C(options =>
                   {
                       Configuration.Bind("AzureAdB2C", options);
                   })
                   .AddCookie();

            services.Configure<OpenIdConnectOptions>(AzureADB2CDefaults.AuthenticationScheme, opt =>
             {
                 //Configuration.Bind("AzureAdB2C", opt);

                 opt.Authority = Configuration["OIDC:Authority"];
                 opt.RequireHttpsMetadata = true;
                 opt.GetClaimsFromUserInfoEndpoint = true;

                 opt.ClientId = Configuration["OIDC:ClientId"];
                 opt.ClientSecret = Configuration["OIDC:Secret"];
                 opt.ResponseType = "code";

                 opt.SaveTokens = true;
                 opt.AuthenticationMethod = OpenIdConnectRedirectBehavior.FormPost;

                 opt.Events = new OpenIdConnectEvents
                 {
                     OnUserInformationReceived = async ctx =>
                     {

                     },
                     OnTokenValidated =async  ctx =>
                     {
                         //Get user's immutable object id from claims that came from Azure AD
                         Guid userId = Guid.Empty;
                         if (ctx.HttpContext.User.Identity.IsAuthenticated)
                         {
                             if (!string.IsNullOrWhiteSpace(ctx.HttpContext.User.FindFirstValue("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name")))
                                 userId = Guid.Parse(ctx.HttpContext.User.FindFirstValue("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"));

                             //Get EF context
                             var userRepo = ctx.HttpContext.RequestServices.GetRequiredService<IUserRepository>();

                             //Check is user a super admin
                             userRepo.RecordLogin(new Client.Models.EditModel.Account.RecordLoginEditModel()
                             {
                                 AttemptedAt = DateTimeOffset.UtcNow,
                                 UserId = userId,
                                 LoginResult = "Success",
                                 OnlineState = "Online",
                                 SStGAppId = "CustomerPortal"
                             });
                         }

                         //return Task.CompletedTask();
                     }
                 };
             });

...
}

public async void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            ...

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();
            app.UseLogContextEnrichment();
            app.UseCorrelationEnrichment();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
            });
}

单击生产站点上的登录链接时会出现 404

在此处输入图像描述

如上图所示,Web 应用程序在 azure 应用程序服务上运行并从实时域访问,该站点尝试更改为 azure AD b2c 页面,然后立即重定向回 CustomDomain.com/signin-oidc它在 Azure AD B2c 门户中列为重定向 uri。

在此处输入图像描述

我试图找出问题所在。所有网站也都有 SSL 证书。

标签: asp.net-coreazure-ad-b2c

解决方案


我终于弄清楚了问题所在。用于 azure AD B2c 的重定向 URI 是:

我将其更改为:

我还添加了

这是我的应用服务上的自定义域。在应用服务上查看应用自定义域时,它是 sstg-app.com。

我希望这对其他人有帮助。


推荐阅读