AspNetCore中的IdentityServer4客户端认证模式实现
1 AuthorizationServer
using IdentityServer4;
using IdentityServer4.Models; 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)
{
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetResource())
.AddInMemoryClients(Config.GetClients());
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();
} app.UseIdentityServer();
}
} 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"},
},
};
}
}
2 AspNetCore RequestClient
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using IdentityServer4;
namespace IdentityServer.Client
{
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)
{
services.AddAuthentication("Bearer").AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ApiName = "api";
});
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();
} app.UseAuthentication();
app.UseMvc();
}
}
}
3 Console Client
using System;
using System.Net;
using System.Net.Http;
using IdentityModel;
using IdentityModel.Client;
using static IdentityModel.OidcConstants; namespace ThirdPartyDemo
{
class Program
{
static void Main(string[] args)
{
var client = new HttpClient();
var result = client.GetDiscoveryDocumentAsync("http://localhost:5000").Result; var token = client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest()
{
Address = result.TokenEndpoint,
Scope = "api",
ClientId = "client",
ClientSecret = "secret",
GrantType = GrantTypes.ClientCredentials
}).Result; client.SetBearerToken(token.AccessToken); var r = client.GetAsync("http://localhost:5001/api/values").Result;
Console.WriteLine("Hello World!");
} }
}
AspNetCore中的IdentityServer4客户端认证模式实现的更多相关文章
- IdentityServer4(客户端授权模式)
1.新建三个项目 IdentityServer:端口5000 IdentityAPI:端口5001 IdentityClient: 2.在IdentityServer项目中添加IdentityServ ...
- 认证授权:IdentityServer4 - 各种授权模式应用
前言: 前面介绍了IdentityServer4 的简单应用,本篇将继续讲解IdentityServer4 的各种授权模式使用示例 授权模式: 环境准备 a)调整项目结构如下: b)调整cz.Id ...
- Core篇——初探IdentityServer4(OpenID Connect模式)
Core篇——初探IdentityServer4(OpenID Connect客户端验证) 目录 1.Oauth2协议授权码模式介绍2.IdentityServer4的OpenID Connect客户 ...
- IdentityServer(二)客户端授权模式
前言 客户端授权模,客户端直接向Identity Server申请token并访问资源.客户端授权模式比较适用于服务之间的通信. 搭建Identity服务 新建名为 IdentityServer 的W ...
- IdentityServer4(7)- 使用客户端认证控制API访问(客户端授权模式)
一.前言 本文已更新到 .NET Core 2.2 本文包括后续的Demo都会放在github:https://github.com/stulzq/IdentityServer4.Samples (Q ...
- IdentityServer4[3]:使用客户端认证控制API访问(客户端授权模式)
使用客户端认证控制API访问(客户端授权模式) 场景描述 使用IdentityServer保护API的最基本场景. 我们定义一个API和要访问API的客户端.客户端从IdentityServer请求A ...
- AspNetCore中使用Ocelot之 IdentityServer4(1)
AspNetCore中使用Ocelot之 IdentityServer4(1) 前言: OceLot网关是基于AspNetCore 产生的可扩展的高性能的企业级Api网关,目前已经基于2.0 升级版本 ...
- IdentityServer4 (1) 客户端授权模式(Client Credentials)
写在前面 1.源码(.Net Core 2.2) git地址:https://github.com/yizhaoxian/CoreIdentityServer4Demo.git 2.相关章节 2.1. ...
- IdentityServer4系列 | 客户端凭证模式
一.前言 从上一篇关于 快速搭建简易项目中,通过手动或者官方模板的方式简易的实现了我们的IdentityServer授权服务器搭建,并做了相应的配置和UI配置,实现了获取Token方式. 而其中我们也 ...
随机推荐
- 开源小程序CMS网站, JeeWx-App-CMS 1.1 版本升级发布,持续更新!
JeeWx-App-CMS开源小程序CMS网站,持续更新ing~ JeeWx-App-CMS 是jeewx开发的小程序网站开源项目,基于小程序wepy语言,具备cms网站的基本功能,能够打造简单易用的 ...
- springboot security
Authority 权限Credential 证书Grant 授予 Authentication 身份验证 以下,我们将通过四步,逐步实现spring-security的userna ...
- python杂记二
1. 写文件可以直接使用print函数 file_name = open("file_name.txt","w") print("file conta ...
- css background-image 自适应宽高——转载
就是这么简单的一句话,设置背景图,并让它100%的适应导航栏宽高,并设置不重复,大小100%就OK了 .zjhn-nav li.active a{ background-image:url(../im ...
- 3.搭建Samba服务器
1.在CentOS7上yum安装samba服务: yum -y install samba 2.目前我是直接共享已经存在的目录,如果您需要共享一个未曾创建的目录,需要您新创建一个目录: vim /et ...
- GDI+_从Bitmap里得到的Color数组值分解
[这里写只是我个人的摸索经历,已经给出解决方案] 我之前写过一篇关于ARGB值的研究:https://www.cnblogs.com/lingqingxue/p/10362639.html 最近我又遇 ...
- 前端笔记-javaScript-3
BOM对象 window对象 所有浏览器都支持 window 对象概念上讲.一个html文档对应一个window对象功能上讲: 控制浏览器窗口的使用上讲: window对象不需要创建对象,直接使用即可 ...
- 导弹拦截问题(DP+贪心)
1. 拦截导弹(Noip1999) 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度. ...
- angular1时间控件之时间比较大小,比如入住日期和离店日期,入住不能晚于离店时间
功能可能是大家有时遇到的,不过angluar1现在用的人很少了,希望帮上和我一样掉坑里的朋友吧,之前自己也在网上找过几乎没找到angluar1和我代码需求一样的控件,没办法自己造 了,看代码图 就差不 ...
- innodb 关键特性(insert buffer)
一.insert buffer 性能改善 insert buffer和数据页一样,也是物理页的一个组成部分. 在innodb存储引擎中,主键是行唯一的标识符.通常应用程序中行记录的插入顺序是按照主键递 ...