zoukankan      html  css  js  c++  java
  • 第20章 定义客户端

    客户端表示可以从您的身份服务器请求令牌的应用程序。

    详细信息各不相同,但您通常会为客户端定义以下常用设置:

    • 唯一的客户ID
    • 如果需要的秘密
    • 允许与令牌服务的交互(称为授权类型)
    • 身份和/或访问令牌发送到的网络位置(称为重定向URI)
    • 允许客户端访问的范围列表(也称为资源)

    注意
    在运行时,通过实现来检索客户端IClientStore。这允许从任意数据源(如配置文件或数据库)加载它们。对于本文档,我们将使用客户端存储的内存版本。您可以通过ConfigureServicesAddInMemoryClientsextensions方法连接内存存储。

    20.1 定义服务器到服务器通信的客户端

    在这种情况下,没有交互式用户 - 服务(aka client)想要与API(aka scope)进行通信:

    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" }
                }
            };
        }
    }
    

    20.2 定义基于浏览器的JavaScript客户端(例如SPA)以进行用户身份验证和委托访问及API

    此客户端使用所谓的隐式流来从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"
        }
    };
    

    20.3 定义服务器端Web应用程序(例如MVC)以使用身份验证和委托API访问

    交互式服务器端(或本地桌面/移动)应用程序使用混合流。此流程为您提供最佳安全性,因为访问令牌仅通过反向通道调用传输(并允许您访问刷新令牌):

    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"
        },
    };
    

    20.4 在appsettings.json中定义客户端

    AddInMemoryClients扩展方法也支持从ASP.NET Core配置文件中添加客户端。这允许您直接从appsettings.json文件定义静态客户端:

    "IdentityServer": {
      "IssuerUri": "urn:sso.company.com",
      "Clients": [
        {
          "Enabled": true,
          "ClientId": "local-dev",
          "ClientName": "Local Development",
          "ClientSecrets": [ { "Value": "<Insert Sha256 hash of the secret encoded as Base64 string>" } ],
          "AllowedGrantTypes": [ "implicit" ],
          "AllowedScopes": [ "openid", "profile" ],
          "RedirectUris": [ "https://localhost:5001/signin-oidc" ],
          "RequireConsent": false
        }
      ]
    }
    

    然后将配置部分传递给AddInMemoryClients方法:

    AddInMemoryClients(configuration.GetSection("IdentityServer:Clients"))
    

    github地址

  • 相关阅读:
    ffmpeg使用中文使用+解释
    CentOS GCC升级到4.6.1(转)
    C#委托与直接调用方法区别
    微软面试题,倒置数组
    MSDN中介绍的图片格式
    你们的学校是几流
    VS代码模版
    正则表达式
    Visual Studio 2010中添加项目模板
    泛型详解
  • 原文地址:https://www.cnblogs.com/thinksjay/p/10774846.html
Copyright © 2011-2022 走看看