原文参考:Switching to Hybrid Flow and adding API Access back

接上篇:IdentityServer-Protecting an API using Client Credentials

首先配置OpenID:

原文参考:Adding User Authentication with OpenID Connect

下载相关UI:Quickstart UI repo

下载下来后主要有3个文件夹

需要把前面两个文件夹的部分内容复制到项目中

Quickstart目录中复制如下内容

Views目录中复制如下内容

Shared目录中只需要复制一个文件

复制完成后将上面的类除Controller外统一修改命名空间:【项目名称】.Models

Controller类修改命名空间:【项目名称】.Controllers

目的是在Views中不需要在引入命名空间,因为原来的Views\_ViewImports.cshtml 中有引用

接下来配置Client进行测试

在postman中测试Hybrid模式需要注意加上scope,否则会报错

在配置Client的时候可以设置是否启用提示页:

new Client
{
    ClientId = "mvc",
    ClientName = "MVC Client",
    AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

    RequireConsent = false,

    ClientSecrets =
    {
        new Secret("secret".Sha256())
    },

    RedirectUris           = { "http://localhost:5002/signin-oidc" },
    PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },

    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        "api1"
    },
    AllowOfflineAccess = true
}

RequireConsent=false表示不显示提示直接授权通过。

代码地址:IdentityCenter

将IdentityCenter和API放在同一个项目中,修改startup

services.AddAuthentication()
               .AddJwtBearer(jwt =>
               {
                   jwt.Authority = "http://localhost:5000";
                   jwt.RequireHttpsMetadata = false;
                   jwt.Audience = "api1";
               });

新建一个测试Controller

namespace IdentityCenter.API
{
    [Produces("application/json")]
    [Route("api/Identity")]
    //[Authorize]

    public class IdentityController : Controller
    {
        // GET: api/Identity
        [HttpGet]
        [Authorize(AuthenticationSchemes = "Bearer")]
        public IActionResult Get()
        {
            return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
        }

        // GET: api/Identity/5
        [HttpGet("{id}", Name = "Get")]
        public string Get(int id)
        {
            return "value";
        }
    }
}

这里要指定[Authorize(AuthenticationSchemes = "Bearer")]

而不能仅仅使用[Authorize]

当然,如果把startup中的代码改下

services.AddAuthentication("Bearer")

那么可以直接使用[Authorize]

这样就可以使用token访问此API

还有一点要注意的是如果把认证中心部署在另外一台机器上,比如放在CentOS中

那么jwt.Authority = "http://10.202.203.29:5000";  这里一定要使用具体的IP地址,否则会出现可以获取token,但无法通过授权

这里在https://identityserver4.readthedocs.io/en/release/endpoints/discovery.html 有说明

IdentityServer4 Hybrid 模式的更多相关文章

  1. 浅析hybrid模式下地支付宝钱包和微信

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VuY2hhbzEyNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  2. IdentityServer4授权模式应用场景

    OpenID 和 OAuth 的区别 IdentityServer4,NET Core下的安全框架 客户端模式(Client Credentials) 密码模式(resource owner pass ...

  3. IdentityServer4 密码模式认证

     授权服务器设置 添加用户 添加测试用户,也可以从数据库查 public static List<TestUser> GetTestUser() { return new List< ...

  4. IdentityServer4 密码模式实现

    1.  修改 Config.cs using System.Collections; using System.Collections.Generic; using IdentityServer4.M ...

  5. IdentityServer4密码模式接入现有用户数据表

    具体接入identityserver请看文档,这里只简单列举部分步骤 1.创建一个web项目,引入Identityserver4的nuget包 2.新建一个类,实现IResourceOwnerPass ...

  6. 【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)

    本站文章均为李华明Himi原创,转载务必在明显处注明:(作者新浪微博:@李华明Himi) 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-c ...

  7. 有关 Hybrid 开发模式实践总结

    前言 随着公司业务不断发展,移动开发项目越来越多,项目任务时间紧,我们内部开发流程是以项目为导向,有别于一般公司对产品不断迭代的做法,但移动端开发人员资源有限,需要在不同项目之间做业务场景切换开发,就 ...

  8. Hybrid App—Hybrid App开发模式介绍和各种开发模式对比

    什么是Hybrid App 最开的App开发只有原生开发这个概念,但自从H5广泛流行后,一种效率更高的开发模式Hybrid应运而生,它就是"Hybrid模式".Hybrid APP ...

  9. Hybrid App 开发模式

    开发移动App主要有三种模式:Native. Hybrid 和 Web App. 需要注意的一点是在选择开发模式的时候,要根据你的项目类型(图片类?视频类?新闻类?等),产品业务和人员技术储备等做权衡 ...

随机推荐

  1. JMeter压力测试及服务器状态监控教程

    转载自:https://blog.csdn.net/cbzcbzcbzcbz/article/details/78023327 前段时间公司需要对服务器进行压力测试,包括登录前的页面和登录后的页面,主 ...

  2. Zookeeper C++编程实战之主备切换

    默认zookeeper日志输出到stderr,可以调用zoo_set_log_stream(FILE*)设置输出到文件中还可以调用zoo_set_debug_level(ZooLogLevel)控制日 ...

  3. ArcGIS 点云转DEM

  4. 有趣的HTML5 CSS3效果

    iphone6 外观:http://www.html5tricks.com/demo/css3-iphone6/index.html 天气图标:http://www.html5tricks.com/d ...

  5. XXX is not mapped

    这个问题绊了我两次跟头,作为一个3年多开发经验的人,甚是尴尬 java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntax ...

  6. Windows下Django环境搭建

    总体示意图如下:  Windows下搭建Django环境 1.安装Python版本 2.安装pip工具,一般Python安装都会自动会有这个,在你python安装命令下Scripts文件夹下 3.dj ...

  7. nodes 验证码

    一个常见的需求. 知乎上得讨论:http://www.zhihu.com/question/32156977 node-canvas 在mac上安装有问题,可能是我没有sudo 使用了ccap:挺不错 ...

  8. 16位CRC校验_Delphi

    unit Modbus_main; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, Forms, Controls, G ...

  9. poj 3264 区间最大最小值 RMQ问题之Sparse_Table算法

    Balanced Lineup Time Limit: 5000 MS Memory Limit: 0 KB 64-bit integer IO format: %I64d , %I64u Java ...

  10. Linux-用户及权限

    1. 用户组 RHEL 7/CentOS 7系统中的用户组有如下3类: 超级用户,UID 0:系统的超级用户. 系统用户,UID 1-999:系统中系统服务由不同用户运行,更加安全,默认被限制不能登录 ...