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. 允许远程链接mysql,开放3306端口

    首先查看端口是否打开 netstat -an|grep 3306 此图为开启3306端口的截图,之前显示为. . . 127.0.0.1:3306 . . . 打开mysql配置文件vi /etc/m ...

  2. CIKM 2013推荐系统论文总结

    这几天在家没事,介绍几篇CIKM上关于推荐系统的文章, Personalized Influence Maximization on Social Networks Social Recommenda ...

  3. windows 安装redis并注册服务

        windows下载地址 https://github.com/MSOpenTech/redis/releases     启动:redis-server redis.windows.conf ...

  4. Visual Studio中头文件stdafx.h的作用

    在较新版的Visual Studio中,新生成的C++项目文件的的头文件夹下会默认有头文件stdafx.h,而源文件夹下则默认有源文件stdafx.cpp,手动将这些文件删除后,编译时系统还会报错.下 ...

  5. 一对一关联关系基于主键映射的异常 IdentifierGenerationException

    具体异常:org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one pro ...

  6. R在Centos下安装

    R语言是主要用于统计分析.绘图的语言和操作环境. 官方网站: http://www.r-project.org/ Windows下面有直接的安装包,直接下载安装很方便,但是对于刚出的CentOS6.0 ...

  7. 转:git 的常用命令

    转自:阮一峰 常用git命令清单 一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下. Wor ...

  8. C++自定义函数

    函数可以看成是用户为了解决某特定问题而定义的操作.除了在C++语法里自带的函数,有时候我们需要处理某个问题.但C++里并没有这个函数,这时我们就可以自己来定义一个函数实现需要的功能. 比如我们定义个求 ...

  9. win8开发

    http://msdn.microsoft.com/library/default.aspx

  10. oracle database 9i/10g/11g 编程艺术 源代码下载

    背景 在找这本书的源码,搜到提供的都是需要C币下载的.比较固执(其实是穷). 在这本书的前言中提到源代码可以在 www.appress.com 上下载. 下面是该书在该网站上的链接: https:// ...