0

In My MVC4 Mobile application i have registration, login page and remaining pages. i would like to redirect user to HTTPS connection for all sensitive information pages like registration and login pages and HTTP to remailing pages.

Ranjith Kumar Nagiri
  • 865
  • 3
  • 18
  • 42

2 Answers2

0

Within the controller actions that you wish to be HTTPS add the following code to the top of the method (of course you can simply add this to its own method and then call it):

        if (!HttpContext.Request.IsSecureConnection)
        {
            var url = new UriBuilder(HttpContext.Request.Url);

            url.Scheme = "https";

            Response.Redirect(url.Uri.AbsoluteUri);
        }

It is recommended though that you keep HTTPS on throughout your site to protect against a MITM attack against the auth cookie.

SilverlightFox
  • 32,436
  • 11
  • 76
  • 145
0

I prefer you to use conditional functionality putting the class

public class RequireHttpsConditional : RequireHttpsAttribute
{
    protected override void HandleNonHttpsRequest(AuthorizationContext filterContext)
    {
        var useSslConfig = ConfigurationManager.AppSettings["UseSSL"];
        if (useSslConfig != null)
        {
            if (!string.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("The requested resource can only be accessed via SSL.");
            }

            var request = filterContext.HttpContext.Request;
            string url = null;
            int sslPort;

            if (Int32.TryParse(useSslConfig, out sslPort) && sslPort > 0)
            {
                url = "https://" + request.Url.Host + request.RawUrl;

                if (sslPort != 443)
                {
                    var builder = new UriBuilder(url) { Port = sslPort };
                    url = builder.Uri.ToString();
                }
            }

            if (sslPort != request.Url.Port)
            {
                filterContext.Result = new RedirectResult(url);
            }
        }
    }
}

and using this [RequireHttpsConditional] above the action result.

i have got this code somewhere in internet and is working fine for me.

in web.config appsettings use <add key="UseSSL" value="443" />

and in the controller above the action result you need put

  [RequireHttpsConditional]
  public ActionResult SignIn()
 {
 }

In IIS where you have your project right click and click "Edit Bindings" then you add a custom type https and port no 443 (you can change it)

Note this will work only in production environment. when executed locally it wont be working.

When you execute it locally you have request.Url.Host which will return you only localhost and missing your port number. so if you use it in MVC you will find error loading page for your pages where you put this code.

So this will work when you have the host assigned instead of using the localhost with a specific port number.

Shiva Saurabh
  • 1,281
  • 2
  • 25
  • 47