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

Entra SSO本地及测试环境正常,部署为IIS子应用时出现502错误

问题描述

我为.NET Core应用配置了Entra SSO,该配置在本地及测试环境中运行正常。生产环境中,站点被部署为IIS主站点下的子应用,地址为mysite.com/update-my-data(测试环境地址为test.mysite.com/)。

本地测试时,我在个人租户中配置了应用,启动URL添加了update-my-data,重定向URI设为https://localhost:44357/update-my-data/signin-oidc,appsettings.json中的CallbackPath为/signin-oidc,该配置在本地及测试环境均正常工作。

生产环境中,日志显示用户已完成SSO认证,但认证后未跳转到正确页面,浏览器地址栏显示重定向URI(https://my.site.com/update-my-data/signin-oidc),同时出现502错误。

相关配置

appsettings.json

"AzureAd": {
    "ClientId": "xxxxxxxx",
    "ClientSecret": "yyyyyyyyyyyyy",
    "Authority": "https://login.microsoftonline.com/zzzzzzzzz",
    "CallbackPath": "/signin-oidc",
    "AppsEditorGroup": "editor",
    "RemEditorGroup": "editor"
},

SSO登录代码

[Route("login")]
public IActionResult Login()
{
    try
    {
        return RedirectToAction("home", "accounts");
    }
    catch (Exception ex)
    {
        Log.Error(ex, "SSO Error - Exception during authentication: " + ex.Message);
        return BadRequest();
    }
}

Program.cs认证配置

builder.Services.AddIdentity<IdentityUser, IdentityRole>(options =>
    {
        options.SignIn.RequireConfirmedAccount = false;
    })
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

// Configure authentication with separate cookie authentication for Identity
builder.Services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = OpenIdConnectDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    options.DefaultSignInScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi()
.AddMicrosoftGraph(builder.Configuration.GetSection("Graphv1"))
.AddInMemoryTokenCaches();

// ...

builder.Services.AddControllersWithViews(options =>
{
    var policy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .Build();
    options.Filters.Add(new AuthorizeFilter(policy));
});
解决方案

部署为IIS子应用时,需要针对路径上下文、Cookie和IIS配置做特殊调整:

  1. 设置应用路径基础(Path Base)
    在Program.cs中,app.UseRouting()之前添加路径基础配置,让应用知晓自身运行在子应用路径下:

    app.UsePathBase("/update-my-data");
    

    该配置会自动将路由、认证回调等路径与子应用路径拼接,确保/signin-oidc被解析为/update-my-data/signin-oidc。

  2. 调整认证Cookie路径
    为避免与主站点Cookie冲突,需指定Cookie的路径为子应用路径。修改Microsoft Identity认证配置:

    builder.Services.AddAuthentication(options =>
    {
        // 原有默认Scheme配置
    })
    .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"), options =>
    {
        options.Cookie.Path = "/update-my-data";
    })
    .EnableTokenAcquisitionToCallDownstreamApi()
    .AddMicrosoftGraph(builder.Configuration.GetSection("Graphv1"))
    .AddInMemoryTokenCaches();
    
  3. 检查IIS子应用配置

    • 确认子应用的应用程序池使用.NET Core托管模式,版本与应用匹配;
    • 验证子应用的物理路径指向正确的发布文件夹;
    • 检查IIS的URL重写规则,确保/signin-oidc路径未被拦截,请求能正确转发到.NET Core应用;
    • 若使用反向代理,排查代理配置是否导致502错误(如超时、后端应用未响应)。
  4. 修正登录跳转逻辑
    当前Login方法直接使用RedirectToAction可能无法正确生成子应用路径下的URL,改用Url.Action生成完整路径:

    [Route("login")]
    public IActionResult Login()
    {
        try
        {
            return Redirect(Url.Action("home", "accounts"));
        }
        catch (Exception ex)
        {
            Log.Error(ex, "SSO Error - Exception during authentication: " + ex.Message);
            return BadRequest();
        }
    }
    

    Url.Action会自动结合PathBase生成包含子应用路径的正确URL。

  5. 启用详细日志排查
    在appsettings.json中添加认证相关的Debug日志,进一步定位问题:

    "Logging": {
        "LogLevel": {
            "Microsoft.AspNetCore.Authentication": "Debug",
            "Microsoft.Identity.Web": "Debug"
        }
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 18:15:11