Is it possible in ASP.NET MVC (without writing toooo much code) to manually select what the current sessionId is? I don't mean like creating my own sessionId, but rather I want to store the sessionId in a custom header instead of in a cookie or the url. I don't mind if I need to read the header myself and do something like this in request_start:
Request.Session = new SessionState(Request.Headers[开发者_JS百科"sessionId"]);
The main thing is that I don't want to store the session-state in a cookie (since cookies won't be enabled on the device that's supposed to talk to the mvc-application), and I don't want to have to parse the url on the client to then resend everything with the sessionId inserted into the url.
Also, if I can't do this with the native Session-object I don't really mind that either, I just need a fast and easy solution to having a token in a custom header rather than a cookie.
I know that you mention not wanting to parse the url, but using the url with routing appear to be a simple and fast solution, add the parameter to your routes:
In your Global.asax
routes.MapRoute(
"SessionIdUrl",
"{sessionId}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
Create a base controller and make the SessionId available to your controller:
public class BaseController : Controller
{
public string UrlSessionId { get; set; }
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
object sessionId = "";
if (filterContext.RouteData.Values.TryGetValue("sessionId", out sessionId))
{
UrlSessionId = sessionId.ToString();
}
}
}
On your controller inherits from this base controller:
public class HomeController : BaseController
So your controller has access to base.UrlSessionId.
When the user signed in, start using the sessionId in your Url;
[HttpPost]
public ActionResult SignIn(User data)
{
// authenticate
return RedirectToAction("AfterSignedIn", new { sessionId = Session.SessionID, id = "my id" } );
}
On your views, create your link with the SessionId like this:
@Html.ActionLink("My link", "ActionWithUrlSession", "YourController", new { sessionId = Session.SessionID, id = "my id" }, null)
Here you go, you can actually use anything else than Session.SessionID, you might as well be better of with something identifying the current loged in user maybe.
精彩评论