更新 : html
id4 使用这个 DbContext 哦程序员
dotnet ef migrations add identity-server-init --context PersistedGrantDbContext
参考web
https://docs.microsoft.com/en-us/azure/key-vault/key-vault-use-from-web-application#authenticate-with-a-certificate-instead-of-a-client-secret
https://docs.microsoft.com/en-us/powershell/module/pkiclient/new-selfsignedcertificate?view=win10-ps
https://identityserver4.readthedocs.io/en/release/topics/crypto.html#signing-key-rollover
https://mcguirev10.com/2017/12/23/easy-configuration-sharing-with-azure-key-vault.html
https://mcguirev10.com/2018/01/10/storing-certificates-azure-keyvault.html
https://mcguirev10.com/2018/01/04/localhost-ssl-identityserver-certificates.htmlshell
identity server 4 (简称 ids) 是一个作认证和受权的小工具. 后端
asp.net core 自带的 identity 虽然蛮牛的,可是基于 cookies 不适合手机开发。api
与其说手机开发,干脆点事不适合先后端分离的开发模式. 服务器
先后端分离是作 web application 或 mobile application 时经常使用的手法. cookie
显然 asp.net core identity 目前仍是偏向后端输出 html 的方案,而不是基于 Web Api.app
若是开发 Web Application 一般项目会比较复杂,先后端分离,Web Api 就须要 access token (Json Web Token)asp.net
这类的方式, 还有受权也是同样, oauth 2.0 也是常常会用到的.
ids 除了支持 oauth 2.0 还支持 OpenId Connect
咱们从前都是用 oauth 2.0 来干 Open Id 作的事情,因此忽然多了一个东西我开始的时候是以为很奇怪的。
慢慢明白, oauth 是用于受权, open id 则是认证.
认证之后,咱们一般是须要识别这我的的,好比 username profile claim 等等.
而 oauth 受权只是给你一个 access token 有能力去操做一些东西,可是并无直接告诉你用户。
而从前咱们是经过 access token + getUserProfile 这类的 api 才完成了如今 open id 作的事情。
因此有了 open id 职责就划分的更好了,更规范了。
上面说的整套 ids 都作好了, 但我目前的需求其实也只是要支持 token based 而已. 还不须要 oauth 2.0 或 openid
可是自带的 identity 目前又还不支持 token based, net core 2.2 貌似会有一个中间版本, 把 identity 增强支持 token based.
因此呢,我只能暂时拿 ids 大材小用了.
原本也考虑要本身写一个 JWT 的机制,可是后来想一想这样很傻,毕竟我是写业务的程序员,怎么还要干这么技术的事情呢... 对吧.
对比 identity cookies based, token based 有 2 点很不一样,
1. 刷新机制.
cookies based 咱们能够经过每一次请求,服务器 response 来让 cookie 保存新鲜. 可是基于 header 的 token based 彻底就没有办法作到这点。
因此必须弄多一个 refresh token 出来.
2. 加密机制
token based 一般是用 JWT 它是用非对称加密, 而 cookie based 一般是使用对称加密.
identity 内部使用了 asp.net core data protection 来加密
ids 则须要咱们去弄一个 self signed certificate.
ids 的过程是这样的。
有 3 个角色, 认证服务器(登入的地方), 资源服务器 (通常的 web api 站点), 客户端 (用户游览器)
用户想访问受保护的资源就必须先经过认证服务器登入和受权 (scope, claim 等),oauth 2.0, 4 中方式均可以.
获取到 token 以后访问资源服务器, 资源服务器会去认证服务器获取 document (里面会有认证服务器的资讯,好比 endpoint, 公钥等等), 而后资源服务器发起请求.
整套都是颇有规范的.
固然你要把认证服务器和资源服务器放一块儿也是能够的, 可是走的流程是同样的.
Self signed certificate
参考
https://docs.microsoft.com/en-us/azure/key-vault/key-vault-use-from-web-application#authenticate-with-a-certificate-instead-of-a-client-secret
https://docs.microsoft.com/en-us/powershell/module/pkiclient/new-selfsignedcertificate?view=win10-ps
https://identityserver4.readthedocs.io/en/release/topics/crypto.html#signing-key-rollover
https://mcguirev10.com/2017/12/23/easy-configuration-sharing-with-azure-key-vault.html
https://mcguirev10.com/2018/01/10/storing-certificates-azure-keyvault.html
https://mcguirev10.com/2018/01/04/localhost-ssl-identityserver-certificates.html
// start -> Windows PowerShell -> run as admin
$PfxFilePath = "c:\keatkeat\self-signed-certificate\bestway.pfx"
$CerFilePath = "c:\keatkeat\self-signed-certificate\bestway.cer"
$DNSName = "localhost"
$Password ="P@ssword2012"
$friendlyName = "bestway"
$SecStringPw = ConvertTo-SecureString -String $Password -Force -AsPlainText
$Cert = New-SelfSignedCertificate -KeyFriendlyName $friendlyName -FriendlyName $friendlyName -DnsName $DNSName -CertStoreLocation "cert:\LocalMachine\My" -NotAfter $((Get-Date).AddYears(50))
Export-PfxCertificate -cert $cert -FilePath $PFXFilePath -Password $SecStringPw
Export-Certificate -cert $cert -FilePath $CerFilePath
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); store.Open(OpenFlags.ReadOnly); //var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, "3de296d7b86ea9f6c2feec925134194ad54c09c6".ToUpper(), true); var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, "bb417dee655caebfc85f0ab448db54becc6a495c".ToUpper(), true); var certificate = certificates[0]; services.AddIdentityServer() .AddSigningCredential(certificate) //.AddSigningCredential(new X509Certificate2(@"C:\keatkeat\self-signed-certificate\test-id4-four.pfx", "password")) //.AddValidationKey(new X509Certificate2(@"C:\keatkeat\self-signed-certificate\test-id4.pfx", "password")) //.AddDeveloperSigningCredential() .AddInMemoryApiResources(Config.GetApiResources()) .AddInMemoryClients(Config.GetClients());
run -> certmgr.msc 管理证书