OWIN中间件使用实例

Practical examples of OWIN middleware usage

我认为自己是 OWIN 的初级初学者,在阅读了大量文档之后,我对相互矛盾的概念比开始之前更加困惑。我知道这些是多个问题,但我觉得回答这些问题将消除关于 OWIN 以及如何最好地使用它的最基本的疑虑。这是我的问题:

  1. 我可以使用 OWIN 中间件做什么 消息处理程序或 HTTP 模块?或者他们都是同一件事 除了后两者与 IIS 紧密耦合?
  2. 很多文档说 OWIN 允许在 Web 服务器和 Web 应用程序,即。去除对 IIS 的依赖 用于托管 Web API 应用程序。但我还没有看到 一些使用 OWIN 的 Web 应用程序或 Web api 的示例 成功地从托管在 IIS 和其他一些网站上移植 服务器。那么 IIS 和自我托管是唯一的方法吗 Web 服务器和 Web 应用程序之间的解耦?
  3. 当我搜索 OWIN 中间件示例时,我只有 Katana 和 Helios 是 OWIN 规范的仅有的两个实现。 Katana 几乎完成并且不会超越 revision3 并且 Helios 尚不支持 微软根据一些文章。那么OWIN的未来在哪 那种情况?
  4. 到目前为止我看到的唯一详细的实际用法是 使用 OWIN 使用 OAuth 2 进行身份验证。任何其他此类用法 在中间保持 OWIN 实现?
  5. 在我的启动 class 的配置方法中,我尝试链接简单 中间件代码片段如下,能够看到请求 正在发送:-

但出现错误:

如何查看传入的请求并为中间件中的下一个组件修改它?

  1. 你外挂的各种中间件有哪些 在 Web 服务器和应用程序之间的项目中?

感谢您回答以上任何或所有问题。

What can I use OWIN middleware for that I couldn't already do using message handlers or HTTP modules? Or are they both the same thing except that the latter two are tightly coupled with IIS?

与 IIS 解耦是其中的一部分。 OWIN 中间件是一个管道,如果他们选择,允许某些 "OWIN aware" 的东西参与请求。 IHttpHandler 只处理一件事情——它们不是 chain-able。我更喜欢把流水线比作Global.asax。我见过很多填充 Global.asax 处理程序做各种各样的事情,比如身份验证、授权、吐出 HTTP headers 像 P3P 策略、X-Frame-Options 等。问题的一部分这是开发可重用的组件,这很困难并且依赖于 IIS。 OWIN 试图消除这些问题。

A lot of the documentation says OWIN allows for decoupling between the web server and web application ie. removing dependency on IIS for hosting say Web API applications. But I have yet to see an example of some web application or web api that used OWIN and was successfully ported from being hosted on IIS and then some other web server. So is IIS and self hosting the only way to go for this decoupling between web server and web app?

WebAPI 2 和 SignalR 2 也是如此。MVC 5 及更早版本目前无法真正与 IIS 分离。 MVC 6 将解决这个问题,并且是一次相当大的改革。 ASP.NET 网站在 SignalR 上有一个 tutorial or two 控制台应用程序自托管。您将在本教程中看到 Startup class,就好像它在 IIS 或 IIS Express 上 运行ning 一样。控制台应用程序唯一不同的是它在 Main 方法中使用 HttpListener 引导服务器。

[comment] With respect to point #2 above, what are the owin components here? Is Katana an owin component or is it the code we write using Katana or both put together?

OWIN 实际上不仅仅是一个抽象层,实际上是 Web 应用程序和 Web 服务器之间的规范。 OWIN 有不同的 "implementations",具体取决于您要 运行 的服务器 - Katana 是一个 OWIN 实现,运行s WebAPI 2 和 SignalR 2。Kestrel 是另一个OWIN 实现示例。

When I searched for OWIN middleware examples, I only got Katana and Helios which are the only two implementations of the OWIN spec. Katana is almost done with and wont go beyond revision3 and Helios is not yet supported by Microsoft as per some articles. So what is the future of OWIN in that case?

这仍然有点 up-in-the-air,但是 OWIN 被用于开发 Kestrel Web 服务器,允许 ASP.NET 5 Core 到 运行 on Linux / OS X.

The only detailed practical usage I have seen so far is that of using OWIN for authentication using OAuth 2. Any other such usages of keeping an OWIN implementation in the middle?

SignalR 和 WebAPI 也使用 OWIN。这很有用,因此您可以 运行 SignalR Hub 作为 Windows 服务,同样适用于 Web API。

Any other such usages of keeping an OWIN implementation in the middle?

平台独立。中间有 OWIN 意味着我可以从字面上将我的 MVC 6 核心 Web 应用程序从 IIS 上的 运行ning 复制到我的 Mac 上的 Kestrel,OWIN 实现负责其余部分。

In my startup class's Configuration method I tried to chain simple middleware code snippets as below and to be able to see the request being sent in.

context.Request 在 OWIN 中没有索引器。使用 Get<> 代替:

app.Use(async (context, next) =>
{
    context.Response.Write("hello world 2: " + context.Request.Get<object>("owin.RequestBody"));
    await next();
});

注意 owin.RequestBody 是一个实现细节,实际的 return 类型是内部的。我不确定您要获取什么,如果您想要查询字符串,请使用请求中的 Query,如果您想要 HTTP header.[=31=,请使用 Headers ]

What are the various kinds of middle ware that you have plugged-in in your projects between the web server and application?

处理安全的东西,比如处理内容安全策略中随机数的中间件组件,我在我的个人博客 here 上写过。它的要点是它允许我添加带有随机数的 HTTP header:

public void Configuration(IAppBuilder app)
{
    app.Use((context, next) =>
    {
        var rng = new RNGCryptoServiceProvider();
        var nonceBytes = new byte[16];
        rng.GetBytes(nonceBytes);
        var nonce = Convert.ToBase64String(nonceBytes);
        context.Set("ScriptNonce", nonce);
        context.Response.Headers.Add("Content-Security-Policy",
            new[] {string.Format("script-src 'self' 'nonce-{0}'", nonce)});
        return next();
    });
    //Other configuration...
}

从那里开始,在我的 Razor 视图中,我可以将随机数添加到 <script> 元素从 owin 上下文获取令牌。


它还有很多其他用途。其他框架现在可以很容易地将自己注入到请求/响应过程中。 NancyFx 框架现在可以使用 OWIN。