0

This is simular to this question:

But I want to do it within the MVC where the "Request" object isn't directly accessable. Is there a method decorator to specify actions that require SSL? How do I get the equivalent of the answer in the mentioned answer within the MVC?

If Not Request.IsLocal AndAlso Not Request.IsSecureConnection Then
    Dim ub As var = New UriBuilder(Request.Url)
    ub.Scheme = Uri.UriSchemeHttps
    ub.Port = -1
    ' use default port for scheme
    Response.Redirect(ub.Uri.ToString(), True)
    Return
End If
Community
  • 1
  • 1
DrydenMaker
  • 354
  • 2
  • 17

1 Answers1

0

What if you created an custom attribute filter and attach it to the actions that require SSL? I would think if you did that the code you've written above would suffice as a solution to the problem.

EDIT: Here's a small bit of sample code. Sorry it isn't in VB, but I'm not really well versed in it, especially when it comes to MVC stuff.

public class SSLFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        string requestUrl = filterContext.HttpContext.Request.Url;

        //If requestUrl not using SSL
            string newUrl = "URL using SSL";
            filterContext.Result = new RedirectResult(newUrl);
        //End If
    }
}

Then you just add the attribute to actions that need it and you should be all taken care of.

Blair Scott
  • 1,875
  • 2
  • 19
  • 29