C# : OWIN – Environment Elements and Another Way of Information Passing

Earlier this week I ended up in a situation where, a signal flag wanted to be set in a controller method which will eventually invoke the RedirectToIdentityProvider notification where the signal flag value has to be checked and retrieved. Usually the RedirectToIdentityProvider notification is getting called when SignIn(), SignOut() or Challange() methods are getting called.

I was trying different mechanisms to solve the problem and finally found OWIN Environment is really handy to solve this. So in the place where I wanted to set the flag,

HttpContext.GetOwinContext().Environment.Add("flag", "true");

later which was called inside RedirectToIdentityProvider notification Func as below

object obj = null;
if (context.OwinContext.Environment.TryGetValue("flag", out obj))
{
string flag = obj as string;
// operations below
}

One use-case in this approach is that its really easy to late-bind Authentication related properties without hassle.

 

Happy coding.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s