【问题标题】:invalid_grant error IdentityServer 3 & asp.net coreinvalid_grant 错误 IdentityServer 3 & asp.net 核心
【发布时间】:2018-10-03 19:40:34
【问题描述】:

我有一个 ASP.NET Core 2 MVC 应用程序,它使用具有混合流的身份服务器 3,目的是获取访问令牌,我也可以进一步使用它来访问 API,有时我被重定向到 IDP 登录页面并在输入用户名后和密码我被重定向回 MVC 应用程序,但它随机失败。

我有如下配置

 services.AddAuthentication(sharedOptions =>
            {
                sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;

            })
             .AddOpenIdConnect(options =>
             {
                 options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                 options.Authority = authConfig.GetValue<string>("Authority");
                 options.RequireHttpsMetadata = false;
                 options.ClientId = authConfig.GetValue<string>("ClientId");
                 options.ClientSecret = authConfig.GetValue<string>("ClientSecret");
                 options.ResponseType = "code id_token token";
                 options.SaveTokens = true;
                 options.GetClaimsFromUserInfoEndpoint = false;
                 options.TokenValidationParameters = new
                 TokenValidationParameters
                 {
                     NameClaimType = ClaimTypes.Name,
                     RoleClaimType = ClaimTypes.Role
                 };
                 options.Events = new OpenIdConnectEvents
                 {
                     OnRemoteFailure = context =>
                     {
                         context.HttpContext.Response.Redirect($"/Error?RequestId=4000&errormessage={context.Failure?.Message }");
                         context.HandleResponse();
                         return Task.FromResult(0);
                     },
                     OnRedirectToIdentityProvider = context =>
                     {
                         //TODO: Get IdentityProvider value for Multiple subscribers and not from config
                         var idp = authConfig.GetValue<string>("IdentityProvider");
                         var acrValues = new List<string>();

                         if (!string.IsNullOrWhiteSpace(idp))
                             acrValues.Add($"idp:{idp}");

                         if (acrValues.Count > 0)
                             context.ProtocolMessage.AcrValues = string.Join(" ", acrValues);

                         //if (context.ProtocolMessage.RequestType != OpenIdConnectRequestType.Logout)
                         //{
                         //    if (!CurrentEnvironment.IsDevelopment() &&
                         //        context.ProtocolMessage.RequestType == OpenIdConnectRequestType.Authentication)
                         //    {
                         //        // in widget iframe skip prompt login screen
                         //        context.ProtocolMessage.Prompt = "none";
                         //    }
                         //    return Task.FromResult(0);
                         //}

                         var idTokenHint = context.HttpContext.User.FindFirst("id_token");
                         if (idTokenHint != null)
                             context.ProtocolMessage.IdTokenHint = idTokenHint.Value;

                         return Task.FromResult(0);
                     }

并且客户端的身份服务器上的配置就像

    "ClientName": "SampleApp",
    "ClientId": "sample.app.mvc",
    "Flow": 2,
    "RedirectUris": ["https://localhost:44368/signin-oidc"],
    "PostLogoutRedirectUris": ["https://localhost:44368/"],
        "PrefixClientClaims": true,
    "RequireConsent": false,
    "AllowedScopes": 
    [
        "openid",
        "profile",
        "roles",
        "CustomScope"
    ],
    "Claims": [{
        "Type": "subscriberId",
        "Value": "dcbe85c6-05b6-470d-b558-289d1ae3bb15"
    }],
"ClientSecrets": [{
        "Secret": "tudc73K2y7pnEjT2"
    }],

    "IdentityTokenLifetime": 300,
    "AccessTokenLifetime": 3600,
    "AuthorizationCodeLifetime": 300,
    "EnableLocalLogin": true
}

当我在浏览器中尝试时,大多数时候我都会遇到错误 invalid_grant。你能告诉我配置的哪一部分不正确吗?

【问题讨论】:

  • 大多数时候是什么意思?还有 - 您是否启用了 Identity Server 日志记录?如果没有 - 去做吧,它会对你有很大帮助。
  • 我已启用日志记录..我看到一个错误,上面写着无效的授权码
  • 这绝不是一个完整的答案,但有一个解决方案使用身份服务器 4 和 .net core 2.0,codeproject.com/Articles/1205745/…。我很好奇这是否对您有帮助,因为我没有看到 EnableLocalLogin。

标签: asp.net-core-mvc openid-connect identityserver3


【解决方案1】:

我找到了问题所在。

由于上述任何配置,它没有发生。但是因为我使用 InMemory 存储作为 AuthorizationCode 存储,而且我的身份服务器部署在 Azure 上并且有 2 个实例。

【讨论】:

  • 这是在身份服务器还是客户端应用程序上?您在代码中的哪个位置进行了更改?另外,您是否减少了 Identity Server 运行的实例数量?
  • 身份服务器抛出错误,当我将实例数减少到 1 时解决了
  • 我们也遇到了这个问题。 Identityserver3 在生产槽 (80%) 和 Canaryslot (20%) 之间分配流量。在将所有流量转移到生产槽后,问题似乎得到了解决。还是有点奇怪,因为我们在 Azure 上启用了 ARRAffinity-cookie
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-10
  • 2018-03-12
  • 2019-02-01
  • 2016-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多