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

一、客户端模式介绍

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

image.png

请求步骤为:

  • 客户端向认证服务器进行认证,并请求一个访问令牌token
  • 认证服务器进行认证,通过之后,返回客户端一个访问令牌。

二、创建认证服务

  • 创建一个认证服务IdentityServerCenter,通过NuGet安装IdentityServer4
  • 添加配置资源与客户端的文件,引入using IdentityServer4.Models
   public class Config
{
public static IEnumerable<ApiResource> GetResources()
{
return new List<ApiResource> {
new ApiResource {
Name = "ImageResource",
Scopes={ new Scope ("ImageResource")},//Scopes必须配置,否则获取token时返回 invalid_scope
},
new ApiResource { Name = "FileResourse" },
new ApiResource { Name="Api", Scopes={new Scope ("Api") } }
};
}
public static IEnumerable<Client> GetClients()
{
return new List<Client> {
new Client {
ClientId = "ClientId",
AllowedGrantTypes =GrantTypes.ClientCredentials,//授权模式:客户端模式
AllowedScopes={ "ImageResource","Api" }, //允许访问的资源 GetResources()中配置的
ClientSecrets={ new Secret { Value= "ClientSecret".Sha256(), Expiration=DateTime.Now.AddMinutes(5)} }
} };
}
}
  • 注入IdentityServer4,添加IdentityServer4配置
        public void ConfigureServices(IServiceCollection services)
{
//注入IdentityServer 添加IdentityServer4配置
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetResources()) //添加配置的资源ApiResource
.AddInMemoryClients(Config.GetClients());//添加配置的客户端Client
// services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
  • 使用IdentityServer4
     public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//使用IdentityServer
app.UseIdentityServer();
}
  • 启动项

    image.png

    由于未使用MVC,访问该api返回404。IdentityServer4提供一个地址可获取相关配置项。

http://examplehostname.com.well-known/openid-configuration/

访问 http://localhost:5000/.well-known/openid-configuration 将返回的信息序列化如下

image.png

scopes_supported": ["ImageResource", "Api", "offline_access"]即为Config中配置的访问的资源AllowedScopes

  • 使用postman获取token

    image.png

    grant_type为客户端授权client_credentials,client_idClient_SecretConfig中配置的ClientIdSecret。接下来创建一个可访问的资源。

三、创建资源服务

  • 创建一个资源服务项目ImageResourceApi
  • 注入认证
 public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(config => {
config.DefaultScheme = "Bearer";
}).AddIdentityServerAuthentication(option=> {
option.ApiName = "ImageResource";
option.Authority = "http://localhost:5000"; //认证服务的url
option.ApiSecret = "ClientSecret".ToSha256();// 访问的secret
option.RequireHttpsMetadata = false; });
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
} app.UseHttpsRedirection();
//使用认证
app.UseAuthentication();
app.UseMvc();
}
  • 启动资源服务,返回401未授权;
  • 使用postman带着token请求资源服务ImageResourceApi,请求成功。
    image.png

.Net Core身份认证:IdentityServer4实现OAuth 2.0 客户端模式 - 简书的更多相关文章

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

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

  2. 深入解读 ASP.NET Core 身份认证过程

    长话短说:上文我们讲了 ASP.NET Core 基于声明的访问控制到底是什么鬼? 今天我们乘胜追击:聊一聊ASP.NET Core 中的身份验证. 身份验证是确定用户身份的过程. 授权是确定用户是否 ...

  3. 在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client

    在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client   阅读目录 验证代码流程 Refreshing a Token Built-In Providers 这个包能够让你 ...

  4. .Net Core:身份认证组件

    类库组件 .NET Core的身份认证使用的类库如下图:常用的 Microsoft.AspNetCore.Authorization Microsoft.AspNetCore.Authorizatio ...

  5. Core身份认证

    Core中实现一个基础的身份认证 注:本文提到的代码示例下载地址> How to achieve a basic authorization in ASP.NET Core 如何在ASP.NET ...

  6. .NET 黑魔法 - asp.net core 身份认证 - Policy

    身份认证几乎是每个项目都要集成的功能,在面向接口(Microservice)的系统中,我们需要有跨平台,多终端支持等特性的认证机制,基于token的认证方式无疑是最好的方案.今天我们就来介绍下在.Ne ...

  7. 使用 IdentityServer4 实现 OAuth 2.0 与 OpenID Connect 服务

    IdentityServer4 是 ASP.NET Core 的一个包含 OIDC 和 OAuth 2.0 协议的框架.最近的关注点在 ABP 上,默认 ABP 也集成 IdentityServer4 ...

  8. IdentityServer4 实现 OAuth 2.0(密码模式 - HTTP Post 方式)

    之前写了一篇文章:<IdentityServer4 实现 OpenID Connect 和 OAuth 2.0> 上面这篇文章虽然详细,但都是点到为止的介绍,并没有实际应用的示例,所以,后 ...

  9. IdentityServer4:IdentityServer4+API+Client实践OAuth2.0客户端模式(1)

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

随机推荐

  1. Android连接socket服务器上传下载多个文件

    android连接socket服务器上传下载多个文件1.socket服务端SocketServer.java public class SocketServer { ;// 端口号,必须与客户端一致 ...

  2. Flask项目之手机端租房网站功能测试(完结)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶注册和登录以及用户退出功能 二丶上传头像功能和修改用户名功能测试 三丶发布房源以及实名认证功能测试 四丶网站房屋搜索功能 ...

  3. Linux 进程通信之管道

    管道是单向的.先进先出的,它把一个进程的输出和还有一个进程的输入连接在一起.一个进程(写进程)在管道的尾部写入数据,还有一个进程(读进程)从管道的头部读出数据.数据被一个进程读出后,将被从管道中删除, ...

  4. php编译参数注释

    1. 指定安装路径 --prefix=PREFIX 2. 指定运行用户 --with-fpm-user=nginx 3. 指定运行组 --with-fpm-group=nginx 3.与'--pref ...

  5. [python]类与对象-下

    [实例对象]可以简称为[实例] 一.类与对象的关系 [类]是[对象]的模板. [类]就像工厂的模具,以它为模板,造出来的成千上万的产品,才是被我们消费.购买.使用,真正融入我们生活的东西.这些产品,在 ...

  6. session 、cookie、token的区别及联系

    本文转自:https://blog.csdn.net/jikeehuang/article/details/51488020:https://blog.csdn.net/weixin_37196194 ...

  7. 【hdu 3518】Boring counting

    [链接]h在这里写链接 [题意] 给出一个字符串,求出至少不重叠出现2次以上的子串有多少个. [题解] 枚举要找的子串的长度i; 根据height数组,找出连续>=i的height; 这几个起始 ...

  8. Spring MVC学习总结(1)——Spring MVC单元测试

    关于spring MVC单元测试常规的方法则是启动WEB服务器,测试出错 ,停掉WEB 改代码,重启WEB,测试,大量的时间都浪费在WEB服务器的启动上,下面介绍个实用的方法,spring MVC单元 ...

  9. 洛谷——P1022 计算器的改良

    https://www.luogu.org/problem/show?pid=1022#sub 题目背景 NCL是一家专门从事计算器改良与升级的实验室,最近该实验室收到了某公司所委托的一个任务:需要在 ...

  10. (6)uboot具体解释——关闭缓存和mmu

    uboot具体解释--关闭缓存和mmu 当设置完时钟分频以后,uboot就会运行cpu_init_crit汇编函数,这个函数的主要作用就是关闭缓存和mmu.然后调用lowlevel_init函数进行系 ...