IdentityServer4 密码模式实现
1. 修改 Config.cs
using System.Collections;
using System.Collections.Generic;
using IdentityServer4.Models;
using IdentityServer4.Test;//测试的时候使用 namespace IdentityServiceSample
{
public class Config
{
public static IEnumerable<ApiResource> GetResource()
{ return new List<ApiResource>(){
new ApiResource("api","my api")
}; } 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"}
}
};
} //TODO 实际使用可以读取Db
public static List<TestUser> GetTestUser()
{
return new List<TestUser>{
new TestUser{
SubjectId="",
Username="sunxuchu",
Password=""
}
};
}
}
}
2. 修改 Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using IdentityServer4; namespace IdentityServiceSample
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//1.注入IdentityServer
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetResource())
.AddInMemoryClients(Config.GetClients())
.AddTestUsers(Config.GetTestUser()); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
} // app.UseHttpsRedirection();
// app.UseMvc(); //2. 注册IdentityServer
app.UseIdentityServer();
}
}
}
3. 使用PostMan 调用调试

请求参数:
client_id:pwdClient
client_secret:secret
grant_type:password
password:123456
username:sunxuchu
4. 密码模式HttpClient 模拟请求
using System;
using System.Net.Http;
using IdentityModel.Client; namespace PwdClient
{
class Program
{
static void Main(string[] args)
{
try
{
new Program().GetAsync();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
} Console.ReadKey();
} public async void GetAsync()
{
var diso = await DiscoveryClient.GetAsync("http://localhost:5003");
if (diso.IsError)
{
System.Console.WriteLine("diso.Error");
}
var tokenClient = new TokenClient(diso.TokenEndpoint, "pwdClient", "secrt");
var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync("sunxuchu","");
if (tokenResponse.IsError)
{
System.Console.WriteLine(tokenResponse.Error);
}
else
{
System.Console.WriteLine(tokenResponse.Json);
} using (var httpClient = new HttpClient())
{
httpClient.SetBearerToken(tokenResponse.AccessToken);
var response = await httpClient.GetAsync("http://localhost:5001/api/values");
if (response.IsSuccessStatusCode)
{
System.Console.WriteLine(await response.Content.ReadAsStringAsync());
}
} }
}
}
IdentityServer4 密码模式实现的更多相关文章
- IdentityServer4 密码模式认证
授权服务器设置 添加用户 添加测试用户,也可以从数据库查 public static List<TestUser> GetTestUser() { return new List< ...
- IdentityServer4密码模式接入现有用户数据表
具体接入identityserver请看文档,这里只简单列举部分步骤 1.创建一个web项目,引入Identityserver4的nuget包 2.新建一个类,实现IResourceOwnerPass ...
- Core篇——初探IdentityServer4(客户端模式,密码模式)
Core篇——初探IdentityServer4(客户端模式,密码模式) 目录 1.Oatuth2协议的客户端模式介绍2.IdentityServer4客户端模式实现3.Oatuth2协议的密码模式介 ...
- 【ASP.NET Core分布式项目实战】(一)IdentityServer4登录中心、oauth密码模式identity server4实现
本博客根据http://video.jessetalk.cn/my/course/5视频整理 资料 OAuth2 流程:http://www.ruanyifeng.com/blog/2014/05/o ...
- asp.net core 使用identityServer4的密码模式来进行身份认证(一)
IdentityServer4是ASP.NET Core的一个包含OpenID和OAuth 2.0协议的框架.具体Oauth 2.0和openId请百度. 前言本博文适用于前后端分离或者为移动产品来后 ...
- IdentityServer4 实现OAuth2.0四种模式之密码模式
接上一篇:IdentityServer4 实现OAuth2.0四种模式之客户端模式,这一篇讲IdentityServer4 使用密码模式保护API访问. 一,IdentityServer配置 1,添加 ...
- IdentityServer4:IdentityServer4+API+Client+User实践OAuth2.0密码模式(2)
一.密码模式实操 仍然使用第一节的代码:做如下改动: 1.授权服务端 前面我们使用项目:Practice.IdentityServer作为授权服务器 修改项目的Config.cs类: 添加测试用户,并 ...
- IdentityServer4授权模式应用场景
OpenID 和 OAuth 的区别 IdentityServer4,NET Core下的安全框架 客户端模式(Client Credentials) 密码模式(resource owner pass ...
- ASP.NET Core分布式项目-2.oauth密码模式identity server4实现
源码下载 这里根据<ASP.NET Core分布式项目-1.IdentityServer4登录中心>的代码来继续更新oauth密码模式,这里的密码模式比上次的客户端模式更安全 在WebAp ...
随机推荐
- Debian use sudo
刚安装好的Debian默认还没有sudo功能.1.安装sudo# apt-get install sudo2.编辑 /etc/sudoers ,添加如下行# visudoroot ALL=(ALL:A ...
- PhpStorm 2017 激活办法
最新版PhpStorm 2017正式版改进了PHP 7支持,改进代码完成功能.PhpStorm 绝对是PHP开发最好的开发工具. phpstorm已经升级到2017.1,原注册码失效,2017.1.2 ...
- Hadoop 系列(二)安装配置
Hadoop 系列(二)安装配置 Hadoop 官网:http://hadoop.apache.or 一.Hadoop 安装 1.1 Hadoop 依赖的组件 JDK :从 Oracle 官网下载,设 ...
- msys2 启用windows PATH环境变量
有三种方法修改 ①msys2_shell.cmd 中取消一行的注释:set MSYS2_PATH_TYPE=inherit ②调用msys2_shell.cmd时使用-use-full-path参数 ...
- [精彩] 关于DB2的内存分配
这两天在看DB2的内存管理的内容,看的很是模糊,有以下问题不明白,请教 是不是数据库管理器的共享内存就是DB2能够使用的最大内容呢,然后数据库全局内存从管理器内存那里获得分配的内存,然后应用程序全局内 ...
- python 线程和进程概述
计算机中执行任务的最小单元:线程 IO操作利用CPU GIL,全局解释器锁 IO密集型: 多线程(不用CPU) 计算机密集型(用CPU) 进程和线程的目的:提高执行效率 1.单进程单线程,主进程.主线 ...
- 卡特尔16PF性格测试与答案
大学生在职业生涯规划时,必须充分注意到自己的性格和职业的适宜性.性格是指一个人在生活中形成的对现实的稳定的态度和行为方式.研究表明,性格影响着一个人的职业取向,由于性格的不同,每个人对工作和职业的态度 ...
- 2018.10.15 loj#6013. 「网络流 24 题」负载平衡(费用流)
传送门 费用流sb题. 直接从sss向每个点连边,容量为现有物品量. 然后从ttt向每个点连边,容量为最后库存量. 由于两个点之间可以互相任意运送物品,因此相邻的直接连infinfinf的边就行了. ...
- EasyUI 让dialog中的treegrid的列头固定
先上效果: 最主要是在treegrid要加上"fit:true "如果不加那么就会用diglog的滚动条,导致treegrid的头就没办法固定. Code<div id=&q ...
- LeetCode146:LRU Cache
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...