【发布时间】:2026-01-20 02:20:03
【问题描述】:
这是我遇到过的最棘手的问题之一。
我有一个在 Docker 容器中运行的 .net5 应用程序,在 Azure Web 应用程序中。 这是我的 Startup.cs 文件:
public class Startup
{
public Startup(
IConfiguration configuration,
IWebHostEnvironment hostingEnvironment)
{
this.Configuration = configuration;
this.HostingEnvironment = hostingEnvironment;
}
public IConfiguration Configuration { get; }
public IWebHostEnvironment HostingEnvironment { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(
IServiceCollection services)
{
services.AddRepositories(this.Configuration);
services.AddSettings(this.Configuration);
services.AddServices();
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddControllersWithViews();
services.AddRazorPages(options =>
{
options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
});
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = $"/Identity/Account/Login";
options.LogoutPath = $"/Identity/Account/Logout";
options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
});
services.AddAuthentication().AddGoogle(options =>
{
var googleAuthNSection =
this.Configuration.GetSection("AppSettings:Authentication:Google");
options.ClientId = googleAuthNSection["ClientId"];
options.ClientSecret = googleAuthNSection["ClientSecret"];
options.CorrelationCookie.SameSite = SameSiteMode.Lax;
})
;
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env,
ILogger<Startup> logger)
{
app.UseForwardedHeaders();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
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();
}
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
logger.LogInformation("Starting Migration");
using var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
context.Database.Migrate();
logger.LogInformation("Finished Migration");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"default",
"{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
问题:在大多数情况下,应用程序运行平稳。但是,有时它会意外失败并出现以下错误:
System.Exception: An error was encountered while handling the remote login.
---> System.Exception: The oauth state was missing or invalid.
--- End of inner exception stack trace ---
at
Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.HandleRequestAsync()
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext
context) at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
我有时会说,因为应用程序会运行几天而没有问题,然后随机返回这个错误几个小时。然后它又恢复正常工作了。
如果我出于调试目的启动本地应用服务器,则应用每次都按预期运行。
这可能是天蓝色资源问题吗?还是我达到了 Google API 允许的最大调用数?一次只有我和另一个人在访问该应用程序,而且我们不经常使用它。
谢谢
【问题讨论】:
标签: c# asp.net .net azure asp.net-core