【发布时间】:2018-03-10 11:36:18
【问题描述】:
我正在开发一个 Asp.Net-Core-2.0 Web API 项目。我将 Web API 发布到 IIS,现在当我尝试访问它时,它会在所有控制器中给我一个 404 Error 消息。
我尝试用谷歌搜索所有内容,但没有任何帮助。
如果有人需要任何信息,请在问题下方留言,我会更新我的问题。
Program.cs 文件
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
Startup.cs 文件
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
//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.AddScoped<AuthRepository>();
services.AddSingleton<IKapanRepository, KapanRepository>();
services.AddSingleton<IDepartmentRepository, DepartmentRepository>();
services.AddSingleton<IPriorityRepository, PriorityRepository>();
services.AddSingleton<IPlanRepository, PlanRepository>();
services.AddSingleton<IRuleTemplateRepository, RuleTemplateRepository>();
services.AddSingleton<IDamageReportRepository, DamageReportRepository>();
services.AddSingleton<IDashBoardRepository, DashBoardRepository>();
services.AddSingleton<IPacketRepository, PacketRepository>();
services.AddSingleton<IEmployeeRepository, EmployeeRepository>();
services.AddSingleton<IPriceCalculatorRepository, PriceCalculatorRepository>();
services.AddSingleton<ISearchRepository, SearchRepository>();
services.AddSingleton<IUserInfoRepository, UserInfoRepository>();
services.AddSingleton<IOverLossRepository, OverLossRepository>();
// Container could be configured via services as well.
// Just be careful not to override registrations
services.AddDbContext<ApplicationContext>(opts =>
opts.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
// Add framework services.
services.AddMvc();
services.AddIdentity<ApplicationUser, IdentityRole>(options => {
options.Password.RequireNonAlphanumeric = false;
})
.AddEntityFrameworkStores<ApiDbContext>()
.AddDefaultTokenProviders();
services.AddDbContext<ApiDbContext>(options => options.UseSqlServer(Settings.ConnectionStringAuth));
// return 401 instead of redirect to login
services.ConfigureApplicationCookie(options => {
options.Events.OnRedirectToLogin = context => {
context.Response.Headers["Location"] = context.RedirectUri;
context.Response.StatusCode = 401;
return Task.CompletedTask;
};
});
services.AddAuthentication(sharedOptions => {
sharedOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(cfg => {
cfg.RequireHttpsMetadata = false;
cfg.TokenValidationParameters = new TokenValidationParameters() {
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
LifetimeValidator = CustomLifetimeValidator,
ValidIssuer = Configuration["TokenAuthentication:Issuer"],
ValidAudience = Configuration["TokenAuthentication:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration["TokenAuthentication:client_secret"]))
};
cfg.Events = new JwtBearerEvents {
OnAuthenticationFailed = context => {
Console.WriteLine("OnAuthenticationFailed: " +
context.Exception.Message);
return Task.CompletedTask;
},
OnTokenValidated = context => {
Console.WriteLine("OnTokenValidated: " +
context.SecurityToken);
return Task.CompletedTask;
}
};
});
services.Configure<IISOptions>(options =>
{
options.ForwardClientCertificate = false;
});
}
private bool CustomLifetimeValidator(DateTime? notBefore, DateTime? expires, SecurityToken securityToken, TokenValidationParameters validationParameters)
{
if (expires != null) {
return expires > DateTime.UtcNow;
}
return false;
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMiddleware<TokenProviderMiddleware>();
app.UseMiddleware<RefreshTokenProviderMiddleware>();
app.UseAuthentication();
app.UseMvc();
}
}
任何帮助将不胜感激。
【问题讨论】:
-
请分享program.cs和startup.cs
-
@MarcusHöglund 添加文件请检查。
-
@MarcusHöglund 在程序中我添加了
CreateDefaultBuilder,它也会自动使用UseIISIntegration()和Kestrel。在docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/…中读到这一点ApplicationConfiguration asp.net core 2 -
好的,太好了,我不知道,谢谢。 Windows 应用程序日志说明了什么?有什么错误吗?
-
你解决了这个问题吗?
标签: asp.net-core-2.0 iis-8 iis-8.5 .net-core-2.0