使用Identity Server 4建立Authorization Server (5)

预备知识: http://www.cnblogs.com/cgzl/p/7746496.html

第一部分: http://www.cnblogs.com/cgzl/p/7780559.html

第二部分: http://www.cnblogs.com/cgzl/p/7788636.html

第三部分: http://www.cnblogs.com/cgzl/p/7793241.html

第四部分: http://www.cnblogs.com/cgzl/p/7795121.html

之前的配置都是在内存中, 下面将如何把这些数据存储到Sql Server数据库, 这样更适合生产环境.

这部分基本完全参考官方文档:

https://identityserver4.readthedocs.io/en/release/quickstarts/8_entity_framework.html

安装Entity Framework相关的库

为Authorization Server 添加 IdentityServer4.EntityFramework:

还需要安装Microsoft.EntityFrameworkCore.SqlServer:

最后是Microsoft.EntityFrameworkCore.Tools:

使用它可以进行迁移等操作.

然后使用命令行进入Auth Server项目的目录, 试一下dotnet ef命令:

很不幸, 没找到dotnet ef命令. 这里需要手动修改AuthServer的项目文件, 右键点击项目, 点击Edit AuthServer.csproj.

这部分操作的官方文档在这: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet

我们需要添加这部分代码:

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
  </ItemGroup>

然后回到命令行, 再执行 dotnet ef:

这次好用了. 接下来就是add migrations了.

幸运的是, 之前装的库里面有封装好的model, 它们可以自动创建migration文件.

这里一共有两个命令(migrations), 一个是为了IdentityServer的配置, 另一个是为了持久化授权.

dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb

运行发现了问题, 这是因为我们还没有配置AuthServer来使用数据库.

添加appSettings.json, 并指定连接字符串:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=AuthServer;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

修改Startup.cs:

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = Configuration.GetConnectionString("DefaultConnection");
            var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

            services.AddIdentityServer()
                // .AddDeveloperSigningCredential()
                .AddSigningCredential(new X509Certificate2(@"D:\Projects\test\socialnetwork.pfx", "[email protected]"))
                .AddTestUsers(InMemoryConfiguration.Users().ToList())
                .AddConfigurationStore(options =>
                {
                    options.ConfigureDbContext = builder =>
                        builder.UseSqlServer(connectionString,
                            sql => sql.MigrationsAssembly(migrationsAssembly));
                })
                // this adds the operational data from DB (codes, tokens, consents)
                .AddOperationalStore(options =>
                {
                    options.ConfigureDbContext = builder =>
                        builder.UseSqlServer(connectionString,
                            sql => sql.MigrationsAssembly(migrationsAssembly));

                    // this enables automatic token cleanup. this is optional.
                    options.EnableTokenCleanup = true;
                    options.TokenCleanupInterval = 30;
                });

            services.AddMvc();
        }

首先获取数据库连接字符串, 然后添加两部分配置, 一个是配置数据(clients, resources), 一个是操作数据(tokens, codes, consents同意).

再次运行命令行:

dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb

好用了.

看看生成的文件, 是有这两部分:

看一下文件的内容, 会发现有很多的Table.

下一步就是添加自动迁移, 暂且在StartUp里面找个位置新建个方法吧:

        private void InitializeDatabase(IApplicationBuilder app)
        {
            using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
            {
                serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();

                var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
                context.Database.Migrate();
                if (!context.Clients.Any())
                {
                    foreach (var client in InMemoryConfiguration.Clients())
                    {
                        context.Clients.Add(client.ToEntity());
                    }
                    context.SaveChanges();
                }

                if (!context.IdentityResources.Any())
                {
                    foreach (var resource in InMemoryConfiguration.IdentityResources())
                    {
                        context.IdentityResources.Add(resource.ToEntity());
                    }
                    context.SaveChanges();
                }

                if (!context.ApiResources.Any())
                {
                    foreach (var resource in InMemoryConfiguration.ApiResources())
                    {
                        context.ApiResources.Add(resource.ToEntity());
                    }
                    context.SaveChanges();
                }
            }
        }

首先是分别对两个context进行迁移, 然后判断是否这些表里是空的, 如果没有数据, 就把配置的内存数据添加到数据库里面.

别忘了在Configure方法调用:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            InitializeDatabase(app);

            app.UseDeveloperExceptionPage();
            app.UseIdentityServer();
            app.UseStaticFiles();
            app.UseMvcWithDefaultRoute();
        }

运行项目, 重新操作一下登陆, 同意的过程, 依然好用.

看一下数据库:

确实生成了很多表.

查看Clients表, 里面有三条数据.

PersistedGrants里面也有一条数据. 登陆时当你同意请求许可的时候, 就会在这个表里面添加一条数据.

把用户存储到数据库

可以使用自定义的用户表来存储用户数据, 但是我要用的是asp.net core identity, 所以我就不讲别的方式了.

不过首先, 需要重建个项目, 并且把之前讲的所有内容都操作一遍, 因为这里要使用asp.net core mvc 模板并使用Individual User Account的验证方式:

建立好项目后, 需要把之前讲的所有步骤操作一下, 然后安装: IdentityServer4.AspNetIdentity:

修改Startup, 大约成为这个样子, 只看红色部分即可:

namespace AuthorizationServer
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = Configuration.GetConnectionString("DefaultConnection");
            var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(connectionString));

            services.AddIdentity<ApplicationUser, IdentityRole>(options =>
            {
                // Password settings
                options.Password.RequireDigit = false;
                options.Password.RequiredLength = 6;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase = false;
                options.Password.RequireLowercase = false;
                options.Password.RequiredUniqueChars = 2;
                // Lockout settings
                options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
                options.Lockout.MaxFailedAccessAttempts = 5;
                options.Lockout.AllowedForNewUsers = true;
                // Signin settings
                options.SignIn.RequireConfirmedEmail = false;
                options.SignIn.RequireConfirmedPhoneNumber = false;
                // User settings
                options.User.RequireUniqueEmail = false;
            })
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddTransient<IEmailSender, EmailSender>();
            services.AddMvc();

            services.AddIdentityServer()
                .AddDeveloperSigningCredential()
                // .AddSigningCredential(new X509Certificate2(@"D:\Projects\test\socialnetwork.pfx", "password"))
                .AddConfigurationStore(options =>
                {
                    options.ConfigureDbContext = builder =>
                        builder.UseSqlServer(connectionString,
                            sql => sql.MigrationsAssembly(migrationsAssembly));
                })
                .AddOperationalStore(options =>
                {
                    options.ConfigureDbContext = builder =>
                        builder.UseSqlServer(connectionString,
                            sql => sql.MigrationsAssembly(migrationsAssembly));
                    options.EnableTokenCleanup = true;
                    options.TokenCleanupInterval = 30;
                })
                .AddAspNetIdentity<ApplicationUser>();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.InitializeDatabase();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();
            app.UseIdentityServer();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

注意在Configure方法里面不要使用app.UseAuthentication(), 因为app.UseIdentityServer()方法已经包含了这个中间件. 然后使用命令行执行:

dotnet ef database update

或者在Packge Manager Console执行 update-database也行.

我照着官方文档操作出现了一些问题, 有几个重复的controller, 因为项目建立好之后有个HomeController和AccountController, 而使用Quickstart UI里面也有这两个Controller.

所以我最后clone了官方的例子:  https://github.com/IdentityServer/IdentityServer4.Samples/tree/dev/Quickstarts/6_AspNetIdentity

修改了一下, 放到了我这个项目里: https://github.com/solenovex/Learning-Identity-Server-4

其他

有的项目可能需要使用第三方登陆, 例如使用Google账户, 微软账户, QQ等, 这部分请看官方文档自行学习吧. 我要做的是企业内部项目. 所以这块先不研究了.

也有可能会使用Auth0, Stormpath这样的OAuth Provider, Auth0我用过, 登陆有点慢, 但功能很强大. 这个也不讲了, 他们的文档写的很好, 也给出了各种客户端的代码, 很容易集成.

Javascript 客户端

这将是最后一部分.

这个要等到我把项目的angular5客户端基础框架搭建好之后才能写.

先告一段落

时间: 2024-08-30 16:54:58

使用Identity Server 4建立Authorization Server (5)的相关文章

使用Identity Server 4建立Authorization Server (1)

预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 本文内容基本完全来自于Identity Server 4官方文档: https://identityserver4.readthedocs.io/ 官方文档很详细的. 使用OAuth可以更安全, 这里我们的authorization server和web api 以及网站将分别独立运行. 建立authorization server 建立asp.net core 项目使用空模板. 项目建立后, 运行

使用Identity Server 4建立Authorization Server (2)

第一部分: http://www.cnblogs.com/cgzl/p/7780559.html 第一部分主要是建立了一个简单的Identity Server. 接下来继续: 建立Web Api项目 如图可以在同一个解决方案下建立一个web api项目: (可选)然后修改webapi的launchSettings.json, 我习惯使用控制台, 所以把IISExpress相关的都删掉, 并且把端口改成5001: { "profiles": { "WebApi": {

OAuth2、OpenID Connect、IdentityServer建立Authorization Server简介

当我们在登录一些网站的时候,需要第三方的登录.比如,现在我们要登录简书https://www.jianshu.com/sign_in,我们使用微博登录,点击下方的一个微博的小按钮,就会出现这么一个地址https://api.weibo.com/oauth2/authorize?client_id=1881139527&redirect_uri=http%3A%2F%2Fwww.jianshu.com%2Fusers%2Fauth%2Fweibo%2Fcallback&response_ty

五、WebApi建立authorization server

一.服务端建立authorization server 1.安装 Install-Package identityserver4 2.编写配置类 //一.IDS4服务制定 public class Config { //1.定义API资源 public static IEnumerable<ApiResource> GetApis() //ApiResource是属于using IdentityServer4.Models;内的. { return new List<ApiResourc

续IDS4建立Authorization server和Client

一.准备 创建一个名为QuickstartIdentityServer的ASP.NET Core Web 空项目(asp.net core 2.2),端口5000创建一个名为Api的ASP.NET Core Web Api 项目(asp.net core 2.2),端口5001 二.定义服务端配置 1.NuGet命令行 NuGet命令行:Install-Package IdentityServer4 2.在QuickstartIdentityServer项目中添加一个Config.cs文件: u

OWIN OAuth 2.0 Authorization Server

OWIN OAuth 2.0 Authorization Server 源码在上面的地址中可以下载 打开客户端页面http://localhost:38500/ 客户端代码引用了DotNetOpenAuth.OAuth2 public class HomeController : Controller { private WebServerClient _webServerClient; public ActionResult Index() { ViewBag.AccessToken = Re

Lost connection to MySQL server at &#39;reading authorization packet&#39; 处理解决办法

Mysql Version : 5.1.73 Os Version : Centos 5.9 问题叙述: 今天一个php程序从客户端连接Mysql被监测到在偶尔的连接过程中报错,信息如下: Lost connection to MySQL server at 'reading authorization packet',看到这个错误提示,第一想到是客户端连接不上,服务端本地是否能连接.经验证,服务端本地是可以连接的,这就松了口气,一方面有很多任务是跑在Mysql服务端的,如果本地无法连接,会造成

SQL Server中建立外键的方法

在SQL中建立外键约束,可以级联查询表中的数据,在C#代码生成器中,也能根据外键关系生成相应的外键表数据模型.外键也可防止删除有外键关系的记录,一定程度上保护了数据的安全性. 步骤: 1.要建立外键关系,首先要保证用来建立外键关系的列具有唯一性,即具有 UNIQUE 约束通常是某表的主键作为另外一个表的外键 2.打开数据库表,找到要建立外键的表.并确保其中要建立外键关系的列与主键表中的数据类型完全一致 3.在要建立外键关系的表中,在任意列上右击,选择[关系] 4.在外键关系对话框中,点击左下角的

通过作业调度建立SQL Server的自动备份

SQL Server的作业调度来建立自动备份的方法 ◆1.进入企业管理器中->管理->sql server代理->作业; ◆2.新建作业,作业名称随便取,例如:data备份,所有者选择sa,当然你也可以选择其他用户,前提是该用户有执行作业的权限; ◆3.点击步骤标签,进入步骤面板.新建步骤,步骤名可以随便填写,如步骤1,类型和数据库默认,不需要修改.命令中写入以下语句: BACKUP DATABASE [数据库名] TO DISK = N''F:data数据库备份'' WITH NOIN