How to disable RequireHttps MVC.NET version 2 attribute?

I see that version 2 of MVC.NET now has the RequireHttps attribute, which works fine for me. However, what is a good strategy for removing the effect? For example, I want to use Https on some pages, but regular Http for others. Should I create my own RequireHttp attribute?

EDIT: I use my own RequireHttp attribute, and it works fine, but I wonder if there is any built-in functionality in the MVC.NET version 2 version that I am missing.

EDIT 2: I should not have been clear. My question is this: if you use RequireHttps, then any requests after that will exceed Https, even if the controller or action is not decorated with RequireHttps. If I'm not mistaken, you need a second attribute, such as RequireHttp, to redirect requests to Http instead of Https.

+3
source share
4 answers

You can apply the attribute either on the controller, in which case it will be applied to all actions or only to selected actions.

//apply to all actions
[RequireHttps] 
public class SomeController 
{
    //apply to this action only
    [RequireHttps] 
    public ActionResult SomeAction()
    {
    }

}
+1
source

The point ActionFilterAttributeis that you can apply them to any actions you want. Or, in other words, you do not need to apply them to all actions.

, , . :

public class SomeController : Controller {
    [RequireHttps]
    public ActionResult SomeAction() {
        //the attribute logic will be injected to this action.
        return View();
    }

    public ActionResult SomeOtherAction() {
        //this action doesn't require https protocol
        return View();
    }
}

, .

:

HTTP https, , . , MVC 2 . ( ):

public class RequireHttp : ActionFilterAttribute {
   public override void OnActionExecuting(ActionExecutingContext filterContext) {
        if (filterContext.HttpContext.Request.IsSecureConnection) {
            UriBuilder builder = new UriBuilder() {
                Scheme = "http",
                Host = filterContext.HttpContext.Request.Url.Host,
                Path = filterContext.HttpContext.Request.RawUrl
            };

            filterContext.Result = new RedirectResult(builder.ToString());
            filterContext.Result.ExecuteResult(filterContext);
        }
        base.OnActionExecuting(filterContext);
    }
}
+6

!

çağdaş Visual Basic:

Public Class RequireHttpAttribute
    Inherits ActionFilterAttribute

    Public Overrides Sub OnActionExecuting(ByVal filterContext As _
                                           ActionExecutingContext)
        If (filterContext.HttpContext.Request.IsSecureConnection) Then
            Dim builder As UriBuilder = New UriBuilder()
            builder.Scheme = "http"
            builder.Host = filterContext.HttpContext.Request.Url.Host
            builder.Path = filterContext.HttpContext.Request.RawUrl

            filterContext.Result = New RedirectResult(builder.ToString())
            filterContext.Result.ExecuteResult(filterContext)
        End If

        MyBase.OnActionExecuting(filterContext)
    End Sub

End Class

:

<RequireHttp()> _
Public Class SomeController

    <RequireHttp()> _
    Function SomeAction(...) As ActionResult
        ...
    End Function

End Class

Joel Mueller RemoteRequireHttps, :
ASP.NET MVC RequireHttps

+1
source
[RequireHttps] //apply to all actions in controller
public class SomeController
{
    //... or ...
    [RequireHttps] //apply to this action only
    public ActionResult SomeAction()
    {
    }
}

Applying it at the controller level also applies to all action methods; otherwise, apply it to each individual Action method.

0
source

Source: https://habr.com/ru/post/1720977/


All Articles