IdentityServer4 + SignalR Core +RabbitMQ 构建web即时通讯(二)


IdentityServer4 用户中心生成数据库

上文已经创建了所有的数据库上下文迁移代码,这里开始数据库的迁移和种子数据,EF Core 2.1刚好新增了种子数据的功能,文档地址,一开始的想法是使用这种方式,看起来很简洁与方便,但需要在OnModelCreating中配置,不过IdentityServer4中的2个数据库上下文我不知道怎么配置进去了,所以还是用比较原始的方式吧。

1.在SeedData添加客户端Client ApiResource IdentityRecouse的种子数据;

        public static List<Client> Clients()
{
return new List<Client>
{
new Client{
// 客户端id
ClientId ="chat_client",
// 客户端名称
ClientName ="chat client",
// TOKEN有效时长
AccessTokenLifetime = ,
// 配置TOKEN类型,reference为引用类型,数据不会存在TOKEN中
AccessTokenType= AccessTokenType.Jwt,
// 配置客户端授权模式
AllowedGrantTypes= GrantTypes.ResourceOwnerPassword,
// 配置客户端连接密码
ClientSecrets={ new Secret("".Sha256())},
// 客户端允许的请求范围
AllowedScopes={
"chatapi",
IdentityServerConstants.StandardScopes.OfflineAccess,
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile },
//允许离线,即开启refresh_token
AllowOfflineAccess =true,
RequireClientSecret=false
}
};
} public static IEnumerable<ApiResource> ApiResources()
{
return new List<ApiResource>
{
// 定义api资源 这里如果使用构造函数传入Name会默认创建一个同名的Scope,
// 这点需要注意,因为这个Api如果没有Scope,那根本无法访问
new ApiResource
{
Name="chatapi",
DisplayName="chat api",
ApiSecrets= { new Secret("".Sha256()) },
Scopes={
new Scope("chatapi","chat api")
}
}
};
} public static IEnumerable<IdentityResource> IdentityResources()
{
return new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResources.Profile()
};
}

2.StartUp中增加迁移方法,Database.Migrate() 实际就是Update-Database命令,那为什么这样用呢?生产环境总不能让我去执行Update-Database吧?,当我们修改了实体代码与配置时,不用做任何处理,程序运行时就能自动更新数据库结构。Configure中调用 Migration(app).Wait();

        public static async Task Migration(IApplicationBuilder app)
{
using (var scope = app.ApplicationServices.CreateScope())
{
// 迁移DemoDbContext上下文
scope.ServiceProvider.GetRequiredService<DemoDbContext>().Database.Migrate();
// 迁移PersistedGrantDbContext上下文
scope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
var configurationDbContext = scope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
// 迁移ConfigurationDbContext上下文
configurationDbContext.Database.Migrate(); // 注入用户管理 增加用户
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<DemoUser>>();
foreach (var user in SeedData.Users())
{
if (userManager.FindByNameAsync(user.UserName).Result == null)
{
await userManager.CreateAsync(user, "");
}
} // 增加ApiResources IdentityResources Clients
if (!configurationDbContext.ApiResources.Any())
configurationDbContext.ApiResources.AddRange(SeedData.ApiResources().Select(r => r.ToEntity()));
if (!configurationDbContext.IdentityResources.Any())
configurationDbContext.IdentityResources.AddRange(SeedData.IdentityResources().Select(r => r.ToEntity()));
if (!configurationDbContext.Clients.Any())
configurationDbContext.Clients.AddRange(SeedData.Clients().Select(r => r.ToEntity()));
await configurationDbContext.SaveChangesAsync();
}
}

这里有个不算坑的坑,Add-Migration命令创建迁移代码时需要把Migration(app).Wait()注释掉,为什么呢?这个命令实际上会启动应用,并执行里面的代码,当首次执行时,数据库并不存在,迁移代码也不存在,所以数据结构都还不存在,如果执行种子数据操作肯定会失败了,当然Add-Migration本身就算是一个代码生成器,属于开发时操作,所以这是一个不算坑的坑。

4.启动程序,控制台能看到迁移信息,

好了,数据库已经给我们创建好了

看看AspNetUsers,用户数据也没问题

现在打开Postman,用laowang这个用户试一试token能否拿到,token获取的地址为/connect/token,刷新token也是这个地址

没问题,我们把token复制下拿到jwt.io看看是否带有用户信息,

可以看到username,email,avatar,没有问题。

好了,这篇就到这,下一篇开始写聊天室后端服务。

IdentityServer4 + SignalR Core +RabbitMQ 构建web即时通讯(二)的更多相关文章

  1. IdentityServer4 + SignalR Core +RabbitMQ 构建web即时通讯(三)

    IdentityServer4 + SignalR Core +RabbitMQ 构建web即时通讯(三) 后台服务用户与认证 新建一个空的.net core web项目Demo.Chat,端口配置为 ...

  2. IdentityServer4 + SignalR Core +RabbitMQ 构建web即时通讯(一)

    IdentityServer4 + SignalR Core +RabbitMQ 构建web即时通讯 前言 .net core 2.1已经正式发布了,signalr core1.0随之发布,是时候写个 ...

  3. 一套简单的web即时通讯——第一版

    前言 我们之前已经实现了 WebSocket+Java 私聊.群聊实例,后面我们模仿layer弹窗,封装了一个自己的web弹窗 自定义web弹窗/层:简易风格的msg与可拖放的dialog,生成博客园 ...

  4. web即时通讯2--基于Spring websocket达到web聊天室

    如本文所用,Spring4和websocket要构建web聊天室,根据框架SpringMVC+Spring+Hibernate的Maven项目,后台使用spring websocket进行消息转发和聊 ...

  5. 为什么 web 开发人员需要迁移到. NET Core, 并使用 ASP.NET Core MVC 构建 web 和 webservice/API

    2018 .NET开发者调查报告: .NET Core 是怎么样的状态,这里我们看到了还有非常多的.net开发人员还在观望,本文给大家一个建议.这仅代表我的个人意见, 我有充分的理由推荐.net 程序 ...

  6. 利用Eclipse中的Maven构建Web项目(二)

    利用Eclipse中的Maven构建Web项目 1.新建源文件夹,Java Resources鼠标右键,"New-->Source Folder" 2.新建src/main/ ...

  7. 使用 ASP.NET Core MVC 创建 Web API(二)

    使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 六.添加数据库上下文 数据库上下文是使用Entity Framewor ...

  8. .Net core 3.0 SignalR+Vue 实现简单的即时通讯/聊天IM (无jq依赖)

    .Net core 中的SignalR JavaScript客户端已经不需要依赖Jquery了 一.服务端 1.nuget安装 Microsoft.AspNetCore.SignalR 2.在star ...

  9. Mysql EF Core 快速构建 Web Api

    (1)首先创建一个.net core web api web项目; (2)因为我们使用的是ef连接mysql数据库,通过NuGet安装MySql.Data.EntityFrameworkCore,以来 ...

随机推荐

  1. Linux内核情景分析之消息队列

    早期的Unix通信只有管道与信号,管道的缺点: 所载送的信息是无格式的字节流,不知道分界线在哪,也没通信规范,另外缺乏控制手段,比如保温优先级,管道机制的大小只有1页,管道很容易写满而读取没有及时,发 ...

  2. c# 序列化对象为xml 方法

    public static string XmlUtils(object obj, bool omitXmlDeclaration = true, bool indent = false, bool ...

  3. 牛客网 Wannafly挑战赛8 A.小Y和小B睡觉觉

    写了一会不想写了... A-小Y和小B睡觉觉 链接:https://www.nowcoder.com/acm/contest/57/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制: ...

  4. 设计模式(1)---Factory Pattern

    针对的问题:定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行. 第一步:创建接口 //创建一个接口 public interface Shape { pu ...

  5. SecureCRT同时向多个终端发送命令

    1.[View]->[Command Window] 2.[Send Command to]->[All Sessions] 参考: http://www.netingcn.com/sec ...

  6. SecureCRT双击Tab快速复制Session

  7. 【java】StringBuilder的三种清除方法对比

    参考链接:https://blog.csdn.net/roserose0002/article/details/6972391

  8. Scut游戏服务器引擎6.0.5.0发布-支持C#脚本

    1. 增加C#脚本支持2. 增加Pay和Sns中间件对Mysql数据库支持3. 精简布署步骤,取消Redis写入程序,将其移到游戏底层运行4. 修正Mysql对中文可能会出现乱码的BUG 点击下载:S ...

  9. CI框架基础知识

    调用一个视图 a.调用一个视图 $this->load->view('视图文件名'); b.调用多个视图 $this->load->view('index_h'); $this ...

  10. 都是 htmlspecialchars的错,解决 织梦cms dedecms 标题不能为空 不支持php5.3 php5.4 php5.5版本

    article_add.php  101行 $title = htmlspecialchars(cn_substrR($title,$cfg_title_maxlen)); 改成 $title = h ...