zoukankan      html  css  js  c++  java
  • 一步一步学习IdentityServer3 (5)

    这篇文章介绍下数据持久化问题,官方例子可能都是缓存数据 Client  User Scope 

    下面介绍下怎么使用数据库持久化 这里需要导入nuget包 :IdentityServer3.EntityFramework 

    添加Startup类

    在Configuration(IAppBuilder app) 中去实现Idr3相关的中间件配置

     var ef = new EntityFrameworkServiceOptions
                {
                    ConnectionString = "Idr3ConnectionString", //配置的连接字符串,EF会自动生成数据库
                };
                //配置地址参数
    
                app.Map("/lym", lymapp =>
                {
                    SetClients(Clients.Get(), ef); //初始化Client到数据库 
                    SetScopes(Scopes.Get(), ef); //初始化Scopes 到数据库
                    var factory = new IdentityServerServiceFactory();
                    factory.RegisterConfigurationServices(ef);
                    factory.RegisterOperationalServices(ef);
                    factory.RegisterClientStore(ef);
                    factory.RegisterScopeStore(ef);
    });

    初始化数据到数据库 其实就是调用EF相关保存 一些Client初始数据,这里不需要去初始话用户数据,后面会提到怎么使用业务数据库中的用户信息登录

     public static void SetClients(IEnumerable<Client> clients, EntityFrameworkServiceOptions options)
            {
                using (var db = new ClientConfigurationDbContext(options.ConnectionString, options.Schema))
                {
                    if (!db.Clients.Any())
                    {
                        foreach (var c in clients)
                        {
                            var e = c.ToEntity();
                            db.Clients.Add(e);
                        }
                        db.SaveChanges();
                    }
                }
            }
    
            public static void SetScopes(IEnumerable<Scope> scopes, EntityFrameworkServiceOptions options)
            {
                using (var db = new ScopeConfigurationDbContext(options.ConnectionString, options.Schema))
                {
                    if (!db.Scopes.Any())
                    {
                        foreach (var s in scopes)
                        {
                            var e = s.ToEntity();
                            db.Scopes.Add(e);
                        }
                        db.SaveChanges();
                    }
                }
            }

    这样就设置好了持久化数据库了,这里要注意Map中还有其他的设置,这里这是附加了生成数据库相关配置

    运行起来查看那下数据

    数据库已经生成了

    Client数据也初始化了,这是我配置的几个Client 、Scope,也都在里面了

    Idr3数据持久化就到这里了,数据库生成了就可以看下表之间的关系,分析下就基本明白表的作用了

  • 相关阅读:
    Number原生类型的扩展
    复杂参数的基本使用方式
    使用Number原生类型
    Function原生类型
    面向对象类型系统
    Error原生类型的扩展
    Date原生类型的扩展
    flex学习网站大全(转)
    如何调试javaScript
    使用JavaScriptConverter实现返回DataTable对象
  • 原文地址:https://www.cnblogs.com/liyouming/p/7514799.html
Copyright © 2011-2022 走看看