IdentityServer4 Hybrid 模式
原文参考: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 模式的更多相关文章
- 浅析hybrid模式下地支付宝钱包和微信
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VuY2hhbzEyNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- IdentityServer4授权模式应用场景
OpenID 和 OAuth 的区别 IdentityServer4,NET Core下的安全框架 客户端模式(Client Credentials) 密码模式(resource owner pass ...
- IdentityServer4 密码模式认证
授权服务器设置 添加用户 添加测试用户,也可以从数据库查 public static List<TestUser> GetTestUser() { return new List< ...
- IdentityServer4 密码模式实现
1. 修改 Config.cs using System.Collections; using System.Collections.Generic; using IdentityServer4.M ...
- IdentityServer4密码模式接入现有用户数据表
具体接入identityserver请看文档,这里只简单列举部分步骤 1.创建一个web项目,引入Identityserver4的nuget包 2.新建一个类,实现IResourceOwnerPass ...
- 【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)
本站文章均为李华明Himi原创,转载务必在明显处注明:(作者新浪微博:@李华明Himi) 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-c ...
- 有关 Hybrid 开发模式实践总结
前言 随着公司业务不断发展,移动开发项目越来越多,项目任务时间紧,我们内部开发流程是以项目为导向,有别于一般公司对产品不断迭代的做法,但移动端开发人员资源有限,需要在不同项目之间做业务场景切换开发,就 ...
- Hybrid App—Hybrid App开发模式介绍和各种开发模式对比
什么是Hybrid App 最开的App开发只有原生开发这个概念,但自从H5广泛流行后,一种效率更高的开发模式Hybrid应运而生,它就是"Hybrid模式".Hybrid APP ...
- Hybrid App 开发模式
开发移动App主要有三种模式:Native. Hybrid 和 Web App. 需要注意的一点是在选择开发模式的时候,要根据你的项目类型(图片类?视频类?新闻类?等),产品业务和人员技术储备等做权衡 ...
随机推荐
- Docker Compose部署lnmp
参考:https://github.com/micooz/docker-lnmp 一.简介 使用Dcoekr镜像部署lnmp(Linux.Nginx.MySQL.PHP7). 1.1 结构 app └ ...
- ArcGIS 点云转DEM
- Leetcode--136. Single Number(easy)
Given a non-empty array of integers, every element appears twice except for one. Find that single on ...
- POJ1065--Wooden Sticks(动态规划)
There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The st ...
- Linux (rz、sz命令行)与本地电脑 命令行上传、下载文件
Linux 与本地电脑直接交互, 命令行上传.下载文件. 一.lrzsz命令行安装: 1.rpm安装:(链接: http://pan.baidu.com/s/1cBuTm2 密码: vijf) rpm ...
- day06_雷神_面向对象初识
day_06 递归函数 自己用自己.一般递归100多次,都没有解决的问题,放弃递归. count = 0 def func1(): global count count += 1 print(coun ...
- poj 2155 区间更新 单点查询
Matrix Time Limit: 3000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class ...
- Android-Java-synchronized同步锁机制&利与弊
synchronized同步锁机制 定义锁
- ThreadLocal实现线程级上下文
一.ThreadLocal测试 package com.junge.threadlocal.context; /** * @author Administrator * */ public class ...
- c#中的几种Dialog
1.OpenFileDialog private void FileOpen_Click(object sender, EventArgs e) { OpenFileDialog openFile = ...