• Asp.net core 学习笔记 ( identity server 4 JWT Part )


    更新 : 

    id4 使用这个 DbContext 哦

    dotnet ef migrations add identity-server-init --context PersistedGrantDbContext

    参考

    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

    identity server 4 (简称 ids) 是一个做认证和授权的小工具. 

    asp.net core 自带的 identity 虽然蛮牛的,但是基于 cookies 不适合手机开发。

    与其说手机开发,干脆点事不适合前后端分离的开发模式. 

    前后端分离是做 web application 或 mobile application 时常用的手法. 

    显然 asp.net core identity 目前还是偏向后端输出 html 的方案,而不是基于 Web Api.

    如果开发 Web Application 通常项目会比较复杂,前后端分离,Web Api 就需要 access token (Json Web Token)

    这类的方式, 还有授权也是一样, 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:keatkeatself-signed-certificateestway.pfx"
    $CerFilePath = "c:keatkeatself-signed-certificateestway.cer"
    $DNSName = "localhost"
    $Password ="Password"
    $friendlyName = "bestway"
    $SecStringPw = ConvertTo-SecureString -String $Password -Force -AsPlainText
    $Cert = New-SelfSignedCertificate -KeyFriendlyName $friendlyName -FriendlyName $friendlyName -DnsName $DNSName -CertStoreLocation "cert:LocalMachineMy" -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:keatkeatself-signed-certificate	est-id4-four.pfx", "password"))
                   //.AddValidationKey(new X509Certificate2(@"C:keatkeatself-signed-certificate	est-id4.pfx", "password"))
                   //.AddDeveloperSigningCredential()
                   .AddInMemoryApiResources(Config.GetApiResources())
                   .AddInMemoryClients(Config.GetClients());

    run -> certmgr.msc 管理证书

  • 相关阅读:
    reids学习redis的五种基本数据类型
    CentOS下配置Java开发环境安装redis
    CentOS下配置Java开发环境安装Tomcat
    如何去除掉inlineblock元素之间的默认间距
    js按值传递还是按引用传递?
    overflow: hidden用法,不仅仅是隐藏溢出
    关于WinForm中的DataGridView控件显示数据字典的解决方案。
    C#中对Winform中的DataGridView的控制技巧。(单独控制某单元格的按钮不显示、某单元格的ReadOnly)
    改进版网页表格的合并单元格(支持不连续的列合并)
    [共享]MT随机算法(符合正态分布的随机算法)
  • 原文地址:https://www.cnblogs.com/keatkeat/p/9326389.html
Copyright © 2020-2023  润新知