我在CentO上部署了.net core 2.2 api。在我的工作区中,在Windows上,该api运行良好,但是当我尝试在CentOS上运行它时,会得到一个Null异常。
我认为问题出在JWT身份验证上,因为如果我尝试注释掉该配置,则api运行正常。但是有了它我得到了例外:
我得到的错误:
[root@localhost ~]# /bin/dotnet /var/www/vhosts/prueba.com/prueba.dll
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at prueba.Startup.ConfigureServices(IServiceCollection services)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServic eCollection services)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at prueba.Program.Main(String[] args) in C:\Users\user\source\repos\prueba\prueba\Program.cs:line 17
我的启动类:
using AutoMapper;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using System.Text;
namespace prueba
{
public class Startup
{
public Startup(IConfiguration configuration)
{
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.AddCors();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// Auto Mapper Configurations
services.AddSingleton(typeof(Startup));
//// configure strongly typed settings objects
var appSettingsSection = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettingsSection);
var appSettings = appSettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.TokenValidationParameters = new TokenValidationParameters
{
// The signing key must match!
//ValidateIssuerSigningKey = true,
//ValidateAudience = false,
//ValidateIssuer = false,
//IssuerSigningKeys = new List<SecurityKey> { signingKey },
// Validate the token expiry
ValidateLifetime = false,
RequireExpirationTime = false
};
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});
//// configure DI for application services
//services.AddScoped<IUserService, UserService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// global cors policy
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
}
}
}
我认为此配置应该很好,因为当我在Visual Studio中运行它时,我可以发出一个令牌并与其连接,但无法使其在CentO中工作。顺便说一下,我的CentOs版本是7,并且我已经安装了.net core 2.2 sdk。
非常感谢您!
---------编辑---------------------
这是我的appsettings.json:
{
"AppSettings": {
"Secret": "ESTA_ES_MI_SECRET"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
我检查了在编译项目时始终复制的选项。
我称呼我的项目的方式是: / bin / dotnet /var/www/vhosts/exchange.com/prueba.dll