Mam metodę ozdobioną dwiema niestandardowymi ActionFilterAttribute.ASP.NET MVC: Response.Redirect (url, TRUE) nie zatrzymuje przetwarzania żądań
[RequiresAuthentication(Order = 1)]
[ToonAction(Order = 2)]
public ActionResult Browse(...
RequiresAuthentication
atrybut pochodzi this article
Wewnątrz RequiresAuthentication, na jego OnActionExecuting zrobić:
filterContext.HttpContext.Response.Redirect(loginUrl, true);
Linia jest zostanie wykonany, a argumenty są, jak oczekiwano. Problem polega na tym, że po wykonaniu powyższej linii otrzymuję następny atrybut (ActionFilterAttribute), tak jakby przekierowanie nie działało, po prostu kontynuuje wykonywanie żądania, zamiast po prostu przekierować przeglądarkę.
Pytanie: co jeszcze muszę zrobić, aby procedura obsługi życzenie
Jest to kompletna metoda:
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;
}
}
To była odpowiedź, której potrzebowałem - robiłem przekierowanie na odpowiedź i przez miesiące nie mogłem się domyślić, dlaczego otrzymuję błędy "Nie mogę ustawić cookie". –