Web API 2:微服务架构中的授权代码流

WebAPI2: Authorization code flow in micro services architecture

身份提供者:Keycloak-9.0.0 .net 版本:4.5.2

基本上我正在尝试集成 c# webapi 服务,如下所示。

Authorization code flow

我使用了用于 C# 的 Keycloak 连接器(https://github.com/mattmorg55/Owin.Security.Keycloak),它被设计为 OWIN 身份验证中间件组件

使用 keycloak 示例时出现错误。 但是我不确定调用是否被转发到 keycloak 进行验证而不是我得到一个错误。

  1. 如果未启用 WebAPI 模式,我会得到 "signature-validation-failed-unable-to-match-kid"
  2. 如果启用了 webAPI 模式,我会收到 401 ({"Message":"Authorization has been denied for this request."}Access 未授权:需要有效的不记名令牌授权 header

启动class

public void Configuration(IAppBuilder app)
        {

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = "Bearer"
            });



            app.UseKeycloakAuthentication(new KeycloakAuthenticationOptions
            {
                Realm = "test_keycloak",
                ClientId = "test",
                ClientSecret = "9f25fd55-851f-4eed-9fb9-24a0a0e4ff11",

                KeycloakUrl = "http://localhost:8080/auth",
                AuthenticationType = "Bearer",
                SignInAsAuthenticationType = "Bearer",

                AllowUnsignedTokens = false,
                DisableIssuerSigningKeyValidation = false,
                DisableIssuerValidation = false,
                UseRemoteTokenValidation = true,
                EnableWebApiMode = true,
                DisableAudienceValidation = false,
                Scope= "openid",

            });
}

我在 keycloak 中没有看到任何日志。可能出了什么问题?我该如何调试?

由于它是标准的 Oauth2 流程,我可以使用 Microsoft.Owin.Security.OpenIdConnect 进行令牌验证吗?

例如 java spring 中的安全性具有相同的简单配置(使用 jwt-cert -url)

需要您的意见!

我也可以用 microsoft.owin.security.jwt 来解决。 这是代码。

注:没有做异常处理。只是基本代码。

public void Configuration(IAppBuilder app) {
        HttpClient htpp = new HttpClient();
        var keysResponse = htpp.GetAsync("https://<FQDN of keycloak>/auth/realms/<realm>/protocol/openid-connect/certs").Result;
        
        var rawKeys = keysResponse.Content.ReadAsStringAsync().Result;
        

        Microsoft.IdentityModel.Tokens.JsonWebKeySet jsonWebKeySet = JsonConvert.DeserializeObject<Microsoft.IdentityModel.Tokens.JsonWebKeySet>(rawKeys);

        app.UseJwtBearerAuthentication(
        new JwtBearerAuthenticationOptions {
            AuthenticationType = DefaultAuthenticationTypes.ExternalBearer,
            AuthenticationMode = AuthenticationMode.Active,
            Realm = <realm>",
            
            TokenValidationParameters = new TokenValidationParameters() {
                
                AuthenticationType = "Bearer",
                ValidateIssuer = true,
                ValidateIssuerSigningKey = true,
                ValidAudiences = new string[] { <clientID> },
                ValidIssuer = "<FQDN of keycloak>/auth/realms/<realm>",
                ValidateLifetime = true,
                ValidateAudience = true,
                IssuerSigningKeys = jsonWebKeySet.GetSigningKeys(),
                
            }
        });
    }