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方式. 而其中我们也 ...
随机推荐
- 限时订单实现方案(DelayQueue、ActiveMq)
原文链接:http://www.studyshare.cn/blog-front/blog/details/1132 一.在各种电商网站下订单后会保留一个时间段,时间段内未支付则自动将订单状态设置为已 ...
- 新装 Win7 系统装完驱动精灵,一打开到检测界面就卡死——原因与解决方案
1.现象: 重装系统后,鼠标反应慢,且不能上网.因此装了个驱动精灵,准备更新下驱动,但驱动精灵一打开到检测界面就卡死(换驱动人生.鲁大师也一样). 2.原因: Win7 系统 iso 中自带的驱动程序 ...
- samba实现CentOS和window上的数据同步
前言 之前做了一个项目,需要写python脚本来修改组件的安装方式,脚本是在windows下面的pycharm下面进行编写,但是编译要在linux上面进行分模块的maven编译,虽然之前也写了pych ...
- 没有upcast 也不会发生多态
class A{ public: virtual void f(){ cout << "A::f()"<<endl;} }; class B:public ...
- 排序大集合java
今日面试被问到排序问题,发现自己的不足,特来查漏补缺: 首先是各大排序算法的总结表 排序算法大合集 排序算法 平均时间复杂度 最好情况 最坏情况 空间复杂度 稳定性 冒泡排序 Ο(n2) Ο(n) ...
- Servlet的三大作用域
Servlet的三大作用域 一.request 请求对象 共享的数据:请求共享 特点:同一次请求中,共享数据可以获取(请求一旦结束,请求共享清除站)(请求转发能共享参数,重定向不行) 代码:req. ...
- eclipse-jee-kepler 如何设置编译compiler为1.8
最新下载了jdk1.8,想在eclipse里面用一下 jdk1.8的新特性 但是,貌似eclipse(eclipse-jee-kepler-SR2-win32-x86_64.zip)最高的编译级别为: ...
- 一段自适应的CSS代码
一段自适应HTML5的CSS代码,该代码在陕西特产使用过,手机端效果还好,就是PC端看起来没那么大气,比较窄屏 * { transition-property: all; -ms-transition ...
- mysql内容总体回顾
数据类型: 数值类型: 整形 浮点型 字符串: char(定长)\varchar(不定长) char(定长):插入数据或查询数据都快,因为char在磁盘上插入数据时的存储空间是固定的,简单粗暴,直接就 ...
- systemverilog soft constraint
1.class my_item; rand bit constrainted_random; rand bit usually_one; endclass class my_generator; my ...