一.前言

我们在实际使用 IdentityServer4 的时候,可能会在使用 IdentityServer4 项目添加一些API,比如 找回密码、用户注册、修改用户资料等,这些API与IdentityServer4怎么共存在一个项目呢?

二.整合

1.首先在 Startup.cs 中添加 IdentityServer4

services.AddIdentityServer(options=>options.Authentication.CookieAuthenticationScheme= "Cookies")
.AddDeveloperSigningCredential()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApis())
.AddInMemoryClients(Config.GetClients())
.AddTestUsers(Config.GetUsers());

2.然后在添加 IdentityServer4 下添加认证

services.AddAuthentication("Bearer")
.AddCookie("Cookies")
.AddJwtBearer("Bearer", options =>
{
//identityserver4 地址 也就是本项目地址
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.Audience = "api1";
});

注意事项

  • Cookie Scheme 是非必须的,但是如果不设置会报错,但是也不会影响正常使用

  • AddAuthentication 必须必须必须 放在 AddIdentityServer 之后

3.中间件配置

app.UseIdentityServer();

这里只需 UseIdentityServer 即可

三.测试

在 IdentityServer4 项目添加一个 Controller

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

将 IdentityServer4 项目的端口设置为5000,使用密码模式,下面进行测试:

1.请求Token

2.请求API

四.资料

本文Demo:

https://github.com/stulzq/IdentityServer4.Samples/tree/master/Practice/05_Integration

最新文章

  1. win10使用小技巧以及常见问题处理方案
  2. ASP数组全集,多维数组和一维数组[转]
  3. EventRay UI Kit – Web & Mobile 的素材
  4. JPA学习笔记1——JPA基础
  5. 用PHP将Unicode 转化为UTF-8
  6. 【wikioi】2495 水叮当的舞步(IDA*)
  7. Page_Init 的执行过程
  8. Linux Shell 小脚本经典收藏
  9. 最新的thinkphp 后台入口的问题
  10. 【一天一道LeetCode】#85. Maximal Rectangle
  11. 理解MySql的锁&事务隔离级别
  12. surface shader获取像素深度差值
  13. nginx中try_files
  14. 优雅的使用列表推导式和lambda
  15. Spark基础脚本入门实践2:基础开发
  16. 11G新特性 -- variable size extents
  17. Python遍历文件个文件夹
  18. es6 常用总结
  19. 迈出第一步,Hexo博客搭建
  20. 解决一起web 页面被劫持的案例

热门文章

  1. 还在用NuGet吗?大哥FuGet了解一下
  2. 容器化时代我们应当选择Kubernetes
  3. Activity 之使用
  4. 从壹开始微服务 [ DDD ] 之三 ║ 简单说说:领域、子域、限界上下文
  5. 【Spark篇】---Spark中Shuffle文件的寻址
  6. 【重学计算机】计组D2章:数据表示
  7. HTTP 缓存相关
  8. Python:序列的增量赋值
  9. Python实战171203统计
  10. Git认证方式https和ssh的原理及比较