[英]Generate it's Own Session Id in ASP.NET MVC#
我需要幫助,我正在asp.net MVC3中創建一個應用程序,我想設置生成的自定義會話ID
var str = Math.floor(Math.random() * (2000 - 1000 + 1)) + 1000;
document.write("Session ID::" + str);
上面用Java腳本函數編寫的代碼,現在我想在ASP.NET MVC3的會話ID中設置該數字。
快速的Google搜索顯示此博客條目,該博客條目說明了如何覆蓋asp.net中的會話ID:
http://weblogs.asp.net/anasghanem/archive/2008/12/16/programmatically-changing-the-session-id.aspx
簡而言之:使用SessionIDManager類並調用方法SaveSessionId來設置您自己的會話ID:
SessionIDManager manager = new SessionIDManager();
string mySessionId = ...calculateYourOwnSessionId...;
bool redirected = false;
bool IsAdded = false;
manager.SaveSessionID(Context, mySessionId, out redirected, out IsAdded);
應不惜一切代價避免這種情況,因為您的自定義會話值不是使用加密安全的隨機數生成器生成的,並且您將在會話管理中在應用程序中引入漏洞。
如果需要獲取當前會話標識符,則可以使用以下兩種方法之一:
// If in a page or user control:
string sessionId = this.Session.SessionID;
// In ASP.NET in a normal class:
string sessionId = System.Web.HttpContext.Current.Session.SessionID;
在此處了解更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.