【问题标题】:Exposing two end points from one ASP.NET Core Website On Service Fabric在 Service Fabric 上从一个 ASP.NET Core 网站公开两个端点
【发布时间】:2018-02-13 02:56:07
【问题描述】:

我在 Service Fabric 2.7 上使用 ASP.NET Core 1.1 网站,这是一个面向公众的网站,所有通信都通过 SSL(端口 443)完成。

但是,如果有人尝试错误地连接到端口 80 (http),我想将他们转发到相同的 URL 但切换到端口 443 (https)。

我实现这一点的方法是在同一个 ASP.NET Core 应用程序中使用两个端口侦听器,因为使用额外的无状态服务进行端口重定向似乎很荒谬。

我的问题是:

  1. 从端口 80 转发到端口 443 有没有比这个更好的技巧?
  2. 我可以在同一个 ASP.NET Core 网站中有两个监听器吗?如果是这样,您能指出我的相关资源吗?

【问题讨论】:

    标签: c# asp.net-core azure-service-fabric asp.net-core-1.1 service-fabric-stateless


    【解决方案1】:

    添加重定向规则以强制执行 https:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
    
        var options = new RewriteOptions()
           .AddRedirectToHttps();
    
        app.UseRewriter(options);
    

    服务清单:

    <Resources>
      <Endpoints>
        <Endpoint Name="ServiceEndpoint1" Protocol="http" Port="80"/>
        <Endpoint Name="ServiceEndpoint2" Protocol="https" Port="443"/>
      </Endpoints>
    </Resources>
    

    通讯监听器:

    var endpoints = Context.CodePackageActivationContext.GetEndpoints()
       .Where(endpoint => endpoint.Protocol == EndpointProtocol.Http || endpoint.Protocol == EndpointProtocol.Https)
       .Select(endpoint => endpoint.Name);
    
    return endpoints
       .Select(endpoint => new ServiceInstanceListener(serviceContext => 
          new KestrelCommunicationListener(serviceContext, endpoint), (url, listener) =>{[..]}));
    

    【讨论】:

    • 每个端口的协议不同,http 和 https。那么,endpoint 不应该不同,有两个监听器吗?
    • 另外,能否请您显示服务清单配置以允许两个端口?
    猜你喜欢
    • 2021-04-24
    • 2018-04-05
    • 2023-04-06
    • 1970-01-01
    • 2017-08-08
    • 2017-05-05
    • 2017-03-29
    • 2018-01-31
    • 2019-05-06
    相关资源
    最近更新 更多