【发布时间】:2018-09-18 16:22:25
【问题描述】:
我有一个 .Net core Web 应用程序,托管在安装了 .Net core 的 Windows Server 2016 环境中。
我在 Startup.cs 的 ConfigureServices 方法中包含过滤器后发现,发布到服务器 IIS,运行应用程序后,我收到了 Http 500 错误。
这是我的 ConfigureServices 和 Configure 方法中的代码
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
services.AddDbContext<LocalAppDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddSingleton<IConfiguration>(Configuration);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddAuthentication(IISDefaults.AuthenticationScheme);
// Global filter
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
config.Filters.Add(new AuthorizeFilter(policy));
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseStatusCodePagesWithRedirects("/Error/StatusCode/{0}");
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Requests}/{action=Index}/{id?}");
});
}
在日志文件中,我在所有堆栈输出的顶部看到了这一点:
失败:Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
发生未处理的异常:未指定 authenticationScheme,也未找到 DefaultChallengeScheme。
System.InvalidOperationException: 没有指定 authenticationScheme,也没有找到 DefaultChallengeScheme。
经过一番谷歌搜索,似乎是因为没有定义身份验证方案。
但在我的 services.AddMvc 行上方,我有 AddAuthentication,它使用 IISDefaults 身份验证方案:
services.AddAuthentication(IISDefaults.AuthenticationScheme);
知道在我的情况下是什么导致了这个 500 内部服务器错误吗?
【问题讨论】:
-
对不起柯克,在我删除初始 services.Addauthentication 行后,您更新的代码实际上使错误有所不同。以下是错误:失败:Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0] 发生未处理的异常:没有配置身份验证处理程序来对方案进行身份验证:Windows System.InvalidOperationException:没有配置身份验证处理程序来对方案进行身份验证:窗户
-
这是我的愚蠢错误,IIS中的应用程序没有启用Windows身份验证,而是默认使用匿名身份验证。
-
这是一个比您想象的更常见的错误。我很高兴你把它整理好了。谢谢你告诉我。
-
非常感谢您的帮助。您的建议确实让我有机会了解更多关于 Startup.cs 文件中的方法。
标签: c# authentication iis asp.net-core-2.0