【问题标题】:Is Cookie middleware required to glue OAuth2 and OpenIdConnect?粘合 OAuth2 和 OpenIdConnect 是否需要 Cookie 中间件?
【发布时间】:2018-07-31 07:54:10
【问题描述】:

我们的应用程序包括:

  • 前端,这是一个使用 OAuth2 在我们的后端进行身份验证的 SPA
  • 后端,它是一个 ASP.NET OWIN 应用程序,充当 OAuth2 服务器和 OpenID 连接客户端
  • 第三方 OpenID 连接提供商

因此,当未经身份验证的用户浏览到 SPA 时,SPA 会使用我们的后端启动 OAuth2 隐式流,这会导致重定向到显示登录表单的 OpenID 连接提供程序。用户登录后,JWT 令牌会发布到我们后端的 OIDC 回调端点,这会触发将身份存储在 cookie 中的 cookie 中间件,并重定向到 OAuth2 授权端点,该端点通过返回访问令牌恢复隐式流程在 URL 片段中。

为此,我目前使用 OWIN Cookie 中间件将 OpenID Connect 中间件与 OAuth2 中间件粘合在一起,其中 cookie 过期时间非常短,因为唯一的目的是将身份从 OpenID 连接回调传递给 OAuth2 授权端点。因此,我想问是否有办法做同样的事情,但没有 cookie 中间件,因为在这个设置中感觉有点多余。

不管怎样,这是我们的中间件配置:

// Clear default inbound claim mappings, otherwise the 'sub' claim is mapped to a ClaimTypes.NameIdentifier which is not what we want
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions
{
    AuthenticationMode = AuthenticationMode.Active,
    AllowedAudiences = new[] { options.JwtOptions.Audience },
    IssuerSecurityKeyProviders = new[] { new X509CertificateSecurityKeyProvider(options.JwtOptions.Issuer, options.JwtOptions.SigningCertificate) }
});

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationMode = AuthenticationMode.Passive,
    CookieName = "Brownie",
    CookieHttpOnly = true,
    CookieSecure = CookieSecureOption.Always,
    ExpireTimeSpan = TimeSpan.FromMinutes(1),
    SlidingExpiration = false
});

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
    AuthenticationMode = AuthenticationMode.Active,
    AuthenticationType = Constants.OpenIdConnectAuthenticationType,
    MetadataAddress = options.OpenIdConnectOptions.MetadataAddress,
    ClientId = options.OpenIdConnectOptions.ClientId,
    ClientSecret = options.OpenIdConnectOptions.ClientSecret,
    Notifications = new OpenIdConnectNotificationHandlers(options),
    RedirectUri = new Uri(options.ApplicationUrl, options.OpenIdConnectOptions.CallbackPath.ToString()).ToString(),
    ResponseType = "code id_token",
    Scope = "openid profile offline_access",
    SignInAsAuthenticationType = CookieAuthenticationDefaults.AuthenticationType
});

app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
{
    AuthenticationMode = AuthenticationMode.Active,
    AuthenticationType = Constants.OAuth2AuthenticationType,
    AuthorizeEndpointPath = options.AuthorizeEndpoint,
    TokenEndpointPath = options.TokenEndpoint,
    AllowInsecureHttp = false,
    AccessTokenExpireTimeSpan = options.JwtOptions.TokenLifetime,
    AccessTokenFormat = new JwtTokenFormat(options.JwtOptions),
    Provider = new OAuth2AuthorizationServerProvider(context => context.GetAutofacLifetimeScope().Resolve<IClientRepository>())
});

将 OpenID Connect 中间件的 Constants.OAuth2AuthenticationType 传递给 SignInAsAuthenticationType 不起作用。

【问题讨论】:

  • 您在这里找到解决方案了吗?我的情况有点不同但足够相似;我正在为 OAuth/OpenID 配置单独配置和使用 cookie 身份验证,并且不希望它们发生冲突(AddAuthorization().AddCookie 等)

标签: c# oauth-2.0 owin openid-connect


【解决方案1】:

一般来说,这是最佳实践,如OpenID Connect Basic Client Implementer's Guide 1.0 - draft 37 Section 2.1.1.1. Request Parameters 中所述使用“状态”参数

“推荐。用于维护请求和回调之间状态的不透明值。通常,跨站点请求伪造(CSRF,XSRF)缓解是通过加密将此参数的值与浏览器 cookie 绑定来完成的。”

【讨论】:

  • 这已经由 openidconnect 中间件本身处理了,并且是另一个 cookie,而不是我在我的问题中谈到的那个。无论如何,感谢您的参与。
猜你喜欢
  • 2015-03-03
  • 1970-01-01
  • 1970-01-01
  • 2014-10-25
  • 2011-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多