授权服务器设置  

  1. 添加用户

  添加测试用户,也可以从数据库查  

      

  1. public static List<TestUser> GetTestUser()
  2. {
  3. return new List<TestUser>() {
  4. new TestUser(){
  5. SubjectId = "",
  6. Username ="zps",
  7. Password = "zps",
  8. Claims = new List<Claim>(){
  9. new Claim("role","zps"),
  10. new Claim("aaa","asdasdsd"),
  11. }
  12. },
  13. new TestUser(){
  14. SubjectId = "",
  15. Username ="admin",
  16. Password = "admin",
  17. Claims = new List<Claim>(){
  18. new Claim("role","admin")
  19. }
  20. }
  21. };
  22. }
  1. 添加Api资源                                                                                                                            

添加api资源 ,api的key要和注册的client的api要匹配

  1. public static IEnumerable<ApiResource> GetResource()
  2. {
  3. return new List<ApiResource>(){
  4. new ApiResource("api","my api")
  5. };
  6. }
  1. 添加客户端
  1. 客户端模式
  2. 密码模式
  3. 授权码模式
  4. 混合模式

   授权码模式和mvc模式的时候    这两个模式先不管

         //请求确认

  1. RequireConsent = false, 这个属性要注意 如果是true 会先跳转到确认页面 然后再跳转到RedirectUris
  1.  
  1. public static IEnumerable<Client> GetClients()
  2. {
  3. return new List<Client>(){
  4. new Client(){
  5. ClientId="client",
  6. //客户端模式
  7. AllowedGrantTypes=GrantTypes.ClientCredentials,
  8. ClientSecrets={new Secret("secret".Sha256())},
  9. AllowedScopes={"api"}
  10. },
  11. new Client(){
  12. ClientId="pwdClient",
  13. //OAuth密码模式
  14. AllowedGrantTypes=GrantTypes.ResourceOwnerPassword,
  15. ClientSecrets={new Secret("secret".Sha256())},
  16. AllowedScopes={"api"}
  17. },
  18. new Client
  19. {
  20. ClientId = "mvc",
  21. ClientName = "MVC Client",
  22. AllowedGrantTypes = GrantTypes.Hybrid,
  23. ClientSecrets =
  24. {
  25. new Secret("secret".Sha256())
  26. },
  27. // where to redirect to after login
  28. RedirectUris = { "http://localhost:5001/signin-oidc" },
  29. RequireConsent = false,
  30. AllowOfflineAccess = true,
  31. // where to redirect to after logout
  32. PostLogoutRedirectUris = { "http://localhost:5001/signout-callback-oidc" },
  33.  
  34. AllowedScopes = new List<string>
  35. {
  36. IdentityServerConstants.StandardScopes.OpenId,
  37. IdentityServerConstants.StandardScopes.Profile,
  38. }
  39. },
  40. new Client
  41. {
  42. ClientId = "js",
  43. ClientName = "JavaScript Client",
  44. AllowedGrantTypes = GrantTypes.Code,
  45. RequirePkce = true,
  46. RequireClientSecret = false,
  47.  
  48. RedirectUris = { "http://localhost:5003/callback.html" },
  49. PostLogoutRedirectUris = { "http://localhost:5003/index.html" },
  50. AllowedCorsOrigins = { "http://localhost:5003" },
  51. RequireConsent = false,
  52. AllowedScopes =
  53. {
  54. IdentityServerConstants.StandardScopes.OpenId,
  55. IdentityServerConstants.StandardScopes.Profile,
  56. "api"
  57. }
  58. }
  59. };
  60. }
  1. 添加IdentityServer 保护的资源

可以自定义Claim

  1. public static IEnumerable<IdentityResource> GetIdentityResources()
  2. {
  3. return new IdentityResource[]
  4. {
  5. new IdentityResources.OpenId(),
  6. new IdentityResources.Profile(),
  7. };
  8. }
  1. identityserver注入到容器

  .AddDeveloperSigningCredential() 生成token 需要的密钥和公钥  正式环境需要换成正经的

o.UserInteraction.LoginUrl = "/Auth/Login";

o.UserInteraction.LogoutUrl = "/Auth/Logout";

  1. o.UserInteraction.ErrorUrl = "/Auth/Error";
    这三个是混合模式需要的 登录的地址 登出的地址 授权失败的地址
  1. services.AddIdentityServer(o =>
  2. {
  3. o.UserInteraction.LoginUrl = "/Auth/Login";
  4. o.UserInteraction.LogoutUrl = "/Auth/Logout";
  5. o.UserInteraction.ErrorUrl = "/Auth/Error";
  6. })
  7. .AddInMemoryIdentityResources(Config.GetIdentityResources())
  8. .AddDeveloperSigningCredential()
  9. .AddInMemoryClients(Config.GetClients())
  10. .AddInMemoryApiResources(Config.GetResource())
  11. .AddTestUsers(Config.GetTestUser());

  Configure把中间件加到netcore中

  1. app.UseIdentityServer();

postman测试

  1.   grant-type:密码模式对应 password
  2. username 用户名
  3. password  密码
  4. client_id 客户端id  对应 授权服务ClientId
  5. client_secret  客户端secret

源码

IdentityServer4 密码模式认证的更多相关文章

  1. IdentityServer4 密码模式实现

    1.  修改 Config.cs using System.Collections; using System.Collections.Generic; using IdentityServer4.M ...

  2. IdentityServer4密码模式接入现有用户数据表

    具体接入identityserver请看文档,这里只简单列举部分步骤 1.创建一个web项目,引入Identityserver4的nuget包 2.新建一个类,实现IResourceOwnerPass ...

  3. Core篇——初探IdentityServer4(客户端模式,密码模式)

    Core篇——初探IdentityServer4(客户端模式,密码模式) 目录 1.Oatuth2协议的客户端模式介绍2.IdentityServer4客户端模式实现3.Oatuth2协议的密码模式介 ...

  4. asp.net权限认证:OWIN实现OAuth 2.0 之密码模式(Resource Owner Password Credential)

    asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...

  5. asp.net core 使用identityServer4的密码模式来进行身份认证(一)

    IdentityServer4是ASP.NET Core的一个包含OpenID和OAuth 2.0协议的框架.具体Oauth 2.0和openId请百度. 前言本博文适用于前后端分离或者为移动产品来后 ...

  6. 【ASP.NET Core分布式项目实战】(一)IdentityServer4登录中心、oauth密码模式identity server4实现

    本博客根据http://video.jessetalk.cn/my/course/5视频整理 资料 OAuth2 流程:http://www.ruanyifeng.com/blog/2014/05/o ...

  7. IdentityServer4 实现OAuth2.0四种模式之密码模式

    接上一篇:IdentityServer4 实现OAuth2.0四种模式之客户端模式,这一篇讲IdentityServer4 使用密码模式保护API访问. 一,IdentityServer配置 1,添加 ...

  8. OAuth2密码模式已死,最先进的Spring Cloud认证授权方案在这里

    旧的Spring Security OAuth2停止维护已经有一段时间了,99%的Spring Cloud微服务项目还在使用这些旧的体系,严重青黄不接.很多同学都在寻找新的解决方案,甚至还有念念不忘密 ...

  9. IdentityServer4:IdentityServer4+API+Client+User实践OAuth2.0密码模式(2)

    一.密码模式实操 仍然使用第一节的代码:做如下改动: 1.授权服务端 前面我们使用项目:Practice.IdentityServer作为授权服务器 修改项目的Config.cs类: 添加测试用户,并 ...

随机推荐

  1. 使用css实现无滚动条滚动+使用插件自定义滚动条样式

    使用css实现无滚动条滚动,摘抄自:曹小萌博客 使用css实现无滚动条滚动,大体思路是在div外面再套一个div.这个div设置overflow:hidden.而内容div设置 overflow-x: ...

  2. [ZJOI2019]麻将(动态规划,自动机)

    [ZJOI2019]麻将(动态规划,自动机) 题面 洛谷 题解 先做一点小铺垫,对于一堆牌而言,我们只需要知道这\(n\)张牌分别出现的次数就行了,即我们只需要知道一个长度为\(n\)的串就可以了. ...

  3. <数据结构基础学习>(四)链表 Part 1

    一.链表基础 动态数组.栈.队列底层都是依托静态数组实现的,靠resize来解决固定容量问题. 链表是真正的动态数据结构,是一种最简单的一种动态数据结构. 更深入的理解引用(或者指针). 更深入的理解 ...

  4. Linux-Centos破解安装confluene6.3.1

    Centos 安装企业wiki confluence是一个专业的企业知识管理与协同软件,可以用于构建企业wiki.通过它可以实现团队成员之间的协作和知识共享.现在大多数公司都会部署一套confluen ...

  5. NEED TO DO

    任务清单 计算几何  KDtree  容斥  后缀自动机套数据结构 FFT  四边形不等式/决策单调性优化  欧拉路 KM算法  BM算法  数论 min25筛  后缀数组 吉司机线段树 生成函数  ...

  6. ElasticSearch6.5.0 【Java客户端之REST Client】

    说明 High Level Client 是基于 Low Level Client 的.官方文档如下: * https://www.elastic.co/guide/en/elasticsearch/ ...

  7. 自动化运维工具Ansible介绍

    一个由 Python 编写的强大的配置管理解决方案.尽管市面上已经有很多可供选择的配置管理解决方案,但他们各有优劣,而 ansible 的特点就在于它的简洁. 让 ansible 在主流的配置管理系统 ...

  8. 使用vlc打开usb摄像头

    打开vlc播放器 可以打开网络串流的方式打开摄像头,但只支持第一个摄像头 这一串地址拼凑方法看下面,下面可以选择摄像头 为什么只支持第一个摄像头可以参考下一篇使用Vlc.DotNet打开摄像头并截图 ...

  9. shell 通过EOF在脚本中输入需要的用户名或密码

    参考地址:https://www.cnblogs.com/liyuanhong/p/10390786.html expect使用参考:https://www.cnblogs.com/liyuanhon ...

  10. [再寄小读者之数学篇](2014-06-20 Beta 函数)

    令 $\dps{B(m,n)=\sum_{k=0}^n C_n^k \cfrac{(-1)^k}{m+k+1}}$, $m,n\in\bbN^+$. (1) 证明 $B(m,n)=B(n,m)$; ( ...