无法从同一网络上的不同计算机访问 ASP.Net 核心 RESTAPI(作为 windows 服务托管)
Unable to access ASP.Net Core RESTAPI (Hosted As a windows Service) from different machine on the same network
我正在使用 WindowsService 托管 ASP.net Core API,它在我的本地机器上运行良好,但我无法在同一网络上的其他机器上访问它。
当我直接通过 Kestrel 打开 EXE 时,我可以收听,但是当我将它作为 Windows 服务托管时,我只能在我的本地机器上收听,而不能在网络上的其他机器上收听。
PS: 我是运行本地账号下的WindowsService
Google Chrome 错误:ERR_CONNECTION_TIMED_OUT
Program.CS
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
var repository = new ServiceRepository();
var certificatePath = ConfigHelper.AppSetting("certPath");
var certificatePassword = repository.Decrypt(ConfigHelper.AppSetting("certPass"));
var certificate = new X509Certificate2(certificatePath, certificatePassword);
return Host.CreateDefaultBuilder(args)
.ConfigureWebHost(webBuilder =>
{
webBuilder.UseKestrel(options =>
{
options.AddServerHeader = false;
options.Listen(IPAddress.Any, 44302, listenOptions =>
{
listenOptions.UseHttps(certificate);
});
options.Listen(IPAddress.Any, 5000);
});
webBuilder.UseStartup<Startup>();
}).UseWindowsService();
}
启动:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(option =>
{
option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
option.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(option =>
{
option.RequireHttpsMetadata = true; //made purposly to test ssl with kestrel
option.TokenValidationParameters = new TokenValidationParameters()
{
ValidateLifetime = true,
ValidateIssuer = true,
ValidateAudience = true,
ValidIssuer = ConfigHelper.AppSetting("issuer"),
ValidAudience = ConfigHelper.AppSetting("audience"),
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(ConfigHelper.AppSetting("secretkey"))),
ClockSkew = TimeSpan.Zero
};
});
services.AddControllers().AddNewtonsoftJson(options =>
{
// Use the default property (Pascal) casing
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
services.AddScoped<IApplication, Application>();
services.AddScoped<IServiceRepository, ServiceRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "ServiceNS/{action}");
});
}
端口被防火墙阻止,我必须添加入站规则并指定我在我的应用程序中使用的端口。这样防火墙就不会阻止我的传入请求端口。
参考文献:
我正在使用 WindowsService 托管 ASP.net Core API,它在我的本地机器上运行良好,但我无法在同一网络上的其他机器上访问它。
当我直接通过 Kestrel 打开 EXE 时,我可以收听,但是当我将它作为 Windows 服务托管时,我只能在我的本地机器上收听,而不能在网络上的其他机器上收听。
PS: 我是运行本地账号下的WindowsService
Google Chrome 错误:ERR_CONNECTION_TIMED_OUT
Program.CS
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
var repository = new ServiceRepository();
var certificatePath = ConfigHelper.AppSetting("certPath");
var certificatePassword = repository.Decrypt(ConfigHelper.AppSetting("certPass"));
var certificate = new X509Certificate2(certificatePath, certificatePassword);
return Host.CreateDefaultBuilder(args)
.ConfigureWebHost(webBuilder =>
{
webBuilder.UseKestrel(options =>
{
options.AddServerHeader = false;
options.Listen(IPAddress.Any, 44302, listenOptions =>
{
listenOptions.UseHttps(certificate);
});
options.Listen(IPAddress.Any, 5000);
});
webBuilder.UseStartup<Startup>();
}).UseWindowsService();
}
启动:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(option =>
{
option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
option.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(option =>
{
option.RequireHttpsMetadata = true; //made purposly to test ssl with kestrel
option.TokenValidationParameters = new TokenValidationParameters()
{
ValidateLifetime = true,
ValidateIssuer = true,
ValidateAudience = true,
ValidIssuer = ConfigHelper.AppSetting("issuer"),
ValidAudience = ConfigHelper.AppSetting("audience"),
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(ConfigHelper.AppSetting("secretkey"))),
ClockSkew = TimeSpan.Zero
};
});
services.AddControllers().AddNewtonsoftJson(options =>
{
// Use the default property (Pascal) casing
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
services.AddScoped<IApplication, Application>();
services.AddScoped<IServiceRepository, ServiceRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "ServiceNS/{action}");
});
}
端口被防火墙阻止,我必须添加入站规则并指定我在我的应用程序中使用的端口。这样防火墙就不会阻止我的传入请求端口。
参考文献: