0
我有以下AuthorizeAttribute如何使用域訪問的cookie在AuthorizeAttribute
public class SSOAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var cookie = httpContext.Request.Cookies["testcookie"];
if (cookie == null)
{
Debug.WriteLine("cookie does not exist in attribute");
cookie = new HttpCookie("testcookie")
{
Value = "test",
Domain = "someotherdomain"
};
httpContext.Response.Cookies.Add(cookie);
}
else
Debug.WriteLine("cookie exists in attribute");
return true;
}
}
和下面的控制器(MVC 5.0)
[SSO]
public class HomeController : Controller
{
public ActionResult Index()
{
var cookie = HttpContext.Request.Cookies["testcookie"];
if (cookie == null)
Debug.WriteLine("cookie does not exist in Index");
else
Debug.WriteLine("cookie exists in Index");
return RedirectToAction("Second");
}
public ActionResult Second()
{
var cookie = HttpContext.Request.Cookies["testcookie"];
if (cookie == null)
Debug.WriteLine("cookie does not exist in Second");
else
Debug.WriteLine("cookie exists in Second");
return View("Index");
}
}
當運行該測試應用中,輸出是
Cookie中不存在屬性
的cookie在存在索引
cookie並不在屬性
cookie存在存在於二
無域,一切工作正常。該屬性在第二次通過時找到了預期的cookie。然而,一旦我添加域,該屬性在搜索cookie時返回null。它在控制器中仍然顯示得很好。有人可以解釋爲什麼這是,我怎麼能看到這個屬性中的cookie?
是的,在繼續研究和思考之後,我意識到它就是這樣。當手動創建cookie時,會顯示。但是,只要瀏覽器啓動下一次調用,Cookie就不會因域而被包含。感謝您的迴應。 – falt86