我在Windows中创建了一个 mvc .net core2 应用程序,并将整个内容复制到我的MAC (highSierra 10.13.2)中并尝试使用VS运行它MAC (7.3.3 build 5)。
它在PC和我的MAC上完美编译。但它不能在我的MAC上启动
我在System.ArgumentNullException
文件中收到program.cs
。
默认情况下,VS.NET Windows上创建了所述文件,其中包含:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace [projectname namespace]
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
仅供参考,这是我的创业公司
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.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
var jwtAppSettingOptions = Configuration.GetSection(nameof(JwtIssuerOptions));
string SecretKey = "replaceme";
SymmetricSecurityKey _signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(SecretKey));
// Configure JwtIssuerOptions
services.Configure<JwtIssuerOptions>(options =>
{
options.Issuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)];
options.Audience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)];
options.SigningCredentials = new SigningCredentials(_signingKey, SecurityAlgorithms.HmacSha256);
});
var tokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)],
ValidateAudience = true,
ValidAudience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)],
ValidateIssuerSigningKey = true,
IssuerSigningKey = _signingKey,
RequireExpirationTime = false,
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero
};
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(configureOptions =>
{
configureOptions.ClaimsIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)];
configureOptions.TokenValidationParameters = tokenValidationParameters;
configureOptions.SaveToken = true;
});
// api user claim policy
services.AddAuthorization(options =>
{
options.AddPolicy("ApiUser", policy => policy.RequireClaim(Constants.Strings.JwtClaimIdentifiers.Rol, Constants.Strings.JwtClaims.ApiAccess));
});
services.AddAuthentication().AddFacebook(facebookOptions =>
{
facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
facebookOptions.SaveTokens = true;
facebookOptions.Scope.Add("gender");
facebookOptions.Scope.Add("email");
facebookOptions.Scope.Add("public_profile");
facebookOptions.Scope.Add("user_birthday");
});
services.AddAuthentication().AddGoogle(googleOptions =>
{
googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
});
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddMvc();
services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "Travellogger Web API", Description = ".NET Core 2" }); });
//services.Configure<Helpers.TravelloggerSettings>(Configuration);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
app.UseSwagger();
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Core 2 API"); });
}
}
注意: 我在mac上创建了一个示例mvc core2应用程序,它具有相同的代码(自动生成),并且运行良好,因此我没有从Microsoft下载额外的运行时来自{{3}},如下所示... < / p>
我是否需要另外配置?
我尝试在StackOverflow上搜索,但无法找到任何结果。
如果您之前有人问过,如果您能给我一个链接,我将不胜感激。
答案 0 :(得分:1)
您的信息并未完全解决问题,但您的问题很可能来自以下方面:
如果您还没有下载这些内容,那么该项目将无法正常运行。托管可能需要配置,而在Window的Visual Studio预配置的IIS中充当反向代理进行调试。
我推荐这些内容的原因是您的错误存在的第17行,是您主机的Build
部分。因此,无论是在启动时存在问题还是冒泡,或者在Build
的实例化中。产生以下内容:
使用WebHostBuilder实例创建主机。这通常是 在应用程序的入口点,Main方法中执行。在项目中 模板,Main位于Program.cs中。一个典型的Program.cs调用 CreateDefaultBuilder开始设置主机:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
- CreateDefaultBuilder执行以下任务:配置Kestrel 作为Web服务器。有关Kestrel默认选项,请参阅Kestrel ASP.NET Core中的Kestrel Web服务器实现的选项部分。
- 将内容根目录设置为返回的路径 Directory.GetCurrentDirectory。
- 从以下位置加载可选配置:
- appsettings.json。
- AppSettings的。{环境}上传.json。
- 用户机密时 该应用程序在开发环境中运行。
- 环境变量。
- 命令行参数。
- 配置控制台和调试的日志记录 输出。日志记录包括日志记录中指定的日志过滤规则 appsettings.json或的配置部分 appsettings。{Environment} .json文件。
- 在IIS后面运行时,启用 IIS集成。配置服务器侦听的基本路径和端口 在使用ASP.NET核心模块时。该模块创建一个反向 IIS和Kestrel之间的代理。还配置要捕获的应用程序 启动错误。有关IIS的默认选项,请参阅IIS选项 带有IIS的Windows上的主机ASP.NET核心部分。
<强>更新强>
如果代码真的是相同的,我怀疑。如上所述,您不会告诉构建器访问您的appsettings.json
文件。以下是代码:
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
此外,您的错误很可能源于此行:
string SecretKey = "replaceme";
SymmetricSecurityKey _signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(SecretKey));
答案 1 :(得分:0)
现在可以通过在启动配置上禁用第一个JWT部分以及使用授权的后续web api来实现.....
我没有从Microsoft
安装单独的运行时以下是startup.cs中的ConfigureServices的副本
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddAuthentication().AddFacebook(facebookOptions =>
{
facebookOptions.AppId = Configuration["Settings:Authentication:Facebook:AppId"];
facebookOptions.AppSecret = Configuration["Settings:Authentication:Facebook:AppSecret"];
facebookOptions.SaveTokens = true;
facebookOptions.Scope.Add("gender");
facebookOptions.Scope.Add("email");
facebookOptions.Scope.Add("public_profile");
facebookOptions.Scope.Add("user_birthday");
//facebookOptions.UserInformationEndpoint = "https://graph.facebook.com/v2.5/me?fields=id,name,email";
//facebookOptions.BackchannelHttpHandler = new Helpers.FacebookBackChannelHandler();
//https://stackoverflow.com/questions/32059384/why-new-fb-api-2-4-returns-null-email-on-mvc-5-with-identity-and-oauth-2
//https://github.com/aspnet/Security/issues/435
});
services.AddAuthentication().AddGoogle(googleOptions =>
{
googleOptions.ClientId = Configuration["Settings:Authentication:Google:ClientId"];
googleOptions.ClientSecret = Configuration["Settings:Authentication:Google:ClientSecret"];
});
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddMvc();
services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "Web API", Description = ".NET Core 2" }); });
}
要解决的下一个项目是JWT身份验证:)谢谢你们:)