Asp.net-Mvc

Identity Server 3 - 客戶端應用程序未知或未經授權

  • May 18, 2021

我收到錯誤消息“客戶端應用程序未知或未經授權。” 訪問我網站的受保護區域時。

這是我的客戶:

public static class Clients
{
   public static IEnumerable<Client> Get()
   {
       return new[]
       {
           new Client
           {
               Enabled = true,
               ClientName = "Web Application",
               ClientId = "webapplication",
               Flow = Flows.AuthorizationCode,

               ClientSecrets = new List<Secret>
               {
                   new Secret("webappsecret".Sha256())
               },

               RedirectUris = new List<string>
               {
                   UrlManager.WebApplication
               },
               PostLogoutRedirectUris = new List<string>
               {
                   UrlManager.WebApplication
               },

               AllowedScopes = new List<string>
               {
                   Constants.StandardScopes.OpenId,
                   Constants.StandardScopes.Profile,
                   Constants.StandardScopes.Email,
                   Constants.StandardScopes.Roles,
                   Constants.StandardScopes.OfflineAccess,
                   "read",
                   "write"
               }
           }
       };
   }
}

這是我的 Web 應用程序啟動:

public class Startup
{
   public void Configuration(IAppBuilder app)
   {
       app.UseCookieAuthentication(new CookieAuthenticationOptions
       {
           AuthenticationType = "Cookies"
       });

       app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
       {
           Authority = UrlManager.AuthenticationService + "identity",

           ClientId = "webapplication",
           Scope = "openid profile",
           ResponseType = "code id_token",
           RedirectUri = UrlManager.WebApplication,

           SignInAsAuthenticationType = "Cookies"
       });
   }
}

這是我的身份驗證服務(安裝了 IDS3)啟動:

public class Startup
{
   public void Configuration(IAppBuilder app)
   {
       app.Map("/identity", idsrvApp =>
       {
           idsrvApp.UseIdentityServer(new IdentityServerOptions
           {
               SiteName = "Authentication Service - Embedded IdentityServer",
               SigningCertificate = Certificate.LoadCertificate(),

               Factory = new IdentityServerServiceFactory()
                           .UseInMemoryUsers(Users.Get())
                           .UseInMemoryClients(Clients.Get())
                           .UseInMemoryScopes(Scopes.Get())
           });
       });
   }
}

這是 UrlManager:

public static class UrlManager
{
   public static string WebApplication
   {
       get { return "https://localhost:44381/"; }
   }

   public static string AuthenticationService
   {
       get { return "https://localhost:44329/"; }
   }
}

這是我的家庭控制器:

public class HomeController : Controller
{
   public ActionResult Index()
   {
       return View();
   }

   [Authorize]
   public ActionResult Private()
   {
       return View((User as ClaimsPrincipal).Claims);
   }
}

當我訪問 Private 時,我得到一個 Identity Server 3 螢幕,顯示錯誤消息“客戶端應用程序未知或未授權。”。

我已經讀到這可能來自重定向 URI 中的不匹配,但據我所知,我的是正確的。我不知道還有什麼可能導致它。如果我將流程更改為隱式但我想實現 AuthorizationCode 流程,則應用程序可以完美執行。

該文件似乎也沒有對此有所說明。

客戶端配置為授權碼流

Flow = Flows.AuthorizationCode

但是啟動中的響應類型設置為混合流。

ResponseType = “程式碼 id_token”

嘗試將其更改為

ResponseType = “code”(或將流類型更改為混合)

下面是 ResponseType 和對應 Flow 的列表在此處輸入圖像描述

我收到了這個錯誤,問題是 RedirectUri. 在授權伺服器中是**http://localhost:56840/而在 Web 應用程序中是http://localhost:56840**。請注意網址末尾缺少的“/”。

引用自:https://stackoverflow.com/questions/37661338