粘贴OAuth2和OpenIdConnect需要Cookie中间件吗?

时间:2018-02-20 20:49:08

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

我们的申请包括:

  • 前端,这是一个使用OAuth2在我们的后端进行身份验证的SPA
  • 后端,它是充当OAuth2服务器和OpenID连接客户端的ASP.NET OWIN应用程序
  • 第三方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>())
});

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

1 个答案:

答案 0 :(得分:0)

通常,这是最佳做法,并且可以按照OpenID Connect Basic Client Implementer's Guide 1.0 - draft 37 Section 2.1.1.1. Request Parameters

中的说明使用“状态”参数

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