【问题标题】:Azure AD B2C return 404 Error after authorizedAzure AD B2C 授权后返回 404 错误
【发布时间】:2020-04-21 15:02:43
【问题描述】:

我创建了一个连接到 Azure AD B2C 的 Dot net core Webapp (MVC)。

在我的项目中,我遵循https://elanderson.net/2019/04/asp-net-core-with-azure-b2c-auth/下的教程

当我使用 web 应用程序(不是 MVC / RazorPages)时,它可以正常工作。但是当我在验证后使用 MVC 时,它会返回到https://localhost:44363/signin-oidc 并给我一个404 Error

我将此添加到 startup.cs

   using Microsoft.AspNetCore.Authentication;
    using Microsoft.AspNetCore.Authentication.AzureADB2C.UI;



   // This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services)
        {

            services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)
                .AddAzureADB2C(options => Configuration.Bind("AzureAdB2C", options));

            services.AddControllersWithViews();
        }

创建 _LoginPartial.cshtml

@using Microsoft.AspNetCore.Authentication.AzureADB2C.UI
@using Microsoft.Extensions.Options
@inject IOptionsMonitor<AzureADB2COptions> AzureADB2COptions

@{
    var options = AzureADB2COptions.Get(AzureADB2CDefaults.AuthenticationScheme);
}


<ul class="navbar-nav">
    @if (User.Identity.IsAuthenticated)
    {
        @if (!string.IsNullOrEmpty(options.EditProfilePolicyId))
        {
            <li class="nav-item">
                <a class="nav-link text-dark" asp-area="AzureADB2C" asp-controller="Account" asp-action="EditProfile">
                    <span class="text-dark">Hello @User.Identity.Name!</span>
                </a>
            </li>
        }
        else
        {
            <li class="nav-item">
                <span class="navbar-text text-dark">Hello @User.Identity.Name!</span>
            </li>
        }
        <li class="nav-item">
            <a class="nav-link text-dark" asp-area="AzureADB2C" asp-controller="Account" asp-action="SignOut">Sign out</a>
        </li>
    }
    else
    {
        <li class="nav-item">
            <a class="nav-link text-dark" asp-area="AzureADB2C" asp-controller="Account" asp-action="SignIn">Sign in</a>
        </li>
    }
</ul>

另外,我将参数添加到我的 appsettings.json

【问题讨论】:

    标签: asp.net-mvc asp.net-core azure-ad-b2c


    【解决方案1】:

    请确保您在 Startup.Configure 中添加了app.UseAuthentication()。这就是处理回调路径的方法。

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
            {
                loggerFactory.AddConsole();
                loggerFactory.AddConsole(Configuration.GetSection("Logging"));
                loggerFactory.AddDebug();
    
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                    app.UseBrowserLink();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
    
                app.UseStaticFiles();
    
                app.UseSession();
    
                app.UseAuthentication();
    
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
            }
    

    另外,这里有一个sample,关于如何使用 Azure AD B2C 构建 MVC Web 应用程序。

    【讨论】:

      猜你喜欢
      • 2016-06-29
      • 2017-04-20
      • 2017-01-04
      • 2020-04-10
      • 2019-12-10
      • 2020-10-09
      • 2021-01-20
      • 2021-01-04
      • 2018-05-02
      相关资源
      最近更新 更多