Edit

Share via


Open Web Interface for .NET (OWIN) with ASP.NET Core

By Steve Smith and Rick Anderson

ASP.NET Core:

  • Supports the Open Web Interface for .NET (OWIN).
  • Has .NET Core compatible replacements for the Microsoft.Owin.* (Katana) libraries.

OWIN allows web apps to be decoupled from web servers. It defines a standard way for middleware to be used in a pipeline to handle requests and associated responses. ASP.NET Core applications and middleware can interoperate with OWIN-based applications, servers, and middleware.

OWIN provides a decoupling layer that allows two frameworks with disparate object models to be used together. The Microsoft.AspNetCore.Owin package provides two adapter implementations:

  • ASP.NET Core to OWIN
  • OWIN to ASP.NET Core

This allows ASP.NET Core to be hosted on top of an OWIN compatible server/host or for other OWIN compatible components to be run on top of ASP.NET Core.

Note

Using these adapters comes with a performance cost. Apps using only ASP.NET Core components shouldn't use the Microsoft.AspNetCore.Owin package or adapters.

View or download sample code (how to download)

Running OWIN middleware in the ASP.NET Core pipeline

ASP.NET Core's OWIN support is deployed as part of the Microsoft.AspNetCore.Owin package. You can import OWIN support into your project by installing this package.

OWIN middleware conforms to the OWIN specification, which requires a Func<IDictionary<string, object>, Task> interface, and specific keys be set (such as owin.ResponseBody). The following simple OWIN middleware displays "Hello World":

public Task OwinHello(IDictionary<string, object> environment) { string responseText = "Hello World via OWIN"; byte[] responseBytes = Encoding.UTF8.GetBytes(responseText); // OWIN Environment Keys: http://owin.org/spec/spec/owin-1.0.0.html var responseStream = (Stream)environment["owin.ResponseBody"]; var responseHeaders = (IDictionary<string, string[]>)environment["owin.ResponseHeaders"]; responseHeaders["Content-Length"] = new string[] { responseBytes.Length.ToString(CultureInfo.InvariantCulture) }; responseHeaders["Content-Type"] = new string[] { "text/plain" }; return responseStream.WriteAsync(responseBytes, 0, responseBytes.Length); } 

The sample signature returns a Task and accepts an IDictionary<string, object> as required by OWIN.

The following code shows how to add the OwinHello middleware (shown above) to the ASP.NET Core pipeline with the UseOwin extension method.

public void Configure(IApplicationBuilder app) { app.UseOwin(pipeline => { pipeline(next => OwinHello); }); } 

You can configure other actions to take place within the OWIN pipeline.

Note

Response headers should only be modified prior to the first write to the response stream.

Note

Multiple calls to UseOwin is discouraged for performance reasons. OWIN components will operate best if grouped together.

app.UseOwin(pipeline => { pipeline(next => { return async environment => { // Do something before. await next(environment); // Do something after. }; }); }); 

OWIN environment

You can construct an OWIN environment using the HttpContext.

 var environment = new OwinEnvironment(HttpContext); var features = new OwinFeatureCollection(environment); 

OWIN keys

OWIN depends on an IDictionary<string,object> object to communicate information throughout an HTTP Request/Response exchange. ASP.NET Core implements the keys listed below. See the primary specification, extensions, and OWIN Key Guidelines and Common Keys.

Request data (OWIN v1.0.0)

KeyValue (type)Description
owin.RequestSchemeString
owin.RequestMethodString
owin.RequestPathBaseString
owin.RequestPathString
owin.RequestQueryStringString
owin.RequestProtocolString
owin.RequestHeadersIDictionary<string,string[]>
owin.RequestBodyStream

Request data (OWIN v1.1.0)

KeyValue (type)Description
owin.RequestIdStringOptional

Response data (OWIN v1.0.0)

KeyValue (type)Description
owin.ResponseStatusCodeintOptional
owin.ResponseReasonPhraseStringOptional
owin.ResponseHeadersIDictionary<string,string[]>
owin.ResponseBodyStream

Other data (OWIN v1.0.0)

KeyValue (type)Description
owin.CallCancelledCancellationToken
owin.VersionString

Common keys

KeyValue (type)Description
ssl.ClientCertificateX509Certificate
ssl.LoadClientCertAsyncFunc<Task>
server.RemoteIpAddressString
server.RemotePortString
server.LocalIpAddressString
server.LocalPortString
server.OnSendingHeadersAction<Action<object>,object>

SendFiles v0.3.0

KeyValue (type)Description
sendfile.SendAsyncSee delegate signaturePer Request

Opaque v0.3.0

KeyValue (type)Description
opaque.VersionString
opaque.UpgradeOpaqueUpgradeSee delegate signature
opaque.StreamStream
opaque.CallCancelledCancellationToken

WebSocket v0.3.0

KeyValue (type)Description
websocket.VersionString
websocket.AcceptWebSocketAcceptSee delegate signature
websocket.AcceptAltNon-spec
websocket.SubProtocolStringSee RFC6455 Section 4.2.2 Step 5.5
websocket.SendAsyncWebSocketSendAsyncSee delegate signature
websocket.ReceiveAsyncWebSocketReceiveAsyncSee delegate signature
websocket.CloseAsyncWebSocketCloseAsyncSee delegate signature
websocket.CallCancelledCancellationToken
websocket.ClientCloseStatusintOptional
websocket.ClientCloseDescriptionStringOptional

Additional resources