【问题标题】:IdentityServer4: "code challenge required" under hybrid flowIdentityServer4:混合流下的“需要代码质询”
【发布时间】:2021-06-10 22:29:27
【问题描述】:

我有一个 MVC 客户端访问受 IDS4 服务器保护的 Web API。当我使用 ResponseType = "code" 时,它工作得很好。但是当我将其更改为 ResponseType = "code id_token"(混合流)时,我开始收到此“抱歉,出现错误:需要无效请求代码挑战”错误。

这是我在 IDS4 端的客户端配置:

        new Client
        {
            ClientId = "mvc",
            ClientSecrets = { new Secret("secret".Sha256()) },
            AllowedGrantTypes = GrantTypes.HybridAndClientCredentials, 
            RequirePkce = false,
            //AllowedGrantTypes = GrantTypes.Code,

            RedirectUris = { "https://localhost:6009/signin-oidc" },
            PostLogoutRedirectUris = { "https://localhost:6009/signout-callback-oidc" },
            AllowOfflineAccess = true,

            AllowedScopes = new List<string>
            {
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile,
                IdentityServerConstants.StandardScopes.Email,
                "roles",
                "MyAPI"
            }

这是 MVC 客户端的配置。请注意,导致错误发生的是'options.ResponseType = "code id_token"'。如果我使用“代码”代替它不会出错。

    .AddOpenIdConnect("oidc", options =>
    {
        options.Authority = "https://localhost:6005";
        options.ClientId = "mvc";
        options.ClientSecret = "secret";
        //options.ResponseType = "code";
        options.ResponseType = "code id_token";
        options.SaveTokens = true;

        options.Scope.Add("profile");
        options.Scope.Add("email");
        options.GetClaimsFromUserInfoEndpoint = true;
        options.Scope.Add("roles");
        options.ClaimActions.MapJsonKey("role", "role", "role");
        options.TokenValidationParameters.RoleClaimType = "role";
        options.Scope.Add("MyAPI");
        options.Scope.Add("offline_access");
        options.TokenValidationParameters = new TokenValidationParameters
        {
            NameClaimType = "name"
        };
    });

这是日志中的内容:

[2021-03-12T15:25:51.014Z] [4] [Error] Request validation failed 
[2021-03-12T15:25:51.013Z] [4] [Error] code_challenge is missing
{
  "ClientId": "mvc",
  "RedirectUri": "https://localhost:6009/signin-oidc",
  "AllowedRedirectUris": [
    "https://localhost:6009/signin-oidc"
  ],
  "SubjectId": "anonymous",
  "ResponseType": "code id_token",
  "ResponseMode": "fragment",
  "GrantType": "hybrid",
  "RequestedScopes": "",
  "State": "CfDJ....8oxh4",
  "PromptMode": "",
  "Raw": {
    "client_id": "mvc",
    "redirect_uri": "https://localhost:6009/signin-oidc",
    "response_type": "code id_token",
    "scope": "openid profile email roles MyAPIoffline_access",
    "response_mode": "form_post",
    "nonce": "637511...lOGNh",
    "state": "CfDJ8...8oxh4",
    "x-client-SKU": "ID_NETSTANDARD2_0",
    "x-client-ver": "5.5.0.0"
  }
} 

顺便说一下,我正在测试混合流,因为我试图让 Windows 身份验证在 IDS4 下工作。我的本地登录有效,但 Windows 无效。一些资源说为此需要混合流。有人可以告诉我我在这里做错了什么吗?

【问题讨论】:

    标签: identityserver4 windows-authentication


    【解决方案1】:

    可能是 IdentityServer 总是需要 PKCE,这就是为什么当你没有提供必要的挑战时它会抱怨。

    Dominic 写信here:

    Code flow without PKCE is vulnerable to code injection attacks. That's why we do not expose it.
    

    在客户端中添加代码质询/验证器并不复杂。

    以下是我在培训课程中介绍 OAuth 2.1 的方式:

    【讨论】:

    • 感谢 Tore Nestenius!请原谅我的无知,但我认为只有公共客户端需要 PKCE,比如移动设备,而不是 MVC。另外,如果我只是做 ...ResponseType = "code",它可以与我的 MVC 客户端正常工作。您能否为我指出如何在客户端添加代码质询/验证程序的正确方向?我想试一试。再次感谢。
    • 对于最新的 OAuth 2.1,建议始终在代码流中使用 PKCE。为什么需要混合流?你有什么样的客户端不支持PKCE? ASP.NET Core MVC 内置了它。今天,您应该尝试仅使用授权代码流或客户端凭据流进行机器对机器设置。
    • 再次感谢 Tore Nestenius!很高兴知道!我不确定我是否真的需要混合流——我只是想让 Windows 身份验证工作,有人建议我需要它。我知道我最初的问题与此无关,但我真正的问题是我的 Windows 身份验证似乎在循环运行 - 浏览器的 Windows 登录屏幕在提交后不断返回,没有任何错误。
    • 这篇文章 (damienbod.com/2018/04/15/…) 似乎暗示我需要混合流来让本地和 Windows 登录都适用于 MVC。
    • oauth.net/2.1youtube.com/watch?v=U1lgyU3CKI8&ab_channel=OktaDev Arraon 是 OAuth 2.1 的作者之一,所以这是他对 OAuth 的一个很好的展示。
    猜你喜欢
    • 2019-03-21
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 2020-02-14
    • 2019-07-31
    • 2020-02-06
    相关资源
    最近更新 更多