Exposing two end points from one ASP.NET Core Website On Service Fabric

959 views Asked by At

I am using an ASP.NET Core 1.1 Website on Service Fabric 2.7, this is a public facing website where all communication is done over SSL (port 443).

However, if someone tries to connect to port 80 (http) by mistake, I want to forward them to the same URL but switch to port 443 (https).

My approach to achieving this is having two port listener in the same ASP.NET Core application, as having an additional Stateless service for port redirection seems ridiculous.

My questions are:

  1. Is there a better trick in forwarding from port 80 to port 443 rather than this one?
  2. Can I have two listeners in the same ASP.NET Core website? And if so, can you point me to a related resources?
1

There are 1 answers

2
LoekD On

Add a redirect rule to enforce 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);

Service manifest:

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

Communication listeners:

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) =>{[..]}));