(10)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- Ocelot+Identity Server

 用 JWT 机制实现验证的原理如下图:  认证服务器负责颁发 Token(相当于 JWT 值)和校验 Token 的合法性。

 

 

一、 相关概念

API 资源(API Resource):微博服务器接口、斗鱼弹幕服务器接口、斗鱼直播接口就是API 资源。

客户端(Client):Client 就是官方微博 android 客户端、官方微博 ios 客户端、第三方微博客户端、微博助手等。

身份资源(Identity Resource):就是用户。

一个用户可能使用多个客户端访问服务器;一个客户端也可能服务多个用户。封禁了一个客户端,所有用户都不能使用这个这个客户端访问服务器,但是可以使用其他客户端访问;封禁了一个用户,这个用户在所有设备上都不能访问,但是不影响其他用户。

二、 搭建 identity server 认证服务器

新建一个空的 web 项目 ID4.IdServer

Nuget - 》 Install-Package IdentityServer4

首先编写一个提供应用列表、账号列表的 Config 类

复制代码
using IdentityServer4.Models; using System.Collections.Generic; namespace ID4.IdServer {     public class Config     {         /// <summary>        /// 返回应用列表         /// </summary>        /// <returns></returns>        public static IEnumerable<ApiResource> GetApiResources()         {             List<ApiResource> resources = new List<ApiResource>();             //ApiResource第一个参数是应用的名字,第二个参数是描述            resources.Add(new ApiResource("MsgAPI", "消息服务API"));             resources.Add(new ApiResource("ProductAPI", "产品API"));             return resources;         }         /// <summary>        /// 返回账号列表         /// </summary>                 /// <returns></returns>                 public static IEnumerable<Client> GetClients()         {             List<Client> clients = new List<Client>();             clients.Add(new Client             {                 ClientId = "clientPC1",//API账号、客户端Id                AllowedGrantTypes = GrantTypes.ClientCredentials,                 ClientSecrets =                 {                     new Secret("123321".Sha256())//秘钥                },                 AllowedScopes = { "MsgAPI", "ProductAPI" }//这个账号支持访问哪些应用            });             return clients;         }     } }
复制代码

 如果允许在数据库中配置账号等信息,那么可以从数据库中读取然后返回这些内容。疑问待解。

修改Startup.cs

关键字:
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信