Elsinore

User Forum

www.screenconnect.com
Welcome Guest Search | Active Topics | Log In | Register

Tag as favorite
HTTP redirect to HTTPS
hahnium
#1 Posted : Thursday, November 17, 2011 11:29:20 AM
Rank: Member
Joined: 11/5/2010
Posts: 18
Location: Norway
Hey!

Have a question. I'm trying to redirect HTTP to HTTPS. Now my site uses HTTPS, and can only be accessed by HTTPS://screenconnect. I want my users to be able to access the site by using http, and then redirected to https

Is this doable by editing web.config?

Best regards

Hahnium
bigdessert
#2 Posted : Thursday, November 17, 2011 1:50:08 PM
Rank: Advanced Member
Joined: 9/14/2010
Posts: 458
Location: Minnesota
It is doable to have the web.config use two ports for the web console say 80, 443, but it is not doable to redirect within web.config, this would have to be handled by IIS.

Now in my opinion there is absolutely no reason to use https on the guest page. The only reason you would want https is to protect host login page. So what I would do is use http for guest and direct your techs to use https.
Jake Morgan
#3 Posted : Wednesday, November 23, 2011 7:02:50 PM
Rank: Administration
Joined: 4/9/2010
Posts: 860
You'll need to configure your site for HTTPs. Then add a new key to handle the requests coming in via normal HTTP:

Code:
        <add key="SmtpUseClient" value="false" />
        <add key="WebServerListenUri" value="https://+:443/" />
        <add key="WebServerAlternateListenUri" value="http://+:80/" />
        <add key="RelayListenUri" value="relay://0.0.0.0:8041/" />


Then add a subdirectory called "App_Code" under your ScreenConnect install, and put this file, HttpsRedirectModule.cs:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class HttpsRedirectModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += delegate
        {
            if (!context.Context.Request.IsSecureConnection)
            {
                var newUri = new UriBuilder(Uri.UriSchemeHttps, context.Request.Url.Host, -1, context.Request.Url.AbsolutePath, context.Request.Url.Query);
                context.Response.Redirect(newUri.ToString(), true);
            }
        };
    }

    public void Dispose()
    {
    }
}


Finally put an entry in your web.config to make this module active:

Code:

            <add name="CompressionModule" type="Elsinore.ScreenConnect.CompressionModule, Elsinore.ScreenConnect.Web" />
            <add name="HttpsRedirectModule" type="HttpsRedirectModule" />
        </httpModules>

File Attachment(s):
HttpsRedirectModule.cs (1kb) downloaded 11 time(s).
bigdessert
#4 Posted : Wednesday, November 23, 2011 9:21:55 PM
Rank: Advanced Member
Joined: 9/14/2010
Posts: 458
Location: Minnesota
Thanks Jake, good to know this can be done right in ScreenConnect
bigdessert
#5 Posted : Thursday, January 26, 2012 4:11:51 PM
Rank: Advanced Member
Joined: 9/14/2010
Posts: 458
Location: Minnesota
Jake Morgan wrote:
You'll need to configure your site for HTTPs. Then add a new key to handle the requests coming in via normal HTTP:

Code:
        <add key="SmtpUseClient" value="false" />
        <add key="WebServerListenUri" value="https://+:443/" />
        <add key="WebServerAlternateListenUri" value="http://+:80/" />
        <add key="RelayListenUri" value="relay://0.0.0.0:8041/" />


Then add a subdirectory called "App_Code" under your ScreenConnect install, and put this file, HttpsRedirectModule.cs:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class HttpsRedirectModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += delegate
        {
            if (!context.Context.Request.IsSecureConnection)
            {
                var newUri = new UriBuilder(Uri.UriSchemeHttps, context.Request.Url.Host, -1, context.Request.Url.AbsolutePath, context.Request.Url.Query);
                context.Response.Redirect(newUri.ToString(), true);
            }
        };
    }

    public void Dispose()
    {
    }
}


Finally put an entry in your web.config to make this module active:

Code:

            <add name="CompressionModule" type="Elsinore.ScreenConnect.CompressionModule, Elsinore.ScreenConnect.Web" />
            <add name="HttpsRedirectModule" type="HttpsRedirectModule" />
        </httpModules>



Can this code be modified so that everything but the guest page gets forwarded to HTTPS/SSL? It would be nice to just encrypt the login and host data and leave the guest page on 80.
Jake Morgan
#6 Posted : Thursday, January 26, 2012 4:53:49 PM
Rank: Administration
Joined: 4/9/2010
Posts: 860
This should be a bit more flexible:

Code:
using System;
using System.Web;
using System.Configuration;
using System.Text.RegularExpressions;

public class BaseUrlRedirectionModule : IHttpModule
{
    public void Init(HttpApplication application)
    {
        application.BeginRequest += delegate
        {
            var redirectFromBaseUrl = ConfigurationManager.AppSettings["RedirectFromBaseUrl"];

            if (!string.IsNullOrEmpty(redirectFromBaseUrl))
            {
                var pattern = '^' + Regex.Escape(redirectFromBaseUrl).Replace("\\*", ".*").Replace("\\?", ".");
                var oldUrl = application.Context.Request.Url.AbsoluteUri;
                var match = Regex.Match(oldUrl, pattern, RegexOptions.IgnoreCase);

                if (match.Success)
                {
                    var newUrl = ConfigurationManager.AppSettings["RedirectToBaseUrl"] + oldUrl.Substring(match.Length);

                    if (!string.Equals(newUrl, oldUrl, StringComparison.InvariantCultureIgnoreCase))
                        application.Context.Response.Redirect(newUrl);
                }
            }
        };
    }

    public void Dispose() { }
}


So you define a base URL that you redirect _from_ ... this can contain wildcards. But it really should include enough of the trailing path to "match" correctly. Meaning "http://localhost*" is not good, but "http://local*/" is good. Notice the trailing slash. Without it we don't really know where the base URL ends.

Then you define a URL to redirect _to_. This doesn't contain wildcards. The portion that was "matched" from the redirect _from_ will be replaced by this value.

So for example these are the appSettings I added:

Code:
  <add key="RedirectFromBaseUrl" value="http://*/" />
  <add key="RedirectToBaseUrl" value="http://munich.elsitech.local:8040/" />


You could add something like this:

Code:
  <add key="RedirectFromBaseUrl" value="http://*/" />
  <add key="RedirectToBaseUrl" value="https://ssl.mysecuresite.com:8443/" />


And this can be applied per page. So rather than putting in your main <appSettings>, you can define a location in your web.config:

Code:
<configuration>
    <location path="Host.aspx">
        <appSettings>
            <add key="RedirectFromBaseUrl" value="http://*:8040/" />
            <add key="RedirectToBaseUrl" value="http://munich.elsitech.local:8040/" />
        </appSettings>
    </location>
    <system.web>
File Attachment(s):
BaseUrlRedirectionModule.cs (1kb) downloaded 3 time(s).
bigdessert
#7 Posted : Thursday, January 26, 2012 5:17:08 PM
Rank: Advanced Member
Joined: 9/14/2010
Posts: 458
Location: Minnesota
With a bit of trial and error I have this working now.

I saved the first code as BaseUrlRedirectionModule.cs

Also still had to add

Code:
<add name="BaseUrlRedirectionModule" type="BaseUrlRedirectionModule" />


HiTech
#8 Posted : Wednesday, April 18, 2012 8:11:23 AM
Rank: Newbie
Joined: 4/18/2012
Posts: 2
Location: Spokane, WA
This looks like something we are looking for. Basically, when one clicks on Login it redirects to https
Does anyone know if the contents of this thread would work for that and if it still is compatible with the latest release?
Users browsing this topic
Guest
Tag as favorite
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.