ASP.NET MVC: Wie wird [RequireHttps] auf localhost automatisch deaktiviert?
Ich möchte, dass meine Anmeldeseite nur SSL ist:
[RequireHttps]
public ActionResult Login()
{
if (Helper.LoggedIn)
{
Response.Redirect("/account/stats");
}
return View();
}
Aber offensichtlich funktioniert es nicht auf localhost, wenn ich meine Anwendung entwickle und debugge. Ich möchte IIS 7 nicht mit SSL-Zertifikaten verwenden. Wie kann ich das RequireHttps-Attribut automatisch deaktivieren?
Aktualisiere
asierend auf Informationen, die von StackOverflow-Benutzern und ASP.NET MVC 2-Quellcode bereitgestellt wurden, habe ich die folgende Klasse erstellt, die das Problem lös
public class RequireSSLAttribute : FilterAttribute, IAuthorizationFilter
{
public virtual void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (!filterContext.HttpContext.Request.IsSecureConnection)
{
HandleNonHttpsRequest(filterContext);
}
}
protected virtual void HandleNonHttpsRequest(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.Url.Host.Contains("localhost")) return;
if (!String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("The requested resource can only be accessed via SSL");
}
string url = "https://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
filterContext.Result = new RedirectResult(url);
}
}
Und es wird folgendermaßen verwendet:
[RequireSSL]
public ActionResult Login()
{
if (Helper.LoggedIn)
{
Response.Redirect("/account/stats");
}
return View();
}