IdentityServer Topics(3)- 定義客戶端

客戶端指能夠從你的 identityserver 請求令牌的應用程序。html

細節可能有所不一樣,可是客戶端一般有如下設置web

  • 一個惟一的客戶端ID
  • 一個密鑰(非必須)
  • 容許與令牌服務的交互(稱爲受權類型)
  • 身份或訪問令牌被髮送到的url(稱爲重定向URI)
  • 容許客戶端訪問的Scope列表(API資源)

在運行時,客戶端經過IClientStore的實現來檢索。 這容許從配置文件或數據庫的任意數據源加載它們。 對於本文檔,咱們將使用客戶端存儲的內存存儲版本。 您能夠經過AddInMemoryClients擴展方法在ConfigureServices中配置內存存儲。數據庫

一.定義Server到Server的客戶端

在這種狀況下,沒有交互式用戶 - 服務(也稱爲客戶端)想要與API(aka範圍)進行通訊:api

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"
    },
};
相關文章
相關標籤/搜索