12-oauth密码模式identity server4实现
1-服务端代码, 配置类,可加 RequireClientSecret=false, 这样调用端就不需要传入client_secret参数
using System.Collections;
using System.Collections.Generic;
using IdentityServer4.Models;
using IdentityServer4.Test; namespace IdentityServerCenter{
public class Config{
public static IEnumerable<ApiResource> GetResources(){
return new List<ApiResource>(){
new ApiResource("api","My Api")
};
} public static IEnumerable<Client> GetClients(){
return new List<Client>(){
new Client(){
ClientId="client",
AllowedGrantTypes=GrantTypes.ClientCredentials, ClientSecrets = {
new Secret("secret".Sha256())
},
AllowedScopes = {"api"}
},
new Client(){
ClientId="pwdClient",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, ClientSecrets= {
new Secret("secret".Sha256())
},
AllowedScopes={"api"} }
};
} public static List<TestUser> GetTestUsers(){
return new List<TestUser>(){
new TestUser(){
SubjectId="",
Username="qinzb",
Password=""
}
};
}
}
}
2-在Start.up.cs增加 .AddTestUsers(Config.GetTestUsers()) ;用于测试用户
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetResources())
.AddInMemoryClients(Config.GetClients())
.AddTestUsers(Config.GetTestUsers()) ; services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
3-客户端代码, 与 ClientCredential模式客户端调用不一样的是
using System;
using IdentityModel;
using IdentityModel.Client;
using System.Net.Http;
namespace pwdClient
{
class Program
{
static void Main(string[] args)
{
var discoveryClient = DiscoveryClient.GetAsync("http://localhost:5000").Result;
if(discoveryClient.IsError){
Console.WriteLine("discoveryClient: "+discoveryClient.Error);
return;
} TokenClient tokenClient = new TokenClient(discoveryClient.TokenEndpoint,"pwdClient","secret");
var tokenResponse = tokenClient.RequestResourceOwnerPasswordAsync("qinzb","","api").Result; //就这个地方和调用ClientCredential模式不一样
if(tokenResponse.IsError){
Console.WriteLine(tokenResponse.Error);
}
Console.WriteLine(tokenResponse.Json); HttpClient httpClient = new HttpClient();
httpClient.SetBearerToken(tokenResponse.AccessToken); var response = httpClient.GetAsync("http://localhost:5001/api/values").Result;
string result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
}
}
}
12-oauth密码模式identity server4实现的更多相关文章
- 【ASP.NET Core分布式项目实战】(一)IdentityServer4登录中心、oauth密码模式identity server4实现
本博客根据http://video.jessetalk.cn/my/course/5视频整理 资料 OAuth2 流程:http://www.ruanyifeng.com/blog/2014/05/o ...
- ASP.NET Core分布式项目-2.oauth密码模式identity server4实现
源码下载 这里根据<ASP.NET Core分布式项目-1.IdentityServer4登录中心>的代码来继续更新oauth密码模式,这里的密码模式比上次的客户端模式更安全 在WebAp ...
- OAuth密码模式说明(resource owner password credentials)
用户向客户端(third party application)提供用户名和密码. 客户端将用户名和密码发给认证服务器(Authorization server),向后者请求令牌(token). 认证服 ...
- 【7】.net WebAPI Owin OAuth 2.0 密码模式验证实例
1.OAuth密码模式 2.在VS中创建WebAPI项目 在nuget中安装: Microsoft.AspNet.WebApi.Owin Microsoft.Owin.Host.SystemWeb 这 ...
- IdentityServer4 密码模式认证
授权服务器设置 添加用户 添加测试用户,也可以从数据库查 public static List<TestUser> GetTestUser() { return new List< ...
- IdentityServer4 密码模式实现
1. 修改 Config.cs using System.Collections; using System.Collections.Generic; using IdentityServer4.M ...
- 基于 IdentityServer3 实现 OAuth 2.0 授权服务【密码模式(Resource Owner Password Credentials)】
密码模式(Resource Owner Password Credentials Grant)中,用户向客户端提供自己的用户名和密码.客户端使用这些信息,向"服务商提供商"索要授权 ...
- Identity Server4学习系列四之用户名密码获得访问令牌
1.简介 Identity Server4支持用户名密码模式,允许调用客户端使用用户名密码来获得访问Api资源(遵循Auth 2.0协议)的Access Token,MS可能考虑兼容老的系统,实现了这 ...
- asp.net权限认证:OWIN实现OAuth 2.0 之密码模式(Resource Owner Password Credential)
asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...
随机推荐
- web.config如何实现301跳转
.htaccess的301定向非常简单,那么web.config的301定向又应该怎么实现呢? 先来看下,web.config中的301格式 <?xml version="1.0&qu ...
- jQuery ajax从后台取不到数据
ajax post data 获取不到数据,注意 content-type的设置 .post/get 关于 jQuery data 传递数据.网上各种获取不到数据,乱码之类的. 好吧今天我也遇到了 ...
- June 22nd 2017 Week 25th Thursday
Happiness is when the desolated soul meets love. 幸福是孤寂的灵魂遭遇爱的邂逅. When living alone for a long period ...
- 利用Underscore求数组的交集、并集和差集
1 数组交集函数——intersection 数组的交集是指包含多个数组中的共同元素的一个数组,求数组的交集就是找出给定数组中的共有元素. 下面实现一个求两个数组交集的函数. 判断数组是够包含指定值, ...
- 大数因式分解 Pollard_rho 算法详解
给你一个大数n,将它分解它的质因子的乘积的形式. 首先需要了解Miller_rabin判断一个数是否是素数 大数分解最简单的思想也是试除法,这里就不再展示代码了,就是从2到sqrt(n),一个一个的试 ...
- 正则工具类 -- RegexUtils
import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util. ...
- Java---页面之间传值跳转
从首页A进入页面B,然后从B页面登录,成功后跳转到A页面,并打印一句话“登录成功”,传值需要用的后台的. 在B页面写: <% session.setAttribute("key ...
- 【[AHOI2005]病毒检测】
\(Trie\) 树+搜索 我用的是\(dfs\) 首先对于将所有的RNA片段都建到\(Trie\)树里去,之后来匹配那个模板串就好了 如果是匹配的位置是字母,那么我们就继续往下匹配 如果是\(?\) ...
- Linux学习总结(十四) 文件的打包和压缩
文件的压缩和打包,在windos下我们很熟悉.rar和.zip文件,这是两种压缩文件,他们支持单个文件和多个文件的压缩.windos下我们不提及打包的概念,虽然多个文件的压缩肯定存在打包过程.打包和压 ...
- tensorflow训练代码
from tensorflow.examples.tutorials.mnist import input_data import tensorflow as tf mnist = input_dat ...