【问题标题】:Identity Server 4 with Asp.net (.Net framework 4.7.2) How to fix "Invalid grant type for client"Identity Server 4 with Asp.net (.Net framework 4.7.2) 如何修复“无效的客户端授权类型”
【发布时间】:2019-11-25 18:47:59
【问题描述】:

我将 IdentityServer4 与 Asp.Net MVC(.net 框架)一起使用。授权时,我的站点可以导航到 Identity Server,但出现错误:客户端的授权类型无效。我该如何解决?给我这个组合的样本。非常感谢。

身份服务器客户端配置

{
      "ClientId": "nfc",
      "ClientName": ".net framework client",

      // 5AF90EA2-CA6E-4AF9-AC1C-EAC72933D20D
      "ClientSecrets": [ { "Value": "bBsQFRHNGCMB6W3EdybGe/lO8iOawFpeQ2ipC+nhGVM=" } ],
      "AllowedGrantTypes": [ "client_credentials" ],
      "AllowedScopes": [ "openid", "profile" ],
      "AllowOfflineAccess": true,

      "RedirectUris": [ "http://localhost:44398/signin-oidc" ],
      "FrontChannelLogoutUris": [ "http://localhost:44398/signout-oidc" ],
      "PostLogoutRedirectUris": [ "http://localhost:44398/signout-callback-oidc" ]
    },

Asp.net 客户端启动.cs

[assembly: OwinStartup("ProductionConfiguration", typeof(NetFrameworkClient.Startup))]

namespace NetFrameworkClient
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

            app.UseCookieAuthentication(new CookieAuthenticationOptions { });

            app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                AuthenticationType = "oidc",
                SignInAsAuthenticationType = "Cookies",
                Authority = "http://localhost:5000/",
                RedirectUri = "http://localhost:44398/signin-oidc",
                PostLogoutRedirectUri = "http://localhost:44398/signout-callback-oidc",
                ClientId = "mvc",
                ResponseType = "id_token",
                Scope = "openid profile",
                UseTokenLifetime = false,
                RequireHttpsMetadata = false,
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    SecurityTokenValidated = (context) =>
                    {
                        var identity = context.AuthenticationTicket.Identity;
                        var name = identity.Claims.FirstOrDefault(c => c.Type == identity.NameClaimType)?.Value;

                        return Task.FromResult(0);
                    }
                }
            });
        }
    }
}

在控制器上授权

[Authorize]
public ActionResult Index()
{
    return View();
}

我得到的错误:抱歉,出现错误:未授权客户端 客户的授权类型无效

【问题讨论】:

    标签: asp.net identityserver4


    【解决方案1】:

    它不起作用,因为在您的身份服务器配置中,您的客户端 ID 是“nfc”,而在 .NET 应用程序的配置中,您将客户端 ID 指定为“mvc”。

    为了让您的 .net 应用程序使用 Identity Server 进行授权,它需要传回一个有效的客户端 ID。

    您可以在文档中找到更多信息:http://docs.identityserver.io/en/latest/quickstarts/1_client_credentials.html

    希望有帮助!

    【讨论】:

      【解决方案2】:

      不确定它是否适合你,但适合我:MVC 4.7.2 有 "GrantType": "hybrid" 所以我将 IdentityServer4 中的设置更改为“AllowedGrantTypes”:[“hybrid”]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-01-11
        • 2020-08-06
        • 1970-01-01
        • 1970-01-01
        • 2020-04-28
        • 2018-11-02
        • 2018-06-27
        相关资源
        最近更新 更多