0

i need a help, I'm creating an application in asp.net MVC3 and i want to set my custom session id which is generated

var str = Math.floor(Math.random() * (2000 - 1000 + 1)) + 1000;

 document.write("Session ID::" + str);

that above code written in java script function and now i want to set that number in my session id in ASP.NET MVC3.

vijai shukla
  • 195
  • 2
  • 2
  • 7

2 Answers2

1

A quick google search shows this blog entry which explains how to overwrite the session id in asp.net:

http://weblogs.asp.net/anasghanem/archive/2008/12/16/programmatically-changing-the-session-id.aspx

In short: Use the class SessionIDManager and call the method SaveSessionId to set your own session id:

SessionIDManager manager = new SessionIDManager();
string mySessionId = ...calculateYourOwnSessionId...;
bool redirected = false;
bool IsAdded = false;
manager.SaveSessionID(Context, mySessionId, out redirected, out IsAdded);
Jan
  • 15,802
  • 5
  • 35
  • 59
0

This should be avoided at all costs as your custom session value is not generated with a cryptographically secure random number generator and you are going to introduce vulnerabilities in your application around session management.

If you need to get the current session identifier, you can do it one of two ways:

// 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;

Read more about it here.

Casey
  • 12,070
  • 18
  • 71
  • 107