ASP.NET Core部署至IIS后登录页出现递归302重定向问题求助
问题场景
我有一个采用Cookie认证的ASP.NET Core网站,认证配置代码如下:
builder.Services.AddAuthentication(opts => { opts.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; opts.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; opts.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; }).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, opts => { // ... opts.LoginPath = "/login.html"; });
该网站在Visual Studio开发服务器中运行正常:未认证请求会收到302重定向至登录页的响应,且登录页可正常访问。
但部署到IIS默认站点的子应用(路径/IISAppName,端口80)后,请求http://localhost/IISAppName/login.html时会陷入递归重定向,每次重定向都会触发OnRedirectToLogin事件。web.config配置如下:
<?xml version="1.0" encoding="utf-8"?> <configuration> <location path="." inheritInChildApplications="false"> <system.web> <!--<authentication mode="Forms"> <forms loginUrl="IISAppName/login.html" /> </authentication>--> </system.web> <system.webServer> <!--<security> <authentication> <anonymousAuthentication enabled="true" /> <windowsAuthentication enabled="true" /> </authentication> </security>--> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="./Server.exe" arguments="" stdoutLogEnabled="true" stdoutLogFile="C:\file.log" hostingModel="inprocess" forwardWindowsAuthToken="true" /> </system.webServer> </location> </configuration>
核心原因
- 虚拟路径未被正确处理:IIS子应用的虚拟路径
/IISAppName未被ASP.NET Core认证中间件识别,配置的LoginPath = "/login.html"会指向根站点的/login.html,而非子应用下的/IISAppName/login.html,导致重定向目标错误。 - 登录页未允许匿名访问:即使重定向路径正确,如果登录页本身被认证中间件拦截(未配置匿名访问),会再次触发认证挑战,形成循环重定向。
解决方案
1. 确保登录页允许匿名访问
在登录页的处理逻辑上添加匿名访问权限,避免被认证中间件拦截:
- 如果是MVC控制器/视图,在对应的控制器或Action上添加
[AllowAnonymous]特性:
[AllowAnonymous] public IActionResult Login() { return View(); }
- 如果是静态文件或端点路由,在映射时添加
.AllowAnonymous():
var app = builder.Build(); // 其他中间件配置... app.MapGet("/login.html", async context => { await context.Response.SendFileAsync("wwwroot/login.html"); }).AllowAnonymous();
2. 配置ASP.NET Core识别IIS虚拟路径
通过设置PathBase让应用感知IIS子应用的虚拟路径,确保重定向URL生成正确:
var builder = WebApplication.CreateBuilder(args); // 从配置或环境变量获取IIS虚拟路径,也可直接指定 var pathBase = builder.Configuration["ASPNETCORE_PATHBASE"] ?? "/IISAppName"; if (!string.IsNullOrEmpty(pathBase)) { builder.WebHost.UsePathBase(pathBase); } // 配置Cookie认证时,结合PathBase设置LoginPath builder.Services.AddAuthentication(opts => { opts.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; opts.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; opts.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; }).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, opts => { opts.LoginPath = $"{pathBase}/login.html"; });
3. 修正IIS认证配置
在web.config中启用匿名认证,避免IIS层面的认证拦截:
<system.webServer> <security> <authentication> <anonymousAuthentication enabled="true" /> <!-- 若不需要Windows认证,可禁用 --> <windowsAuthentication enabled="false" /> </authentication> </security> <!-- 其他原有配置保留 --> </system.webServer>
同时确保注释掉system.web中的Forms认证配置,避免与ASP.NET Core的Cookie认证冲突。
内容的提问来源于stack exchange,提问作者Erithax
相关产品推荐
相关产品推荐

