Jorge Ramon

  • Home
  • New Here?
  • Articles
    • All
    • Sencha Touch
    • jQuery Mobile
    • ExtJS
  • Books
  • About

Forcing HTTPS in ASP.NET MVC Application

January 12, 2017 1 Comment

I just finished this ASP.NET MVC web application that required https for all requests. Here’s the approach I took to force https in the app.

Using the BeginRequest Method to Force Https

In the global.ascx.cs file (I used C# as the programming language), I decided to intercept all requests with the following Application_BeginRequest method:

protected void Application_BeginRequest()
{
    if (!Context.Request.IsSecureConnection)
    {
        // This is an insecure connection, so redirect to the secure version
        UriBuilder uri = new UriBuilder(Context.Request.Url);                
        if (!uri.Host.Equals("localhost"))
        {
            uri.Port = 443;
            uri.Scheme = "https";
            Response.Redirect(uri.ToString());
        }
    }
}

In the method, I use the Request.IsSecureConnection property to check if the connection isn’t already using https. If the request isn’t over a secure connection and I’m not running the app on my development workstation, I instantiate an Uri object off the request’s Url and change the value of the port property to 443, the https port used by my production server. I also switch the Uri’s scheme to https. Finally, I redirect the request to the updated Uri.

That’s it.

Alternative Approaches

There are other approaches you can take to force https in ASP.NET MVC. For example, you can decorate the controller methods that require https with the RequireHttpsAttribute attribute, which will force https connections for those methods, even in your development environment. You can also derive from the RequireHttpsAttribute Class and create an attribute that will force https for remote connections only:

public class RequireHttpsForRemoteRequestAttribute : RequireHttpsAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext == null)
        {
            throw new ArgumentNullException("filterContext");
        }

        if (filterContext.HttpContext.Request.IsLocal)
        {
            // Don't require HTTPS for local requests.
            return;
        }
        base.OnAuthorization(filterContext);
    }
}

Over to You

Your turn now. Do you use https in your ASP.NET MVC apps? How to you set it up? Leave a comment with your thoughts below.

Tagged With: Web Application Tutorial 1 Comment

Meeting Room Scheduler Project, Update 1

October 1, 2016 Leave a Comment

Here’s a quick update on the meeting room scheduler proof of concept I introduced a few weeks ago.

I’ve been busy working on the form that will allow users to book a meeting room. This is the mockup I showed you in the first post:

Meeting Room Scheduler

And this is how the screen looks now:

conf-room-5

Continue Reading →

Tagged With: Web Application Tutorial Leave a Comment

A Web Page Progress Indicator with Bootstrap and Font Awesome

August 2, 2016 2 Comments

In this article, I will describe a quick method I use in my web applications to create a progress indicator using Bootstrap and Font Awesome. I like this method because it doesn’t require adding large amounts of html and JavaScript code to the application.

A progress indicator gives users feedback about what’s happening in the application. You can use it to increase the perceived performance of the app, especially during long-running operations. My indicator will render as a rectangle with an animated spinner and a text message:

loading-indicator-3

I can control the spinner type, text, colors and other visual attributes using CSS. I use Font Awesome for the spinner and some styles from Bootstrap for the other elements of the indicator.

Continue Reading →

Tagged With: Web Application Tutorial 2 Comments

Free Mobile User Interface Recipes Kit

Free Mobile User Interface Recipes Kit

Sign up to receive my posts via email and get a FREE Mobile User Interface Recipes Kit with mockups packages for three real-world mobile apps. You will save hours of design time when you use the kit to build your own apps.



I will never share your email address.

Get My Books

The beginner's guide to Sencha Touch apps
The beginner's guide to jQuery Mobile apps

Book: How to Build a jQuery Mobile Application

Topics

  • » jQuery Mobile Tutorials
  • » Sencha Touch Tutorials
  • » ExtJS Tutorials
  • » Books
  • » Tools
  • » .Net Tutorials
  • » BlackBerry Tutorials
  • » iOS Tutorials
  • » Node.js Tutorials
  • » Android Training
  • » BlackBerry Training
  • » iOS Training

Search

Contact Me

  •  Email: jorge[AT]jorgeramon.me
  •  Twitter: @MiamiCoder
  •  LinkedIn: Jorge Ramon


Don’t Miss the Free Updates

Receive free updates in your inbox.
Your address is safe with me.

Copyright © 2019 Jorge Ramon · The opinions expressed herein do not represent the views of my employers in any way · Log in