簡體   English   中英

Identity Server 4 with Asp.net (.Net framework 4.7.2) 如何修復“客戶端的無效授權類型”

[英]Identity Server 4 with Asp.net (.Net framework 4.7.2) How to fix "Invalid grant type for client"

我將 IdentityServer4 與 Asp.Net MVC(.net 框架)一起使用。 授權時,我的站點可以導航到身份服務器,但出現錯誤:客戶端的授權類型無效。 我該如何解決? 給我一個這個組合的樣本。 非常感謝。

身份服務器客戶端配置

{
      "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 客戶端 Startup.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();
}

我得到的錯誤:對不起,有一個錯誤:unauthorized_client 客戶端的授權類型無效

它不起作用,因為在您的身份服務器配置中,您的客戶端 ID 是“nfc”,而在 .NET 應用程序的配置中,您將客戶端 ID 指定為“mvc”。

為了讓您的 .net 應用程序使用 Identity Server 進行授權,它需要傳回一個有效的客戶端 ID。

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

希望有幫助!

不確定它是否適合你,但對我來說:MVC 4.7.2 有 "GrantType": "hybrid" 所以我將 IdentityServer4 中的設置更改為 "AllowedGrantTypes": [ "hybrid" ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM