如何从ASPXAUTH cookie创建会话号?

时间:2014-12-07 22:01:41

标签: c# asp.net-mvc simplemembership

我想从cookie值中创建一个人类可读的数字作为会话号。 它应该相对于用户ID是唯一的。

我知道如何检索cookie,我会对算法感兴趣,或者想要生成会话号的其他想法吗?

怎么样:

String.Format("{0}{1}", userId, cookieValue.GetHashCode());

1 个答案:

答案 0 :(得分:1)

您可以使用以下命令在控制器中获取SessionID:

Session.SessionID

它将包含实际的SessionID,如下所示:q42eezgj3l5msxpwr5mbvhex

根据msdn,它存储在cookie中:

The SessionID property is used to uniquely identify a browser with session data on the server. The SessionID value is randomly generated by ASP.NET and stored in a non-expiring session cookie in the browser. The SessionID value is then sent in a cookie with each request to the ASP.NET application.

ASPXAUTH cookie用于确定用户是否已登录。如果您需要解密,则应执行以下操作:

HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];//.ASPXAUTH
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);