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

1 Answer 1

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.

2
  • studio says 'Request' is not declared, am I not including something? Sep 8, 2009 at 19:43
  • I added a code sample, not fully fleshed out, and in C# which I see is different from what you're using, but it will hopefully be of some use. Sep 8, 2009 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.