Showing posts with label https. Show all posts
Showing posts with label https. Show all posts

Sunday, September 17, 2017

URL rewrite for https and www in ASPNET Core (Solved)

If you want to url rewrite to both https and www using ASP.NET Core 2.0, here is some pretty straight-forward C# you can add to your Startup.cs.

Woo-hoo!

// ...

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        // ...
    }
    else
    {
        // url rewrite; do this before UseStaticFiles
        var options = new RewriteOptions().Add(
            new RedirectRules(rewriteHttps: true, rewriteWww: true));
        app.UseRewriter(options);
    }

    // ...
}

// ...

public class RedirectRules : Microsoft.AspNetCore.Rewrite.IRule
{
    private readonly bool _rewriteHttps;
    private readonly bool _rewriteWww;

    public RedirectRules(bool rewriteHttps, bool rewriteWww)
    {
        _rewriteHttps = rewriteHttps;
        _rewriteWww = rewriteWww;
    }

    public void ApplyRule(RewriteContext context)
    {
        //
        // redirect two ways
        //    - http to https (if enabled)
        //    - non-www host to www host (if enabled)
        //

        const string hostRoot = "mydomain.com";
        var request = context.HttpContext.Request;
        var currentHost = request.Host;
        HostString newHost;

        // do we need to rewrite from http to https
        var isNewProtocol = _rewriteHttps && request.Scheme.ToLower() == "http";

        // do we need to rewrite from non-www host to www host
        if (_rewriteWww && currentHost.Host == hostRoot)
            newHost = new HostString($"www.{hostRoot}", currentHost.Port ?? 80);

        // if either rewrite done, create a new url
        if (isNewProtocol || newHost.HasValue)
        {
            var newUrl = new StringBuilder()
                .Append(isNewProtocol ? "https" : request.Scheme)
                .Append("://")
                .Append(newHost.HasValue ? newHost : currentHost)
                .Append(request.PathBase)
                .Append(request.Path)
                .Append(request.QueryString);
            context.HttpContext.Response.Redirect(newUrl.ToString(), permanent: true);
            context.Result = RuleResult.EndResponse;
        }
    }
}

Saturday, August 7, 2010

Using HTTPS for WCF in Windows Service

These are instructions on how to use HTTPS in a Windows Service application hosting a WCF web service. The steps are a consolidation of information provided by Microsoft at:
http://msdn.microsoft.com/en-us/library/ms733791.aspx

I've tried to keep this as simple as possible.
  1. Set the endpoint of your application to HTTPS (i.e. your app.config file). Note that if you are going to use a root certificate that is for your COMPUTER, your endpoint will need to match your computer name…not “localhost”. Example:
    https://mycomputername:9010/mywebservice
  2. Verify HTTPS is not already configured by viewing ports configured for HTTPS (admin privs required):
    C:> netsh http show sslcert
  3. Generate an application guid; any guid will do. This can be done from a computer with Visual Studio tools installed. Example:
    C:> uuidgen
    429d0213-340b-44db-991e-1c0c1ed3d91f
  4. Find the thumbprint value for a machine/computer certificate in Personal store on local machine. Remove the spaces from the thumbprint. Example:
    b41dad508c2025eabe10f7d88b2c9a66983f950d
  5. Register the certificate for whatever port you want (any IP address=0.0.0.0), using the thumbprint and guid (admin privs):
    C:> netsh http add sslcert ipport=0.0.0.0:9010 certhash=b51dad508c2025eabe10f7d88b2c9a66983f950d appid={429d0213-340b-44db-991e-1c0c1ed3d91f}
  6. Verify the port is configured for HTTPS (admin privs):
    C:> netsh http show sslcert
  7. To remove HTTPS for a port use the following:
    C:> Netsh http delete sslcert ipport=0.0.0.0:9010

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...