Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC Action Filter Redirect Loop
    primarykey
    data
    text
    <p>I am seeing a redirect loop from our production servers when we enable the following attribute on a controller class:</p> <pre><code>public class TestRedirectHttpsAttribute : RedirectHttpsAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { if (Convert.ToBoolean(ConfigurationManager.AppSettings["TestSecureEnabled"] ?? bool.FalseString)) { base.OnAuthorization(filterContext); } } } public class RedirectHttpsAttribute : FilterAttribute, IAuthorizationFilter { public virtual void OnAuthorization(AuthorizationContext filterContext) { if (filterContext == null) { throw new ArgumentNullException("filterContext"); } if (!filterContext.HttpContext.Request.IsSecureConnection) { UriBuilder uriBuilder = new UriBuilder(filterContext.HttpContext.Request.Url); uriBuilder.Scheme = "https"; uriBuilder.Port = -1; // Don't inject port filterContext.Result = new RedirectResult(uriBuilder.ToString()); } } } </code></pre> <p>I know this is similar to the question - <a href="https://stackoverflow.com/questions/2323544/redirect-loop-with-ssl-action-filter-in-asp-net-mvc">Redirect loop with SSL action filter in ASP.NET MVC</a> but it was asked in February and I was hoping someone might have a better solution. The solution for the question in the link was to redirect from an action in the controller but if we want to redirect all actions to HTTPS we would have to make sure it was called in each rather than applying a custom FilterAttribute to the controller.</p> <p>Note: This code works from our development environments using dummy SSL certs.</p> <p>I may have got the wrong end of the stick and should be doing something else entirely, if so please accept my apologies.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload