views:

53

answers:

1

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
A: 

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.

dhulk
studio says 'Request' is not declared, am I not including something?
DrydenMaker
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.
dhulk