Microsoft.Web.Identity nuget >=1.9.2 使用 401 响应中断 AppService/Azure AD 登录

问题描述

使用托管在 Azure 应用服务(由 linux 应用服务计划支持)上的普通 vanilla(来自 VS2019 的开箱即用示例 asp.net 5 MVC Web 应用)进行测试。除了添加 [Authorize] 标记以针对来自认控制器的单个视图进行测试之外,没有任何更改或添加

Azure 中的认应用服务,Azure AD 中的认应用注册

我注意到每个版本的 Microsoft.Web.Identity >=1.9.2 在应用服务中运行时都会中断(但在本地运行良好)。当尝试访问受保护的视图时,它将返回 401。降级到 1.9.1 会将我重定向登录页面。是否有一些我遗漏的额外配置?

我的应用设置配置

"AzureAd": {
    "Instance": "https://login.microsoftonline.com/","Domain": "mydomain.org","TenantId": "XXX","ClientId": "XXX","CallbackPath": "/signin-oidc","SignedOutCallbackPath": "/signout-oidc"
  },

我的startup.cs

 public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));

            services.AddControllersWithViews(options =>
            {
                var policy = new AuthorizationPolicyBuilder()
                    .RequireAuthenticatedUser()
                    .Build();
                options.Filters.Add(new Authorizefilter(policy));
            });
            services.AddRazorPages()
                 .AddMicrosoftIdentityUI();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios,see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });
        }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)