最简单的IdentityServer实现——Api
1.创建项目并添加引用
创建ASP.NET Core Web API项目IdentityServer.EasyDemo.Api


引用IdentityServer4.AccessTokenValidation

2.定义一个Api接口
新增接口文件IdentityController.cs,用于测试授权
如果你直接访问http://localhost:5001/identity ,你会得到一个401错误,因为调用这个接口需要凭证
这里设置一个Api接口,路由是"identity",跟传统的/controller/action访问路由不同,GET请求访问/identity即可
[Route("identity")]
[Authorize]
public class IdentityController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
//这里是查询声明身份
return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
}
}
3.配置Api
services添加IdentityServerAuthentication,设置授权地址为IdentityServer的网址(这里保证了在用户访问到未授权的方法时,会自动跳转到IdentityServer的授权页面)
注意保证Api的ApiName在IdentityServer的Api集合中
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
//将认证服务添加到DI,配置"Bearer"作为默认方案
.AddAuthorization()
.AddJsonFormatters();
services.AddAuthentication("Bearer")
//将IdentityServer访问令牌验证处理程序添加到DI中以供身份验证服务使用
.AddIdentityServerAuthentication(options =>
{
//用于授权的地址
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
//该Api项目对应的IdentityServer的Api资源,与GetApiResources方法里面的Api名称对应
options.ApiName = "api1";
});
}
public void Configure(IApplicationBuilder app)
{
//将认证中间件添加到流水线中,以便在对主机的每次呼叫时自动执行认证
app.UseAuthentication();
app.UseMvc();
}
}
4.在属性中将Api项目的端口号设置为5001

最简单的IdentityServer实现——Api的更多相关文章
- 原生js简单调用百度翻译API实现的翻译工具
先来个在线demo: js翻译工具 或者百度搜索js简单调用百度翻译API工具(不过有个小小的界面显示bug,我想细心的人应该会发现) 或者直接前往该网址:js翻译工具 或者前往我的github:gi ...
- IdentityServer4 简单使用,包括api访问控制,openid的授权登录,js访问
写在前面 先分享一首数摇:http://music.163.com/m/song?id=36089751&userid=52749763 其次是:对于identityServer理解并不是特别 ...
- SQLite3简单入门及C++ API
转载请注明出处:http://www.cnblogs.com/StartoverX/p/4660487.html 项目用到SQLite3,简单记录一下. MySQL不同,SQLite3的数据库基于文件 ...
- 使用go, gin, gorm编写一个简单的curd的api接口
go 是一门非常灵活的语言,既具有静态语言的高性能,又有动态语言的开发速度快的优点,语法也比较简单,下面是通过简单的代码实现了一个简单的增删改查 api 接口 hello world 常规版 新建 d ...
- 一个简单可参考的API网关架构设计
网关一词较早出现在网络设备里面,比如两个相互独立的局域网段之间通过路由器或者桥接设备进行通信, 这中间的路由或者桥接设备我们称之为网关. 相应的 API 网关将各系统对外暴露的服务聚合起来,所有要调用 ...
- 简单服务端缓存API设计
Want 我们希望设计一套缓存API,适应不同的缓存产品,并且基于Spring框架完美集成应用开发. 本文旨在针对缓存产品定义一个轻量级的客户端访问框架,目标支持多种缓存产品,面向接口编程,目前支持简 ...
- 最简单的IdentityServer实现——Client
客户端控制台演示请求访问令牌,然后使用此令牌访问API 1.新建项目并添加引用 新建一个.net core的控制台程序IdentityServer.EasyDemo.Client 1 引用Iden ...
- 最简单的IdentityServer实现——IdentityServer
1.新建项目 新建ASP .Net Core项目IdentityServer.EasyDemo.IdentityServer,选择.net core 2.0 1 2 引用IdentitySer ...
- 最简单的IdentityServer实现——项目基本结构与流程
项目结构 共分为三个组成部分: IdentityServer:用于登录.身份认证与授权 Api:提供获得授权后调用的各接口 Client(客户端,控制台):访问IdentityServer授权,再访问 ...
随机推荐
- 超级牛X的免费开源小工具之tldr
github介绍:http://tldr-pages.github.io/ github源码:https://github.com/tldr-pages/tldr 什么是tldr? 新命令行世界?还是 ...
- Android 使用binder访问service的方式
binder机制是贯穿整个Android系统的进程间访问机制,经常被用来访问service,我们结合代码看一下binder在访问service的情形下是怎么具体使用的. service 你可以理解成没 ...
- 配置java 环境变量(jdk)
java环境变量需要配置3个: JAVA_HOME:D:\Program Files (x86)\Java\jdk1.8 CLASSPATH:.;%JAVA_HOME%\lib\dt.jar;%JAV ...
- [Node.js] Build microservices in Node.js with micro
micro is a small module that makes it easy to write high performance and asynchronous microservices ...
- Ajax基础与Json应用(一)
一.Ajax概念 Ajax是异步的javacript和xml 发音: Ajax [ˈeɪˌdʒæks] 二.同步与异步 传统方式(同步):一个请求对应一个回应,他们是同步的,回应不完成,没办法对这个页 ...
- [WPF]VS2019打包WPF程序
原文:[WPF]VS2019打包WPF程序 版权声明:本文为本人原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37316917/article/details/8 ...
- linux 登录windows跳板机
rdesktop -u 用户 -p 密码 ip -r sound:on/off -g 1200:830
- [Django] Building the rest API
Install the rest api framework: pip install djangorestfamework In settings.py: INSTALLED_APPS = [ 'd ...
- TimeUnit用法
TimeUnit是java.util.concurrent包下面的一个类,表示给定单元粒度的时间段 1.主要作用 时间颗粒度转换 延时 2.常用的颗粒度 TimeUnit.DAYS //天 TimeU ...
- python 多线程拷贝单个文件
# -*- coding: utf-8 -*- # @author: Tele # @Time : 2019/04/04 下午 12:25 # 多线程方式拷贝单个文件 import threading ...