授权服务器设置  

添加用户

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

      

public static List<TestUser> GetTestUser()
{
return new List<TestUser>() {
new TestUser(){
SubjectId = "",
Username ="zps",
Password = "zps",
Claims = new List<Claim>(){
new Claim("role","zps"),
new Claim("aaa","asdasdsd"),
}
},
new TestUser(){
SubjectId = "",
Username ="admin",
Password = "admin",
Claims = new List<Claim>(){
new Claim("role","admin")
}
}
};
}
添加Api资源                                                                                                                            

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

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

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

         //请求确认

               RequireConsent = false,   这个属性要注意  如果是true  会先跳转到确认页面 然后再跳转到RedirectUris

 public static IEnumerable<Client> GetClients()
{
return new List<Client>(){
new Client(){
ClientId="client",
//客户端模式
AllowedGrantTypes=GrantTypes.ClientCredentials,
ClientSecrets={new Secret("secret".Sha256())},
AllowedScopes={"api"}
},
new Client(){
ClientId="pwdClient",
//OAuth密码模式
AllowedGrantTypes=GrantTypes.ResourceOwnerPassword,
ClientSecrets={new Secret("secret".Sha256())},
AllowedScopes={"api"}
},
new Client
{
ClientId = "mvc",
ClientName = "MVC Client",
AllowedGrantTypes = GrantTypes.Hybrid,
ClientSecrets =
{
new Secret("secret".Sha256())
},
// where to redirect to after login
RedirectUris = { "http://localhost:5001/signin-oidc" },
RequireConsent = false,
AllowOfflineAccess = true,
// where to redirect to after logout
PostLogoutRedirectUris = { "http://localhost:5001/signout-callback-oidc" }, AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
}
},
new Client
{
ClientId = "js",
ClientName = "JavaScript Client",
AllowedGrantTypes = GrantTypes.Code,
RequirePkce = true,
RequireClientSecret = false, RedirectUris = { "http://localhost:5003/callback.html" },
PostLogoutRedirectUris = { "http://localhost:5003/index.html" },
AllowedCorsOrigins = { "http://localhost:5003" },
RequireConsent = false,
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"api"
}
}
};
}
添加IdentityServer 保护的资源

可以自定义Claim

 public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new IdentityResource[]
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
};
}
把identityserver注入到容器

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

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

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

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

  Configure把中间件加到netcore中

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. 2.6 datetime 模块

    目录 2.6.1  常用类 2.6.1.1 datetime.date 2.6.1.2 datetime.time 2.6.1.3 datetime.datetime 2.6.1.4 datetime ...

  2. [manjaro]换源到中国并按照速度排序

    sudo pacman-mirrors -i -c China -m rank 勾选第一行的源.

  3. python之OpenCv(五)---抓取摄像头视频图像

    OpenCV 可以通过 头videoCapture()方法打开摄像 摄像头变量 = cv2.VideoCapture(n)   n为整数,内置摄像头为0,若有其他摄像头则依次为1,2,3,4,... ...

  4. Python变量命名规范

    模块名: 小写字母,单词之间用_分割 ad_stats.py 包名: 和模块名一样 类名: 单词首字母大写 AdStats ConfigUtil 全局变量名(类变量,在java中相当于static变量 ...

  5. java BIO/NIO/AIO 学习

    一.了解Unix网络编程5种I/O模型 1.1.阻塞式I/O模型 阻塞I/O(blocking I/O)模型,进程调用recvfrom,其系统调用直到数据报到达且被拷贝到应用进程的缓冲区中或者发生错误 ...

  6. 深入jar包:从jar包中读取资源文件getResourceAsStream

    一.背景 我们常常在代码中读取一些资源文件(比如图片,音乐,文本等等). 在单独运行的时候这些简单的处理当然不会有问题.但是,如果我们把代码打成一个jar包以后,即使将资源文件一并打包,这些东西也找不 ...

  7. phpstorm快捷键大全

    前言:这段时间换了编辑器,所以挺多命令也改变了 转载来自:https://www.jianshu.com/p/ffb24d61000d?utm_campaign=maleskine&utm_c ...

  8. vsftpd启动问题简记

    centos7 能以ipv6方式启动,启动只需修改配置如下 如需同时启动到ipv4跟ipv6,需拷贝配置文件,一份配置中只监听ipv4,一份配置中只监听ipv6 centos6中无法启动到ipv6,错 ...

  9. 浏览器录宏重放软件-iMacros

    iMacros https://imacros.net/ iMacros v12 Now Available The world's most popular web automation, data ...

  10. luogu 4345 Lucas的变形应用

    求 sigma i由0-k C(n,i) 利用Lucas定理+整除分块将C(n/p,i/p)利用i/p分块,得到k/p-1个整块(p-1)和一个小块(k%p) 最后得到式子 F(n,k)=F(n/p, ...