前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IdentityServer4实战 - 与API单项目整合

IdentityServer4实战 - 与API单项目整合

作者头像
晓晨
发布2019-03-05 10:35:39
8400
发布2019-03-05 10:35:39
举报
文章被收录于专栏:晓晨的专栏

一.前言

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

二.整合

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

代码语言:javascript
复制
services.AddIdentityServer(options=>options.Authentication.CookieAuthenticationScheme= "Cookies")
                .AddDeveloperSigningCredential()
                .AddInMemoryIdentityResources(Config.GetIdentityResources())
                .AddInMemoryApiResources(Config.GetApis())
                .AddInMemoryClients(Config.GetClients())
                .AddTestUsers(Config.GetUsers());

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

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

注意事项

  • Cookie Scheme 是非必须的,但是如果不设置会报错,但是也不会影响正常使用
  • AddAuthentication 必须必须必须 放在 AddIdentityServer 之后
1549006874861
1549006874861

3.中间件配置

代码语言:javascript
复制
app.UseIdentityServer();

这里只需 UseIdentityServer 即可

三.测试

在 IdentityServer4 项目添加一个 Controller

代码语言:javascript
复制
[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

1549007117138
1549007117138

2.请求API

1549007152188
1549007152188

四.资料

本文Demo:

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-02-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一.前言
  • 二.整合
  • 三.测试
  • 四.资料
相关产品与服务
消息队列 TDMQ
消息队列 TDMQ (Tencent Distributed Message Queue)是腾讯基于 Apache Pulsar 自研的一个云原生消息中间件系列,其中包含兼容Pulsar、RabbitMQ、RocketMQ 等协议的消息队列子产品,得益于其底层计算与存储分离的架构,TDMQ 具备良好的弹性伸缩以及故障恢复能力。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档