【问题标题】:OWIN Cookie middleware with web.config authorization elements带有 web.config 授权元素的 OWIN Cookie 中间件
【发布时间】:2016-07-29 18:50:43
【问题描述】:

我已经为 OWIN cookie 中间件 (3.0.1) 的一个奇怪问题苦苦挣扎了好几天。

我用一个最小的代码示例重新创建了这个问题。首先创建一个空的 ASP.NET Web 应用程序项目 (VS.NET 2015)。还有 OWIN nuget 包等......这里还有一个基本的 Startup 类:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.Use(async (context, next) =>
        {
            // context.Response.StatusCode = 200 here
            Debug.WriteLine("1. ==> Request - Before Cookie Auth");
            await next();
            // context.Response.StatusCode = 401 here
            Debug.WriteLine("4. <== Response - After Cookie Auth");
        });

        app.UseCookieAuthentication(new CookieAuthenticationOptions());

        // by adding this element to web.config, the next middle is no longer used, the request ends with 401.2
        // <authorization>
        //    <deny users="?" />
        // </authorization>

        app.Use(async (context, next) =>
        {
            // I thought this middleware would be hit so it could deal with 401 - issue challenge
            // e.g. app.UseOpenIdConnectAuthentication etc...
            Debug.WriteLine("2. ==> After Cookie Auth, Never Hit!! if web.config has <authorization>< deny users = \"?\" /> ");
            await next();
            Debug.WriteLine("3. <== Never hit");
        });
    }
}

代码按预期工作,直到我将跟随元素添加到 web.config

<authorization>
  <deny users="?" />
</authorization>

我的第二个 OWIN 中间件现在不再被调用。在此示例中,它是简单的日志记录语句,但在一个更大的项目中,它是 app.UseOpenIdConnectAuthentication。

我的理解是 401 状态将由其他身份验证中间件处理,进而创建质询响应。

但是在调用 cookie 中间件之后什么都没有。响应只是返回 401.2

本地 IIS 和 IIS Express 都会发生这种情况。

【问题讨论】:

    标签: asp.net cookies owin openid-connect katana


    【解决方案1】:

    在app.UseCookieAuthentication之后添加如下代码:

    app.UseStageMarker(PipelineStage.Authenticate);
    

    这将指示 Owin 在集成管道中运行。

    【讨论】:

      猜你喜欢
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多