Fred
Fred

Reputation: 12826

Can I access the OWIN environment dictionary?

Can I access the OWIN environment dictionary in ASP.NET Core?

I would like to access it from the Startup.cs file.

The reason I want to access it is because I want to pass the OWIN environment dictionary to my middleware or access it from within my middleware.

Upvotes: 3

Views: 4224

Answers (2)

Fred
Fred

Reputation: 12826

Yes, you can!

First install the Microsoft.AspNetCore.Owin package.

Then from anywhere you got an instance of HttpContext you can create a instance of the OwinEnvironment class with the HttpContext as parameter to the constructor.

var environment = new OwinEnvironment(HttpContext);

This doesn't have any indexer so you cant do environment["owin.RequestProtocol"], but you can do:

var requestProtocol = environment.Single(x => x.Key == Owin.RequestProtocol");

However, you can make it into a dictionary with .ToDictionary by doing:

var dictionary = environment.ToDictionary(x => x.Key, x => x.Value);

Now it has a indexer and you refer to keys like environment["owin.RequestProtocol"] to get the value.

There is also the OwinFeatureCollection class that you can feed the environment to to get out a dictionary.

var environment = new OwinEnvironment(HttpContext);
var features = new OwinFeatureCollection(environment);
var requestProtocol = features["owin.RequestProtocol"];

However, there is no OWinContext for strongly-typed accessors anymore in Microsoft.AspNetCore.Owin, that is in the Microsoft.Owin namespace in a different package.

Upvotes: 7

Fred
Fred

Reputation: 12826

I have come to believe that the OWIN environment is only available by installing the package Microsoft.AspNetCore.Owin which exposes the UseOwin extension method to the IApplicationBuilder class.

Then it is available only in Startup.cs from the Configure method as a parameter passed to your method by app.UseOwin.

I could not find the OWIN environment dictionary anywhere in Controller, ControllerBase, HttpContext, or ControllerContext. HttpContext has a Items dictionary though.

You could manually build an own OWIN environment yourself though using the data you find in HttpContext. But only from inside app.UseOwin.

https://docs.asp.net/en/latest/fundamentals/owin.html#owin-keys

A stock OWIN environment in ASP.NET Core with the additional AspNetCore.Owin package looks like the following:

[owin.RequestProtocol, HTTP/1.1]
[owin.RequestScheme, http]
[owin.RequestMethod, GET]
[owin.RequestPathBase, ]
[owin.RequestPath, /]
[owin.RequestQueryString, ]
[owin.RequestHeaders, Microsoft.AspNetCore.Owin.DictionaryStringArrayWrapper]
[owin.RequestBody, Microsoft.AspNetCore.Server.Kestrel.Internal.Http.FrameRequestStream]
[owin.RequestUser, ]
[owin.ResponseStatusCode, 200]
[owin.ResponseReasonPhrase, ]
[owin.ResponseHeaders, Microsoft.AspNetCore.Owin.DictionaryStringArrayWrapper]
[owin.ResponseBody, Microsoft.VisualStudio.Web.BrowserLink.Runtime.ScriptInjectionFilterStream]
[server.OnSendingHeaders, System.Action`2[System.Action`1[System.Object],System.Object]]
[server.ConnectionId, 0HKV72R0U3SDL]
[server.LocalPort, 27417]
[server.RemotePort, 56374]
[server.LocalIpAddress, 127.0.0.1]
[server.RemoteIpAddress, ::1]
[server.User, ]
[owin.CallCancelled, System.Threading.CancellationToken]
[owin.Version, 1.0]
[Microsoft.AspNetCore.Http.HttpContext, Microsoft.AspNetCore.Http.DefaultHttpContext]

Points:

  • OWIN is not part of ASP.NET Core.
  • ASP.NET Core MVC is not built atop of OWIN.
  • Support for OWIN can be added to ASP.NET Core using the AspNetCore.Owin package.

Upvotes: 2

Related Questions