一、OAuth2.0

1、OAuth2.0概念

OAuth2.0(Open Authorization)是一个开放授权协议;第三方应用不需要接触到用户的账户信息(如用户名密码),通过用户的授权访问用户资源

OAuth的步骤一般如下:

1、客户端要求用户给予授权
2、用户同意给予授权
3、根据上一步获得的授权,向认证服务器请求令牌(token)
4、认证服务器对授权进行认证,确认无误后发放令牌
5、客户端使用令牌向资源服务器请求资源
6、资源服务器使用令牌向认证服务器确认令牌的正确性,确认无误后提供资源

该协议的参与者至少包含:

RO (resource owner): 资源所有者:用户。

RS (resource server): 资源服务器:数据中心;它存储资源,并处理对资源的访问请求。如:API资源,相册服务器、博客服务器。

AS (authorization server): 授权服务器

Client: 第三方应用

2、授权模式

四种模式:

1、授权码模式(authorization code)
2、简化模式(implicit)
3、密码模式(resource owner password credentials)
4、客户端模式(client credentials)

二、IdentityServer + API+Client演示客户端模式

客户端模式(ClientCredentials):经常运用于服务器对服务器中间通讯使用;步骤如下:

1、客户端直接用自身的信息向授权服务器请求token:

HTTP请求:

granttype:授权类型

scope:授权范围

     POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded grant_type=client_credentials&scope=api001

2、授权服务器验证信息后返回token

     HTTP/1.1  OK
Content-Type: application/json;charset=UTF-
Cache-Control: no-store
Pragma: no-cache {
"access_token":"2YotnFZFEjr1zCsicMWpAA",
"token_type":"example",
"expires_in":,
"example_parameter":"example_value"
}

下面通过一个快速示例理解;快速示例将通过服务器与服务器直接通过api访问数据;

1、授权服务端;

这里将通过IdnetityServer4实现一个标准的Oauth2.0协议的服务端;

引用IdentityServer4包

新建ASP.NET Core Web Application ——Empty项目;这里通过程序包控制台添加IdentityServer4引用包

Install-Package IdentityServer4

定义API资源、定义客户端

新建类Config.cs;定义资源Scopes、Client;

using IdentityServer4.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace Practice.IdentityServer
{
public class Config
{
//scopes定义
public static IEnumerable<ApiResource> GetApiResource()
{
return new List<ApiResource>
{
//给api资源定义一个scopes
new ApiResource("api1","my api")
}; } //客户端注册,客户端能够访问的资源(通过:AllowedScopes)
public static IEnumerable<Client> GetClient()
{
return new List<Client>
{
new Client
{
ClientId="client",
AllowedGrantTypes=GrantTypes.ClientCredentials,
ClientSecrets={new Secret("secrect".Sha256())},
AllowedScopes={"api"}
}
};
}
}
}

把资源和客户端、存储方式、添加到service container(DI system)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; namespace Practice.IdentityServer
{
public class Startup
{
// 添加服务到容器(add services to the container)DI系统. public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddTemporarySigningCredential()
.AddInMemoryApiResources(Config.GetApiResource())
.AddInMemoryClients(Config.GetClient());
} //配置HTTP request 管道(pipeline).
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(LogLevel.Debug); if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseIdentityServer(); //app.Run(async (context) =>
//{
// await context.Response.WriteAsync("Hello World!");
//});
}
}
}

配置

注意:使用自宿的方式调试;会把日志输出到控制台;自宿的配置方式:

方法1:

方法2:

配置地址:

在program.cs添加一句:.UseUrls("http://localhost:5000") 设置调试url;

运行

运行、通过http://localhost:5000/.well-known/openid-configuration访问 ;可以看到是一个restful的api;

2、API资源

新建ASP.NET Core Web API 项目;添加中间件IdentityServer4.AccessTokenValidation 包引用

配置api的地址

添加控制器

[Route("identity")]
[Authorize]
public class IdentityController : Controller
{
[HttpGet]
public IActionResult Get()
{
return new JsonResult(from a in User.Claims select new { a.Type,a.Value});
}
}

配置

把授权中间件配置到api host里;IdentityServer4.AccessTokenValidation这里的主要作用

1、验证token令牌,确保token令牌的Issuer发行者是经过注册认证可信任的发行者;

2、验证token令牌,确保这个令牌的授权范围(scope)包括授权使用这个api

    public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
} public IConfigurationRoot Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters();
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions {
Authority = "http://localhost:5000",
RequireHttpsMetadata=false,
ApiName="api1"
});
app.UseMvc();
}
}

运行后,直接浏览器访问http://localhost:5001/identity会被拒绝说明成功;访问这个api需要在http请求的header加入token才可以访问;

3、Client客户端

新建.Net Core——控制台应用;添加中间件

IdentityModel是官方提供给我们的一个Client类库;当然用户也可以自行构建原始的http协议访问API;

public class Program
{
public static void Main(string[] args) => MainAsync().GetAwaiter().GetResult(); private static async Task MainAsync()
{
//
var dico = await DiscoveryClient.GetAsync("http://localhost:5000"); //token
var tokenClient = new TokenClient(dico.TokenEndpoint, "client", "secret");
var tokenresp = await tokenClient.RequestClientCredentialsAsync("api1");
if (tokenresp.IsError)
{
Console.WriteLine(tokenresp.Error);
return; } Console.WriteLine(tokenresp.Json);
Console.WriteLine("\n\n"); var client = new HttpClient();
client.SetBearerToken(tokenresp.AccessToken); var resp = await client.GetAsync("http://localhost:5000/identity");
if (!resp.IsSuccessStatusCode)
{
Console.WriteLine(resp.StatusCode);
}
else
{
var content = await resp.Content.ReadAsStringAsync();
Console.WriteLine(JArray.Parse(content));
} }
}

DiscoveryClient类:IdentityModel提供给我们通过基础地址(如:http://localhost:5000)就可以访问令牌服务端;当然可以根据上面的restful api里面的url自行构建;上面就是通过基础地址,获取一个TokenClient;(对应restful的url:token_endpoint   "http://localhost:5000/connect/token")

RequestClientCredentialsAsync方法:请求令牌;

获取令牌后,就可以通过构建http请求访问API接口;这里使用HttpClient构建请求,获取内容;

运行效果:

我们换一种原始的方式模拟这个流程

打开Postman:按照restful api页面的说明,依次进行下面的步骤操作,一个很原始的http流程就熟悉了;自行查看原图

资料:

http://wiki.connect.qq.com/%E4%BD%BF%E7%94%A8implicit_grant%E6%96%B9%E5%BC%8F%E8%8E%B7%E5%8F%96access_token

IdentityServer4:IdentityServer4+API+Client实践OAuth2.0客户端模式(1)的更多相关文章

  1. IdentityServer4:IdentityServer4+API+Client+User实践OAuth2.0密码模式(2)

    一.密码模式实操 仍然使用第一节的代码:做如下改动: 1.授权服务端 前面我们使用项目:Practice.IdentityServer作为授权服务器 修改项目的Config.cs类: 添加测试用户,并 ...

  2. .Net Core身份认证:IdentityServer4实现OAuth 2.0 客户端模式 - 简书

    原文:.Net Core身份认证:IdentityServer4实现OAuth 2.0 客户端模式 - 简书 一.客户端模式介绍 客户端模式(Client Credentials Grant)是指客户 ...

  3. IdentityServer4之SSO(基于OAuth2.0、OIDC)单点登录、登出

    IdentityServer4之SSO(基于OAuth2.0.OIDC)单点登录.登出 准备  五个Web站点: 1.localhost:5000 :                  认证服务器.2 ...

  4. Spring Boot Security Oauth2之客户端模式及密码模式实现

    Spring Boot Security Oauth2之客户端模式及密码模式实现 示例主要内容 1.多认证模式(密码模式.客户端模式) 2.token存到redis支持 3.资源保护 4.密码模式用户 ...

  5. Ubuntu宝塔面板设置网站 Apache Server API为Apache 2.0 Handler模式

    用过宝塔面板(https://www.bt.cn)的谁用谁知道:  以下来自官网的介绍: “宝塔Linux面板是提升运维效率的服务器管理软件,支持一键LAMP/LNMP/集群/监控/网站/FTP/数据 ...

  6. oauth2.0密码模式详解

    oauth2.0密码模式 欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章http://www.javaman.cn/sb2/oauth-password 如果你高度信任某个应用, ...

  7. .Net Core身份认证:IdentityServer4实现OAuth 2.0 客户端模式

    一.客户端模式介绍 客户端模式(Client Credentials Grant)是指客户端直接向认证服务(Authorization Server)发送认证请求,获取token,进行认证,一般适用于 ...

  8. RESTful API架构和oauth2.0认证机制(概念版)

    1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fielding的 ...

  9. [PHP] 调用微博API 发微博OAuth2.0

    在实际测试中出现很多问题, 第一就是按照文档调用ACCESS_TOKEN的时候费老劲啦,因为是编辑线上的,有好多中文空格,没有看出来!整了好久! 第二个就是在调用api发微博的时候出现乱码!必须把发送 ...

随机推荐

  1. bootstrapValidator多字段联合验证(如开始日期和结束日期中,开始日期不可晚于结束日期)

    接触bootstrapvalidator时间不久,最近需要多个字段共同验证,网上查了一下未找到,查阅api文档,发现确实可以实现. 先看dom <div class="form-gro ...

  2. 2018ACM-ICPC焦作区域赛【反思总结】

    摸银结束回来,整个人都轻松了. 自CCPC打铁以来的这两个月真的太痛苦了. 俱乐部退役的退役停训的停训,好冷清啊. 前期切题很稳,前四题两个小时1A. 过了四题之后好像心态有点飘,然后开题就慢了,想题 ...

  3. VMware与Centos系统安装 和重置root密码

    VMware与Centos系统安装   今日任务 1.Linux发行版的选择 2.vmware创建一个虚拟机(centos) 3.安装配置centos7 4.xshell配置连接虚拟机(centos) ...

  4. 20165311 预备作业3 Linux安装及学习

    Linux安装 由于回家没有带笔记本,所以把VirtualBox安装在家里的台式上,回学校之后再重新在自己的笔记本上安装虚拟机.参考<基于VirtualBox安装Ubuntu图文教程>,整 ...

  5. c# http get post转义HttpUtility.UrlEncode

    //该数据如果要http get.post提交,需要经过转义,否则该数据中含& ''等字符会导致意外错误.需要转义.这里用HttpUtility.UrlEncode来转义.接收方无需反解析 s ...

  6. python-多线程等概念

    并发 & 并行 并发:是指系统具有处理多个任务的能力 并行:是指系统具有 同时 处理多个任务的能力 并行 是  并发的一个子集 同步 & 异步 同步:当进程执行到一个I/O(等待外部数 ...

  7. ORACLE之PACKAGE-包、存储过程、函数

    1,简单的包. 创建包规范: create or replace package pack_test1 is -- 定义过程1 procedure p_test1(p_1 in varchar2); ...

  8. 转:CSS设置HTML元素的高度与宽度的各种情况总结

    1.元素不设宽度第一种情况:元素为文档流中元素<!-- 父元素宽度为100px --><div style="width:100px;">     < ...

  9. 最全的MonkeyRunner自动化测试从入门到精通(2)

    一.Python环境变量的配置 步骤一:在官网进行下载python安装包,官网下载的路径:https://www.python.org/,如图所示: 步骤二:下载完成后,双击安装包,进行如下安装的界面 ...

  10. Jenkins 忘记admin用户名以及密码

    1.进入 如果安装的war包,路劲如下: C:\Users\LENOVO\.jenkins\ 2. 1)方式一: 打开config.xml  ->将useSecurity设置为false 2)方 ...