基于IdentityServer4的单点登录——Api
2024-08-24 12:29:21
1.新建项目并添加引用
新建一个asp .net core 2.0的项目
引用IdentityServer4.AccessTokenValidation
2.配置
将Api与IdentityServer服务器挂钩
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters();
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
//这里添加IdentityServer服务器的地址
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
//这个Api资源的名称,注意与IdentityServer的Api资源对应
options.ApiName = "api1";
});
}
public void Configure(IApplicationBuilder app)
{
app.UseAuthentication();
app.UseMvc();
}
}
3.Api接口
添加一个新的Controller,使用此控制器来测试授权要求,以及通过Api可视化声明身份
[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 });
}
}
4.设置Api项目的端口号为5001
最新文章
- BZOJ4591——[Shoi2015]超能粒子炮·改
- 走进spring之springmvc实战篇(二)
- 自然语言18_Named-entity recognition
- 用Dictionary代替if
- javascript创建对象和属性的几种方式
- redis maxmemory设置
- iOS 最值宏定义
- wlcore: firmware chunk too long
- ch5-Class 与 Style 绑定(v-bind:class v-bind:style)
- JavaScript练习网站收集
- Neo4j学习笔记(2)——数据索引
- python+selenium自动测试之WebDriver的常用API(基础篇一)
- Bootstrap 常用属性
- 前端请求参数MD5加密发送后台
- VR外包团队—国内首家VR虚拟现实主题公园即将在北京推出
- C# Winform 跨线程更新UI控件常用方法汇总(多线程访问UI控件)
- SQLServer最耗资源时间的SQL语句
- November 17th, 2017 Week 46th Friday
- PAT B1048 数字加密 (20 分)
- 9.9Dajngo MTV
热门文章
- ios开发日期的NSDate,NSCalendar分类
- .NET-架构优化实战-梳理篇
- sublime-1 sublime设置到鼠标右键
- 非常实用全面的 C++框架,库类等资源
- [Ramda] Sort, SortBy, SortWith in Ramda
- 【BZOJ 1017】 [JSOI2008]魔兽地图DotR
- Crystal Report - 水晶报表导出文件的格式设置
- com.sun.mirror的jar包
- tolua#是Unity静态绑定lua的一个解决方案
- 利用WPF建立自己的3d gis软件(非axhost方式)(十)SDK中一些自带的展示面板应用