Asp.Net MVC3 - FormsAuthentication,如何过期饼干当浏览器关闭? [英] Asp.Net MVC3 - FormsAuthentication, How to expire cookie when browser closing?

查看:808
本文介绍了Asp.Net MVC3 - FormsAuthentication,如何过期饼干当浏览器关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要到期的cookie,对于FormsAuthentication 当浏览器关闭。
(我想它像PHP会议上所做的)

I want to expire cookie that for FormsAuthentication when browser closed. (I want to it works like PHP session does)

下面是我的验证code,它位于型号(不控制器)。

Here is my Auth code that located in Model (Not controller).

型号/ Auth.cs

Models/Auth.cs

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
    1,
    model.UserId,
    DateTime.Now,
    DateTime.Now.AddDays(1),
    true,
    model.UserId +" "+reader["lastname"],
    FormsAuthentication.FormsCookiePath);

string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

if (ticket.IsPersistent)
{
    cookie.Expires = ticket.Expiration;
}

HttpContext.Current.Response.Cookies.Add(cookie);

Web.config文件

Web.config

<authentication mode="Forms">
    <forms name="user" timeout="60" loginUrl="~/Auth/login" path="/"></forms>
</authentication>
<authorization>
    <deny users="?" />
</authorization>

和一个更多的问题是,有2次设置Cookie超时,

And one more questions is, there are 2 times setting cookie timeout,

在机票,

DateTime.Now.AddDays(1),

和在Web.config文件中的认证

and in authentication in Web.config file

<forms name="user" timeout="60" loginUrl="~/Auth/login" path="/"></forms>

他们是如何不同的是,哪些人会影响到实际的过期饼干吗?

how different they are, and which one will effect to actual expire cookie?

有人知道,请告诉我。

感谢您!

推荐答案

在浏览器关闭时不能使cookie失效。你可以,但是,做饼干的非持久性,这意味着它不会保存Cookie,并因此,当你打开一个新的浏览器将有一个新的Cookie(请注意,但是,同程大多数浏览器缓存的非持久性与片饼干,整个浏览器已被关闭该清除出)。

You can't expire the cookie when the browser is closed. You can, however, make the cookie non-persistent, which means it will not save the cookie and thus when you open a new browser it will have a new cookie (be aware, however, that with the way most browsers cache non-persistent cookies with tabs, the entire browser has to be closed for this to clear it out).

至于你的第二个问题,在web.config项,如果你不指定超时使用。

As for your second question, the web.config entry is used if you do not specify a timeout.

这篇关于Asp.Net MVC3 - FormsAuthentication,如何过期饼干当浏览器关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆