eShopOnContainers 知多少[3]:Identity microservice (3)

IdentityResources

public static IEnumerable<IdentityResource> GetResources() { return new List<IdentityResource> { new IdentityResources.OpenId(), new IdentityResources.Profile() }; }

ApiResources

public static IEnumerable<ApiResource> GetApis() { return new List<ApiResource> { new ApiResource("orders", "Orders Service"), new ApiResource("basket", "Basket Service"), new ApiResource("marketing", "Marketing Service"), new ApiResource("locations", "Locations Service"), new ApiResource("mobileshoppingagg", "Mobile Shopping Aggregator"), new ApiResource("webshoppingagg", "Web Shopping Aggregator"), new ApiResource("orders.signalrhub", "Ordering Signalr Hub") }; } 5. 迁移数据库上下文

下面就把提前在代码预置的种子数据迁移到数据库中,我们如何做呢?IdentityServer为配置数据和操作数据分别定义了DBContext用于持久化,配置数据对应ConfigurationDbContext,操作数据对应PersistedGrantDbContext。代码如下所示:

public static void Main(string[] args) { BuildWebHost(args) .MigrateDbContext<PersistedGrantDbContext>((_, __) => { })//迁移操作数据库 .MigrateDbContext<ApplicationDbContext>((context, services) => { var env = services.GetService<IHostingEnvironment>(); var logger = services.GetService<ILogger<ApplicationDbContextSeed>>(); var settings = services.GetService<IOptions<AppSettings>>(); new ApplicationDbContextSeed() .SeedAsync(context, env, logger, settings) .Wait(); })//迁移用户数据库 .MigrateDbContext<ConfigurationDbContext>((context,services)=> { var configuration = services.GetService<IConfiguration>(); new ConfigurationDbContextSeed() .SeedAsync(context, configuration) .Wait(); })//迁移配置数据库 .Run(); }

至此,本服务的核心代码已解析完毕。

最终的生成的数据库如下图所示:

IdentityDb

最后

本文从业务和技术上对本服务进行剖析,介绍了其技术选型,并紧接着简要介绍了ASP.NET Core Identity和IdentityServer4,最后分析源码,一步步揭开其神秘的面纱。至于客户端和其他微服务服务如何使用Identity microservice进行认证和授权,我将在后续文章再行讲解。

如果对ASP.NET Core Idenity和IdentityServer4不太了解,建议大家博客园阅读雨夜朦胧、晓晨Master和Savorboard
的博客进行系统学习后,再重读本文,相信你对Identity microservice的实现机制豁然开朗。

参考资料

雨夜朦胧 -- ASP.NET Core 认证与授权:初识认证/授权
Savorboard -- ASP.NET Core 之 Identity 入门(一)
晓晨Master -- IdentityServer(14)- 通过EntityFramework Core持久化配置和操作数据
IdentityServer4 知多少
OAuth2.0 知多少

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzpyfp.html