WebSecurity.IsAuthenticated是继WebSecurity.Logout之后的true,因为由于cookie的原因,需要在更改属性之前完成响应。由于这个原因,我再次通过javascript重定向。在第二个响应中,通常也是WebSecurity.IsAuthenticated == true。我也不知道原因。在我看来是随机的。有什么办法在注销后得到WebSecurity.IsAuthenticated == false吗?也许是缓存问题。
_Layout.cshtml
<body>
@{Html.RenderAction("Header", "Menu", new { area = "" });}
@RenderBody()
</body>MenuController.cs
[OutputCache(Duration = 1, VaryByParam = "*")]
public class MenuController : Controller
{
[ChildActionOnly]
public ActionResult Header()
{
return PartialView("~/Views/Shared/_Header.cshtml");
}
}Logout.cshtml
<h2>Logout</h2>
<script type="text/javascript">
$(document).ready(function () {
if ('@WebSecurity.IsAuthenticated' == "True") {
window.location.reload(true);
}
});
</script>AccountController.cs
public ActionResult Logout()
{
if (WebSecurity.IsAuthenticated)
{
WebSecurity.Logout();
Session.Abandon();
var url = Url.Action("Logout", new { controller = "Account", area = "SomeArea" });
Response.RemoveOutputCacheItem(url);
}
return View();
}HTTP/1.1 200 OK Cache-Control: public,max-age=1内容-Type: text/html;charset=utf-8内容-编码: gzip过期: Mon,2015年2月16日20:47:56 GMT最后修改: Mon,2015年2月16日20:47:55 GMT更改:接受-编码服务器:Microsoft/8.0 X-AspNetMvc-版本: 4.0 X-AspNet-版本: 4.0.30319 X源代码文件:=?UTF-8?B?RDpcUHJvamVjdHNcU29mdHdhcmVcbXZjLXRlbXBcbXZjLndlYlxWZW5kb3JcQWNjb3VudFxMb2dvdXQ=?= X-通过: ASP.NET访问-控制-允许-来源:*访问-控制-允许-允许-头:X-请求-与原产地,内容类型,接受访问-控制-允许-方法: GET,POST,PUT,DELETE,选项日期: Mon,2015年2月16日20:47:54 GMT内容长度: 1633 HTTP/200响应在默认情况下是可缓存的,除非过期,Pragma或Cache-Control标头出现并禁止缓存。HTTP/1.0到期报头: Mon,2015年2月16日20:47:56格林尼治时间 HTTP/1.1缓存控制头: public,max-age=1 public:此响应可由任何缓存缓存。最大年龄:这个资源将在02分钟内过期.1秒 存在HTTP/1.1可变头:接受--缓存编码必须与服务器联系以验证新鲜度,除非名为头的值与生成缓存条目的请求的值相匹配。 注:对各种变化的支持有限。请参阅http://fiddler2.com/r/?ievary !!警告:不同的响应应该指定一个ETAG来启用条件重新验证请求。 HTTP最后修改的标题是: Mon,2015年2月16日20:47:55格林尼治时间这个响应没有设置任何cookie。此响应不包含P3P头。
用Chrome,Firefox..。
发布于 2015-02-20 17:49:21
检查是否IsAuthenticated并更改可见性状态。
$(document).ready(function () {
$.ajax({
type: "GET",
url: "/SomeArea/Account/IsAuthenticated",
cache: false
})
.done(function (isAuth) {
if (isAuth == "True") {
$(".auth").show();
$(".no-auth").hide();
}
else {
$(".auth").hide();
$(".no-auth").show();
}
});
});发布于 2015-02-16 15:55:39
OutputCache属性缓存页面/响应内容,包括在服务器端计算@WebSecurity.IsAuthenticated的代码块的结果。在MVC视图中,客户端脚本如下所示:
$(document).ready(function () {
if ('@WebSecurity.IsAuthenticated' == "True") {
window.location.reload(true);
}
});当您的缓存响应被发送到浏览器时:(即使在注销时)
$(document).ready(function () {
if ('True' == "True") {
window.location.reload(true);
}
});结论: @WebSecurity.IsAuthenticated仅在服务器上评估一次,然后缓存为响应主体的一部分,用于在OutputCache持续时间内不受任何参数更改的每个后续请求。
解决方案:从控制器方法中删除 [OutputCache(VaryByHeader="Cookie",Duration=1)]或OutputCache属性。
您可以做的另一件事是使用唯一的参数重定向到注销页面:
$(document).ready(function () {
if ('@WebSecurity.IsAuthenticated' == "True") {
window.location = window.location + '&uniqueParameter=' + GenerateRandom();
}
});
function GenerateRandom()
{
...
}以上内容将适用于当前设置为OutputCache的VaryByParam属性。
您对像这样使用输出缓存有什么想法?
https://stackoverflow.com/questions/28419831
复制相似问题