https://identityserver4.readthedocs.io/en/release/quickstarts/8_entity_framework.html 此连接的实践

vscode 下面命令

dotnet new webapi -o is4ef
cd is4ef
dotnet add package IdentityServer4.EntityFramework
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools

增加Config.cs

 // Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. using IdentityServer4;
using IdentityServer4.Models;
using IdentityServer4.Test;
using System.Collections.Generic;
using System.Security.Claims; namespace is4ef
{
public class Config
{
// scopes define the resources in your system
public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
};
} public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("api1", "My API")
};
} // clients want to access resources (aka scopes)
public static IEnumerable<Client> GetClients()
{
// client credentials client
return new List<Client>
{
new Client
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.ClientCredentials, ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api1" }
}, // resource owner password grant client
new Client
{
ClientId = "ro.client",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api1" }
}, // OpenID Connect hybrid flow and client credentials client (MVC)
new Client
{
ClientId = "mvc",
ClientName = "MVC Client",
AllowedGrantTypes = GrantTypes.HybridAndClientCredentials, ClientSecrets =
{
new Secret("secret".Sha256())
}, RedirectUris = { "http://localhost:5002/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" }, AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"api1"
},
AllowOfflineAccess = true
}
};
} public static List<TestUser> GetUsers()
{
return new List<TestUser>
{
new TestUser
{
SubjectId = "",
Username = "alice",
Password = "password", Claims = new List<Claim>
{
new Claim("name", "Alice"),
new Claim("website", "https://alice.com")
}
},
new TestUser
{
SubjectId = "",
Username = "bob",
Password = "password", Claims = new List<Claim>
{
new Claim("name", "Bob"),
new Claim("website", "https://bob.com")
}
}
};
}
}
}

Startup.cs->ConfigureServices

using Microsoft.EntityFrameworkCore;
using System.Reflection;
 const string connectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;database=IdentityServer4.Quickstart.EntityFramework-2.0.0;trusted_connection=yes;";
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name; // configure identity server with in-memory stores, keys, clients and scopes
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddTestUsers(Config.GetUsers())
// this adds the config data from DB (clients, resources)
.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 = ;
});

然后是生成数据库映像文件

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

然后是插入种子数据

 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 Config.GetClients())
{
context.Clients.Add(client.ToEntity());
}
context.SaveChanges();
} if (!context.IdentityResources.Any())
{
foreach (var resource in Config.GetIdentityResources())
{
context.IdentityResources.Add(resource.ToEntity());
}
context.SaveChanges();
} if (!context.ApiResources.Any())
{
foreach (var resource in Config.GetApiResources())
{
context.ApiResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
}
}
Startup.cs->Configure 配置注入
 InitializeDatabase(app);

最后还有两个要引用

using IdentityServer4.EntityFramework.DbContexts;
using IdentityServer4.EntityFramework.Mappers;
 

webapi core2.1 IdentityServer4.EntityFramework Core进行配置和操作数据的更多相关文章

  1. webapi core2.1 Identity.EntityFramework Core进行配置和操作数据 (一)没什么用

    https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-2.1&am ...

  2. 第15章 使用EntityFramework Core进行配置和操作数据 - Identity Server 4 中文文档(v1.0.0)

    IdentityServer旨在实现可扩展性,其中一个可扩展点是用于IdentityServer所需数据的存储机制.本快速入门展示了如何配置IdentityServer以使用EntityFramewo ...

  3. IdentityServer(14)- 通过EntityFramework Core持久化配置和操作数据

    本文用了EF,如果不适用EF的,请参考这篇文章,实现这些接口来自己定义存储等逻辑.http://www.cnblogs.com/stulzq/p/8144056.html IdentityServer ...

  4. mvc core2.1 Identity.EntityFramework Core 实例配置 (四)

    https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/customize_identity_model?view=a ...

  5. mvc core2.1 Identity.EntityFramework Core 配置 (一)

    https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/customize_identity_model?view=a ...

  6. mvc core2.1 Identity.EntityFramework Core ROle和用户绑定查看 (八)完成

    添加角色属性查看 Views ->Shared->_Layout.cshtml <div class="navbar-collapse collapse"> ...

  7. mvc core2.1 Identity.EntityFramework Core 用户Claims查看(七)

    添加角色属性查看 Views ->Shared->_Layout.cshtml <div class="navbar-collapse collapse"> ...

  8. mvc core2.1 Identity.EntityFramework Core 导航状态栏(六)

    之前做的无法 登录退出,和状态,加入主页导航栏 Views ->Shared->_Layout.cshtml <div class="navbar-collapse col ...

  9. mvc core2.1 Identity.EntityFramework Core 用户列表预览 删除 修改 (五)

    用户列表预览 Controllers->AccountController.cs [HttpGet] public IActionResult Index() { return View(_us ...

随机推荐

  1. mac nginx+php-fpm配置(安装过后nginx后访问php文件下载,访问php文件请求200显示空白页面)

    访问php文件下载是因为没配置php-fpm 两个问题主要都是nginx.conf配置的问题: /usr/local/etc/nginx/nginx.conf server {    listen 8 ...

  2. GitHub下的文件放到Linux系统下

    1.在GitHub账号下clone URL 项目. 2.到Linux服务器下执行以下操作: (1)  mkdir test (2)  cd test/ (3)  git clone  复制的项目URL

  3. linux command useradd

    Linux command useradd [Purpose]        Learning linux command useradd to create a new user or update ...

  4. WPF技术实现控件截图

    1.http://www.cnblogs.com/TianFang/archive/2012/10/07/2714140.html 2.http://www.silverlightchina.net/ ...

  5. python 类方法中参数使用默认值的方法

    class A(): __init__(self, **arg): self.__dict__.update(arg)        def M(self, config=None, section= ...

  6. mysql 数据迁移

    最近线上系统新挂了一次磁盘,需要将系统磁盘下的 mysql 数据目录迁移到 数据盘上. 经过一番考察,mysql在安装时,使用了预编译的二进制tar.gz包.共有两处配置了 datadir属性 /et ...

  7. 每天CSS学习之!important

    通过在CSS属性后面跟上!important参数,就会让该属性的优先级变为最高,不管是在css文件,还是内联style中,就属后面跟了!important这个小弟的属性的优先级最高. 让我们来测试一下 ...

  8. Linux学习 :多线程编程

    1.Linux进程与线程() 进程:通过fork创建子进程与创建线程之间是有区别的:fork创建出该进程的一份拷贝,创建时额外申请了新的内存空间以及存储代码段.数据段.BSS段.堆.栈空间,     ...

  9. 201621123001 《Java程序设计》第14周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. Mysql数据库简单操作,常用的操作命令 启动:进入Mysql (从命令行mysql -u root -p) 退 ...

  10. <YaRN><Official doc><RM REST API's>

    Overview ... YARN Architecture The fundamental idea of YARN is to split up the functionalities of re ...