We are happy to share with you another article from the series of articles on the ASP.NET Core platform (formerly ASP.NET 5). This time Vyacheslav Bobik, a .NET developer from Radario, will continue his story about the platform with a story about the use of OWIN, Katana technologies and related issues. All articles of the series you can always find here #aspnetcolumn - Vladimir Yunev
using AppFunc = Func<IDictionary<string, object>, Task>;
public class Startup { public void Configuration(IAppBuilder app) { app.Use(...); app.Use(...); } }
[assembly: OwinStartup(typeof(App.TestApp))]
public class Startup { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app) { // Add the platform handler to the request pipeline. app.UseIISPlatformHandler(); app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } }
Microsoft.AspNet.Owin" : "1.0.0-beta8"
public Task OwinHello(IDictionary<string, object> enviroment) { var responseText = "Hello via Owin"; var responseBytes = Encoding.UTF8.GetBytes(responseText); var responseStream = (Stream)enviroment["owin.ResponseBody"]; var responseHeaders = (IDictionary<string, string[]>)enviroment["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); }
app.UseOwin(pipeline => pipeline(next => OwinHello));
public class Startup { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app) { // Add the platform handler to the request pipeline. app.UseIISPlatformHandler(); app.UseOwin(pipeline => pipeline(next => OwinHello)); app.Run(async (context) => { await context.Response.WriteAsync("Hello World"); }); } public Task OwinHello(IDictionary<string, object> enviroment) { var responseText = "Hello Owin"; var responseBytes = Encoding.UTF8.GetBytes(responseText); var responseStream = (Stream)enviroment["owin.ResponseBody"]; var responseHeaders = (IDictionary<string, string[]>)enviroment["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); } }
owin.ResponseBody
and owin.ResponseHeaders
. The entire set of keys can also be viewed in the specification . We dealt with the execution of the request, but we have two different methods, for using owin, Run
and Use
, which, at first glance, do the same thing. This is not entirely true.Run
- the agreements are described in such a way that this method should be used only when we want to add our middleware
to the end of the processing request (pipeline), respectively, after Run
nothing will be called.middleware
separate class, and to make calls to your components as an extension method.BasicAuthMiddleware
class with a single Invoke
method. // production public class BasicAuthMiddleware { private readonly RequestDelegate _next; public BasicAuthMiddleware(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext context) { if (context.Request.Headers["Authorization"] == "Basic aGFicmE6aGFicg==") { context.Response.StatusCode = 200; await _next.Invoke(context); } else { context.Response.StatusCode = 401; context.Response.Headers.Add("WWW-Authenticate", "Basic realm=\"localhost\""); } } }
BasicAuthMiddlewareExtension
class public static class BasicAuthMiddlewareExtension { public static IApplicationBuilder UseBasicAuth(this IApplicationBuilder builder) { return builder.UseMiddleware<BasicAuthMiddleware>(); } }
Startup
class: public class Startup { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app) { app.UseIISPlatformHandler(); app.UseBasicAuth(); app.Run(async (context) => { await context.Response.WriteAsync("Hello World"); }); } }
middleware
component.Source: https://habr.com/ru/post/275817/
All Articles