客戶端指可以從你的 identityserver 請求令牌的應用程序。
細節可能有所不同,但是客戶端通常有以下設置
- 一個唯一的客戶端ID
- 一個密鑰(非必須)
- 允許與令牌服務的交互(稱為授權類型)
- 身份或訪問令牌被發送到的url(稱為重定向URI)
- 允許客戶端訪問的Scope列表(API資源)
在運行時,客戶端通過
IClientStore
的實現來檢索。 這允許從配置文件或數據庫的任意數據源加載它們。 對於本文檔,我們將使用客戶端存儲的內存存儲版本。 您可以通過AddInMemoryClients
擴展方法在ConfigureServices
中配置內存存儲。
一.定義Server到Server的客戶端
在這種情況下,沒有交互式用戶 - 服務(也稱為客戶端)想要與API(aka范圍)進行通信:
public class Clients
{
public static IEnumerable<Client> Get()
{
return new List<Client>
{
new Client
{
ClientId = "service.client",
ClientSecrets = { new Secret("secret".Sha256()) },
AllowedGrantTypes = GrantTypes.ClientCredentials,
AllowedScopes = { "api1", "api2.read_only" }
}
};
}
}
二.定義 avaScript客戶端(例如SPA)進行用戶認證和授權訪問和API
這個客戶端使用implicit flow來從JavaScript請求身份和訪問令牌:
var jsClient = new Client
{
ClientId = "js",
ClientName = "JavaScript Client",
ClientUri = "http://identityserver.io",
AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser = true,
RedirectUris = { "http://localhost:7017/index.html" },
PostLogoutRedirectUris = { "http://localhost:7017/index.html" },
AllowedCorsOrigins = { "http://localhost:7017" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
"api1", "api2.read_only"
}
};
三.定義服務器端Web應用程序(例如MVC)以進行使用驗證和授權的API訪問
交互式服務器端(或本地桌面/移動)應用程序使用混合流程(hybrid flow)。 這個流程為您提供了最好的安全性,因為訪問令牌僅通過反向通道傳輸(並允許您訪問刷新令牌):
var mvcClient = new Client
{
ClientId = "mvc",
ClientName = "MVC Client",
ClientUri = "http://identityserver.io",
AllowedGrantTypes = GrantTypes.Hybrid,
AllowOfflineAccess = true,
ClientSecrets = { new Secret("secret".Sha256()) },
RedirectUris = { "http://localhost:21402/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:21402/" },
FrontChannelLogoutUri = "http://localhost:21402/signout-oidc",
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
"api1", "api2.read_only"
},
};