Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session variable equivalent in OWIN self-host

I have a sample web API hosted in an OWIN process (self hosted, not in IIS). I get a JWT token in my controller and I want to be able to retreive it in another part of the application, a class that implements NserviceBus IMutateOutgoingTransportMessages. In my other web application POC (hosted in IIS), I used a simple session variable and it works just fine. But I'd like to know what would be the best way to do it in my new OWIN self hosted environment ? Static property in static class ?

like image 532
Patrice Cote Avatar asked Dec 18 '14 15:12

Patrice Cote


1 Answers

This question is really broad and difficult to answer without detailed knowledge of your specific needs. Here's my interpretation of your issue:

  • You're already signing each request, perhaps storing the token in the browser sessionStorage (or even localStorage), but this does not suffice
  • You need to retrieve the token outside of or not in relation to any request cycle (if not, this is probably where you should be looking for answers)
  • Your application does not need to be stateless

Just one static property for one token in a static class would of course start breaking as soon as more than one request hits the application at the same time. Implementing a class that maintains a list of tokens may be a solution, although I can't tell what key you should use to identify each token. Interface details would vary depending on things like if you need to retrieve the token more than once.

Thread safety issues would apply to all handling and implementation of such a class. Using Immutable Collections and functional programming practices as an inspiration may help.

If lingering tokens poses a problem (and they probably would from a security perspective, if nothing else), you need to figure out how to make sure that tokens do not outstay their welcome, even if the cycle is for some reason not completed.

Seeing how you used Session as a solution in your POC, I'm assuming you want some similar behavior, and that one user should not be allowed to carry two tokens at the same time. You could store the tokens i a database, or even in the local file system, making maintenance and validity a separate issue all together.

There are implementations of cache-like functionality already available for OWIN self-hosted applications, and maybe one of those would serve as a shortcut to implementing everything yourself.

If this token business in fact is the only reason for introducing state in your application, then the best solution IMHO would be to rethink your architecture so that the application can remain stateless.

like image 102
Oskar Lindberg Avatar answered Oct 15 '22 04:10

Oskar Lindberg