前言

OAuth 2.0默认四种授权模式(GrantType)

本章主要介绍客户端模式(client credentials)

,他主要是由两部分构成客户端和认证服务器.

认证服务器在确定客户端信息无误后向客户端返回token,客户端请求资源时带着该token进行访问.(在这种模式中用户可直接向客户端注册,客户端再以自己的名义请求认证服务器)

搭建认证服务器

创建一个Api项目工程,端口设置为5000

Package

PM> Install-package IdentityServer4 -version 2.5.3

创建一个类Config(配置要保护的资源和可以访问该API的客户端服务器)

    /// <summary>
/// Identity配置
/// </summary>
public class Config
{
/// <summary>
/// 定义要保护的资源
/// </summary>
/// <returns></returns>
public static IEnumerable<ApiResource> GetApiResources() {
return new List<ApiResource>
{
new ApiResource("api1", "My API")
};
}
/// <summary>
/// 定义授权客户端
/// </summary>
/// <returns></returns>
public static IEnumerable<Client> GetClients() {
return new List<Client>
{
new Client()
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.ClientCredentials, //设置模式,客户端模式
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api1" }
}
};
}
}
配置Startup

在ConfigureServices方法中注入IdentityServer4服务

 public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddIdentityServer()//IdentityServer4服务
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetApiResources()) //配置资源
.AddInMemoryClients(Config.GetClients());//把配置文件的Client配置资源放到内存
}

在Configure方法中添加IdentityServer4服务中间件

app.UseIdentityServer();

搭建 Api Resource

创建一个客户端工程项目,端口设置为5001

Package

PM> Install-package IdentityServer4.AccessTokenValidation -version 2.7.0

配置Startup

在ConfigureServices添加认证服务器地址

  public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:5000";//授权服务器地址
options.RequireHttpsMetadata = false;//不需要https
options.ApiName = "api1";
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

在Configure方法中添加IdentityServer4服务中间件

app.UseIdentityServer();

测试

在客户端values控制器上面增加[Authorize]

直接访问资源服务器http://localhost:5001/api/values

访问受限了 code 401

启动授权服务器

http://localhost:5000/.well-known/openid-configuration

发现端点可通过/.well-known/openid-configuration

获取token

启动后我们通过token_endpoint获取token

client_id为我们在授权服务器配置的clientid,

client_secret为配置中的secret,

grant_type为授权模式此处为客户端模式(client_credentials),

请求后返回凭证信息,

我们通过access_token再去访问资源服务器

使用这种授权类型,会向token 。

code 200

概要

示例地址:https://github.com/fhcodegit/IdentityServer4.Samples

IdentityServer4叙述:https://www.cnblogs.com/yyfh/p/11590383.html

asp.net core IdentityServer4 实现 Client credentials(客户端凭证)的更多相关文章

  1. IdentityServer4之Client Credentials(客户端凭据许可)

    IdentityServer4之Client Credentials(客户端凭据许可) 参考 项目创建:0_overview,1_client_credentials 概念:客户端凭据许可 认证服务端 ...

  2. IdentityServer4 之Client Credentials走起来

    前言 API裸奔是绝对不允许滴,之前专门针对这块分享了jwt的解决方案(WebApi接口裸奔有风险):那如果是微服务,又怎么解决呢?每一个服务都加认证授权也可以解决问题,只是显得认证授权这块冗余,重复 ...

  3. win10 uwp 使用 asp dotnet core 做图床服务器客户端

    原文 win10 uwp 使用 asp dotnet core 做图床服务器客户端 本文告诉大家如何在 UWP 做客户端和 asp dotnet core 做服务器端来做一个图床工具   服务器端 从 ...

  4. Asp.Net Core IdentityServer4 中的基本概念

    一.前言 这篇文章可能大家会觉得很空洞,没有实际的实战东西,主要是自己整理出来的IdentityServer4 的一些概念性的东西:如果你对IdentityServer4有过一定的实战经验,可以跳过不 ...

  5. asp.net core系列 54 IS4用客户端凭据保护API

    一. 概述 本篇开始进入IS4实战学习,从第一个示例开始,该示例是 “使用客户端凭据保护API”,这是使用IdentityServer保护api的最基本场景.该示例涉及到三个项目包括:Identity ...

  6. Asp.net Core IdentityServer4 入门教程(一):概念解析

    目录 1.IdentityServer4 是什么 2.什么是OpenID和OAuth 2.0协议 3.IdentityServer4 可以用来做什么 其他 1.IdentityServer4 是什么 ...

  7. asp.net core IdentityServer4 概述

    概览 现代应用程序看上去大都是这样的: 最常见的交互是: 浏览器与Web应用程序通信 Web应用程序与Web API通信(有时是独立的,有时是代表用户的) 基于浏览器的应用程序与Web API通信 本 ...

  8. Asp.Net Core IdentityServer4 管理面板集成

    前言 IdentityServer4(以下简称 Id4) 是 Asp.Net Core 中一个非常流行的 OpenId Connect 和 OAuth 2.0 框架,可以轻松集成到 Asp.Net C ...

  9. asp.net Core 使用过滤器判断请求客户端是否为移动端,并实现PC端和移动端请求映射和自动跳转

    很多时候我们做网站时单纯的用bootstrap等前端框架实现的前端自适应带给用户的体验并不太好,所以为了提高用户体验会专门针对PC端网页重新设计一套移动端网页,但是怎么才能做到在移动端访问PC页面的时 ...

随机推荐

  1. NMS的python实现

    https://blog.csdn.net/a1103688841/article/details/89711120

  2. HMM学习

    参看博客: 1.https://www.cnblogs.com/skyme/p/4651331.html 2.https://blog.csdn.net/continueoo/article/deta ...

  3. pyinstaller打包出错numpy.core.multiarray failed to import

    py原文件运行时正常,但用pyinstaller打包为exe后,在运行则报错: 这是因为cv2要求的numpy版本与你装的numpy版本不一样,导致冲突:网上很多说升级numpy,但你把numpy升的 ...

  4. 跨库数据迁移利器 —— Sqoop

    一.Sqoop 基本命令 1. 查看所有命令 # sqoop help 2. 查看某条命令的具体使用方法 # sqoop help 命令名 二.Sqoop 与 MySQL 1. 查询MySQL所有数据 ...

  5. 边缘缓存模式(Cache-Aside Pattern)

    边缘缓存模式(Cache-Aside Pattern),即按需将数据从数据存储加载到缓存中.此模式最大的作用就是提高性能减少不必要的查询. 1 模式 先从缓存查询数据 如果没有命中缓存则从数据存储查询 ...

  6. Delphi - 通过WinAPI GetCursorPos实现鼠标位置的实时显示

    通过WinAPI GetCursorPos实现鼠标位置的实时显示 有时候我们需要将鼠标的位置实时抓取出来,可以通过如下方式实现. 添加一个Timer控件,执行间隔改为100ms,双击控件输入如下代码: ...

  7. spring-cloud-kubernetes的服务发现和轮询实战(含熔断)

    本文是<spring-cloud-kubernetes实战系列>的第四篇,主要内容是在kubernetes上部署两个应用:Web-Service和Account-Service,通过spr ...

  8. 基于ASP.Net Core开发的一套通用后台框架

    基于ASP.Net Core开发一套通用后台框架 写在前面 这是本人在学习的过程中搭建学习的框架,如果对你有所帮助那再好不过.如果您有发现错误,请告知我,我会第一时间修改. 知其然,知其所以然,并非重 ...

  9. Kth Minimum Clique_2019牛客暑期多校训练营(第二场)

    题目连接: https://ac.nowcoder.com/acm/contest/882/D Description Given a vertex-weighted graph with N ver ...

  10. codeforce617E-XOR and Favorite Number莫队+异或前缀和

    传送门:http://codeforces.com/contest/617/problem/E 参考:https://blog.csdn.net/keyboarderqq/article/detail ...