Ich habe eine Methode mit zwei benutzerdefinierten ActionFilterAttribute eingerichtet.ASP.NET MVC: Response.Redirect (URL, TRUE) nicht Anforderungsverarbeitung
[RequiresAuthentication(Order = 1)]
[ToonAction(Order = 2)]
public ActionResult Browse(...
RequiresAuthentication
Attribut kommt von this article
Innen RequiresAuthentication, auf es OnActionExecuting ich tun:
filterContext.HttpContext.Response.Redirect(loginUrl, true);
Die Linie wird ausgeführt werden sollen, und die Argumente sind wie erwartet. Das Problem ist, dass nach dem Ausführen der Zeile oben, ich nächste Attribut (ActionFilterAttribute) ausgeführt, als ob redirect nicht funktioniert, es nur weiter die Anfrage ausführt, anstatt einfach Browser umleiten.
Frage: was sonst muss ich tun, um den Request-Handler
Dies ist eine komplette Methode zu machen:
public override void OnActionExecuting(ActionExecutingContext filterContext) {
//redirect if not authenticated
var identity = filterContext.HttpContext.User.Identity;
if (!identity.IsAuthenticated) {
//use the current url for the redirect
string redirectOnSuccess = filterContext.HttpContext.Request.Url.PathAndQuery;
//send them off to the login page
string redirectUrl = string.Format("?ReturnUrl={0}", redirectOnSuccess);
string loginUrl = FormsAuthentication.LoginUrl + redirectUrl;
filterContext.HttpContext.Response.Redirect(loginUrl, true);
// filterContext.Result = new HttpUnauthorizedResult();
// filterContext.HttpContext.Response.StatusCode = 0x191;
}
}
Dies erwies sich als die Antwort, die ich brauchte - ich machte eine Umleitung auf die Antwort, und für Monate konnte ich nicht herausfinden, warum ich "Fehler beim Setzen von Cookies" bekam. –