我创建了一个授权过滤器
public class LandedAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext) {
if (httpContext == null)
throw new ArgumentNullException("httpContext");
if (HttpContext.Current.Response.Cookies["LegalDisclaimer"].Value != "Accepted")
{
return false;
}
return true;
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext){
string url = filterContext.HttpContext.Request.Url.ToString();
System.Diagnostics.Debug.WriteLine(url);
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary {
{ "action", "Index" },
{ "controller", "Landing" },
{ "returnUrl", url }
});
}
}
和我的着陆控制器
public ActionResult Index(string returnUrl)
{
ViewBag.rdParm = returnUrl;
return View();
}
public ActionResult Accept(string returnUrl)
{
HttpCookie cookie = new HttpCookie("LegalDisclaimer", "Accepted");
Response.Cookies.Add(cookie);
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
然后我设置了一个控制器
[Landed]
public class someController : Controller
{
contoller actions here
}
问题是 LegalDisclaimer cookie 从未设置并且总是返回 null。这是我第一次尝试在 mvc 中学习 Authorize 过滤器,并且 3 天没有任何进展。谁能帮我调试一下吗?