If you have to do this programmatically, then as I did in the past, this is to check the URL and find "https" in it. Forwarding if you do not see this. However, Request.IsSecureConnection should be preferred. You may need to add additional logic to handle the return address.
source
share