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客户端认证模式实现的更多相关文章

  1. IdentityServer4(客户端授权模式)

    1.新建三个项目 IdentityServer:端口5000 IdentityAPI:端口5001 IdentityClient: 2.在IdentityServer项目中添加IdentityServ ...

  2. 认证授权:IdentityServer4 - 各种授权模式应用

    前言: 前面介绍了IdentityServer4 的简单应用,本篇将继续讲解IdentityServer4 的各种授权模式使用示例 授权模式: 环境准备 a)调整项目结构如下:   b)调整cz.Id ...

  3. Core篇——初探IdentityServer4(OpenID Connect模式)

    Core篇——初探IdentityServer4(OpenID Connect客户端验证) 目录 1.Oauth2协议授权码模式介绍2.IdentityServer4的OpenID Connect客户 ...

  4. IdentityServer(二)客户端授权模式

    前言 客户端授权模,客户端直接向Identity Server申请token并访问资源.客户端授权模式比较适用于服务之间的通信. 搭建Identity服务 新建名为 IdentityServer 的W ...

  5. IdentityServer4(7)- 使用客户端认证控制API访问(客户端授权模式)

    一.前言 本文已更新到 .NET Core 2.2 本文包括后续的Demo都会放在github:https://github.com/stulzq/IdentityServer4.Samples (Q ...

  6. IdentityServer4[3]:使用客户端认证控制API访问(客户端授权模式)

    使用客户端认证控制API访问(客户端授权模式) 场景描述 使用IdentityServer保护API的最基本场景. 我们定义一个API和要访问API的客户端.客户端从IdentityServer请求A ...

  7. AspNetCore中使用Ocelot之 IdentityServer4(1)

    AspNetCore中使用Ocelot之 IdentityServer4(1) 前言: OceLot网关是基于AspNetCore 产生的可扩展的高性能的企业级Api网关,目前已经基于2.0 升级版本 ...

  8. IdentityServer4 (1) 客户端授权模式(Client Credentials)

    写在前面 1.源码(.Net Core 2.2) git地址:https://github.com/yizhaoxian/CoreIdentityServer4Demo.git 2.相关章节 2.1. ...

  9. IdentityServer4系列 | 客户端凭证模式

    一.前言 从上一篇关于 快速搭建简易项目中,通过手动或者官方模板的方式简易的实现了我们的IdentityServer授权服务器搭建,并做了相应的配置和UI配置,实现了获取Token方式. 而其中我们也 ...

随机推荐

  1. jvm(一)类加载器

    1.jvm的生命周期结束的几种情况 a.执行了System.exit()方法 b.程序正常执行结束 c.程序在执行过程中遇到了异常或错误而异常终止 d.操作系统出现错误 2.类加载过程 加载:查找并加 ...

  2. Java开发经常容易犯的错误

    调用Set.addAll()方法时抛UnsupportedOperationException异常 上面的Set是Map中keySet的返回结果. 程序中这样两句代码运行时,抛UnsupportedO ...

  3. Taro之百度地图显示定位点和信息

    由于没有后台数据就随机生成点来模拟了.具体代码如下. componentDidMount = () => { const { BMap, BMAP_STATUS_SUCCESS } = wind ...

  4. HttpServletRequest 获取cookie

    request.getHeader("cookie") 得到的是a=b,c=d Cookie[] cookies = request.getCookies(); 访问方在heade ...

  5. unable to resolve module react-native-gesture-handler from

    在安装了npm install --save react-navigation后跑项目然后就红屏了. 解决方法: 安装触摸方式(需要执行以下方法) npm install --save react-n ...

  6. 云笔记项目-Spring事务学习-传播MANDATORY

    接下来测试事务传播属性MANDATORY Service层 所有Service层实现类都设置事务传播属性为MANDATORY. LayerT层代码 package LayerT; import jav ...

  7. jQuery判断鼠标滚动方向

    var scrolltop = new Array(); var index = 0; scrolltop[0] = 0; $(document).scroll(function(){ index + ...

  8. vim字符查找和替换

    一.替换 1. r+<待替换的字母>    将光标处的字母替换为指定的字母 2.R       连续替换直到按下ESC 3.cc      整行替换,并进入插入模式 4.cw     替换 ...

  9. esxi虚拟机克隆后的主机网卡设置

    windows 在虚拟机配置页面右击属性,编辑虚拟机设置,删除掉原来的网卡,重新添加新的网卡 进入系统以后,进入 windows/system32 找到sysprep,运行等待电脑重启 重启过程会稍微 ...

  10. 浅析MVC模式与三层架构的区别

    浅析MVC模式与三层架构的区别 三层架构和MVC是有明显区别的,MVC应该是表现模式(三个加起来以后才是三层架构中的UI层).三层架构(3-tier application) 通常意义上的三层架构就是 ...