触发OWIN cookie中间件设置为被动身份验证模式的正确方法是什么?

问题描述 投票:0回答:3

我一直在关注OAuth 2.0授权服务器示例代码 http://www.asp.net/aspnet/overview/owin-and-katana/owin-oauth-20-authorization-server

以及查看金块包 Microsoft.aspnet.identity.samples 包 (安装包 Microsoft.aspnet.identity.samples -Pre)

我正在尝试了解被动与主动 Cookie 中间件的工作原理。

在授权服务器示例中,“应用程序”cookie 设置为被动。 在身份示例中,“ApplicationCookie”处于活动状态。

当我读到此属性时,它解释说被动中间件仅在匹配的 AuthenticationType 请求时才会触发。

如果我编辑 Microsoft.aspnet.identity.samples 中的startup.auth.cs 文件并将应用程序cookie 设置为被动,然后登录,它似乎会验证,但不会让我登录。

深入研究代码,我发现帐户控制器归结为对 SignInHelper.SignInAsync 的调用

此方法从用户获取声明身份,该身份是对以下内容的调用:CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie)

我显然不明白一些事情,因为从我读到的内容来看,cookie 具有与声明相同的 AuthenticationType,但是当调用 Authentication.SignIn 时,Cookie 似乎没有设置,我返回到主页上有注册和登录选项。

要重复该问题,请启动一个新项目空asp.net应用程序,然后安装Identity示例包,然后将startup.auth.cs的app.useCookieAuthentication更改为:

app.UseCookieAuthentication(new CookieAuthenticationOptions {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Passive,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });

我尝试更改startup.auth.cs中的cookie名称并将“自定义”名称添加到生成声明的代码中,但无济于事。

我将继续研究,但同时我想我会接触社区。

asp.net cookies oauth-2.0 middleware owin
3个回答
3
投票

我不确定您的具体问题。

OWIN 是一个运行所有注册的中间件模块的管道。您可以注册多种身份验证类型的中间件。

cookie 解密身份。如果您将身份验证类型更改为外部承载,它将成为 cookie 中的承载令牌。

我不确定为什么它不适合你,这就是我使用的。 (我没有看模板的外部登录)

   // Enable the application to use a cookie to store information for the signed in user
  app.UseCookieAuthentication(new CookieAuthenticationOptions
            {   
                //just to show bearer
                AuthenticationType = DefaultAuthenticationTypes.ExternalBearer,
                LoginPath = new PathString("/Account/Login"),
             }

Brock Allen 先生本人对主动与被动的良好解释。

主动与被动身份验证中间件

出现一个问题 - 如果新模板配置了多个 OWIN 身份验证中间件,那么真正使用的是哪一个?嗯,OWIN 身份验证中间件有被动与主动的概念。活动中间件始终查看每个传入请求并尝试对调用进行身份验证,如果成功,它们会创建代表当前用户的主体并将该主体分配给托管环境。另一方面,被动中间件仅在需要时检查请求。对于 Visual Studio 2013 中的默认模板,默认情况下配置的所有中间件都是被动的,除了“主”cookie 身份验证中间件(事实证明,在某些模板中使用了两个 cookie 中间件——主中间件和另一个用于外部身份提供商,另一个被标记为被动)。


1
投票

我来到这里是因为我问了同样的问题。事实证明,没有正确的方法,因为它做不到。身份验证中间件调用“Invoke”方法来测试它是否应该在被动模式下执行任何操作。在诸如 OpenIdConnect 中间件之类的中间件中,此方法被重写,以便它检查请求路径,如果它是 Options.RedirectUri,则中间件会执行某些操作,设置 Response 对象以执行重定向之类的操作并返回“ true”表示请求已被处理,然后中间件堆栈被展开并处理响应。 CookieAuthenticationMiddleware 的 Invoke 方法不执行任何操作,它仅返回“false”,这意味着控制权将传递给链中的下一个中间件。 所以,结果是CookieAuthenticationMiddleware无法用于被动模式下的身份验证。 我发现这篇博文非常有帮助:https://chris.59north.com/post/Understanding-OWIN-(Katana)-Authentication-Middleware。另外,看看 Katana 源代码:https://github.com/aspnet/AspNetKatana


0
投票

当我在下面的代码中发表评论时,页面工作正常。

app.UseCookieAuthentication(new CookieAuthenticationOptions
{

AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,

//LoginPath = new PathString("/Login"),

Provider = new CookieAuthenticationProvider(),

CookieName = "DefaultAuthenticationTypes",

CookieHttpOnly = true,              
ExpireTimeSpan = TimeSpan.FromHours(cookieActiveTime),

});

.

© www.soinside.com 2019 - 2024. All rights reserved.