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模式客户端调用不一样的是

var tokenResponse = tokenClient.RequestResourceOwnerPasswordAsync("qinzb","123456","api").Result; //就这个地方和调用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实现的更多相关文章

  1. 【ASP.NET Core分布式项目实战】(一)IdentityServer4登录中心、oauth密码模式identity server4实现

    本博客根据http://video.jessetalk.cn/my/course/5视频整理 资料 OAuth2 流程:http://www.ruanyifeng.com/blog/2014/05/o ...

  2. ASP.NET Core分布式项目-2.oauth密码模式identity server4实现

    源码下载 这里根据<ASP.NET Core分布式项目-1.IdentityServer4登录中心>的代码来继续更新oauth密码模式,这里的密码模式比上次的客户端模式更安全 在WebAp ...

  3. OAuth密码模式说明(resource owner password credentials)

    用户向客户端(third party application)提供用户名和密码. 客户端将用户名和密码发给认证服务器(Authorization server),向后者请求令牌(token). 认证服 ...

  4. 【7】.net WebAPI Owin OAuth 2.0 密码模式验证实例

    1.OAuth密码模式 2.在VS中创建WebAPI项目 在nuget中安装: Microsoft.AspNet.WebApi.Owin Microsoft.Owin.Host.SystemWeb 这 ...

  5. IdentityServer4 密码模式认证

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

  6. IdentityServer4 密码模式实现

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

  7. 基于 IdentityServer3 实现 OAuth 2.0 授权服务【密码模式(Resource Owner Password Credentials)】

    密码模式(Resource Owner Password Credentials Grant)中,用户向客户端提供自己的用户名和密码.客户端使用这些信息,向"服务商提供商"索要授权 ...

  8. Identity Server4学习系列四之用户名密码获得访问令牌

    1.简介 Identity Server4支持用户名密码模式,允许调用客户端使用用户名密码来获得访问Api资源(遵循Auth 2.0协议)的Access Token,MS可能考虑兼容老的系统,实现了这 ...

  9. 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权限认证 ...

随机推荐

  1. yii2.0中解决post的400错误

    不想用gii的表单自己写表单,但是又遇到了400错误,怎么解决?下面为你解答一下:

  2. zabbix 添加 ROS 软路由监控 WinBox设置

    如图设置

  3. SAP CRM系统订单模型的设计与实现

    SAP成都研究院的一个部门领导让我给他的团队做一个SAP CRM One Order框架的培训,这是我准备的培训内容. 在Jerry之前的文章 基于SAP Kyma的订单编排增强介绍,我表达了自己对S ...

  4. 搞定INTEL快速存储技术(用SSD硬盘做缓存加速)

    给朋友买了个联想 ideapad s400超级本,还真是锻炼我的idea啊,原机不带WIN7系统,所以只好自己动手装WIN7,并打开24G SSD硬盘做缓存. 一.用常规方法GHOST了一个WIN7系 ...

  5. ethereumjs/ethereumjs-account-1-简介和API

    https://github.com/ethereumjs/ethereumjs-account Encoding, decoding and validation of Ethereum's Acc ...

  6. 决策树在sklearn中的实现

    1 概述 1.1 决策树是如何工作的 1.2 构建决策树 1.2.1 ID3算法构建决策树 1.2.2 简单实例 1.2.3 ID3的局限性 1.3 C4.5算法 & CART算法 1.3.1 ...

  7. webstorm window找不到文件'chrome'

    1.打开webstorm设置: File->Settings->Tools->Web Browsers->更改谷歌浏览器的Path(获取方式:谷歌浏览器的快捷键->右键- ...

  8. 【dp】最大乘积

    题目描述] 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友XZ也有幸 ...

  9. enmu枚举类型

    在实际问题中,有些变量的取值被限定在一个有限的范围内.例如,一个星期内只有七天,一年只有十二个月,一个班每周有六门课程等等.如果把这些量说明为整型,字符型或其它类型显然是不妥当的.为此,C语言提供了一 ...

  10. iOS 文件下载及断点续传

    ios的下载我们可以使用的方法有:NSData.NSURLConnection.NSURLSession还有第三方框架AFNetworking和ASI 利用NSData方法和NSURLConnecti ...