通过密码访问API

一、客户端

图:

客户端请求代码:

        static void Main(string[] args)
{
Console.WriteLine("确定三个项目都已经启动");
Console.Read();
Console.WriteLine("按任意键开始运行");
// discover endpoints from metadata
var client = new HttpClient();
var disco = client.GetDiscoveryDocumentAsync("http://localhost:5000").ConfigureAwait(false).GetAwaiter().GetResult();
if (disco.IsError)
{
Console.WriteLine(disco.Error);
return;
}
// request token
var tokenResponse = client.RequestPasswordTokenAsync(new PasswordTokenRequest
{
Address = disco.TokenEndpoint,//默认
ClientId = "socialnetwork",
ClientSecret = "secret",
//GrantType = "password",//这句话可以加也可以不加 与资源服务器对应 AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
UserName = "mail@qq.com",
Password = "password",
Scope = "socialnetwork",
}).ConfigureAwait(false).GetAwaiter().GetResult();
// call api
client.SetBearerToken(tokenResponse.AccessToken);
var response = client.GetAsync("http://localhost:5001/identity").ConfigureAwait(false).GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
}
else
{
var content = response.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
Console.WriteLine(JArray.Parse(content));
}
Console.ReadKey(false);//因为控制台会关闭,设置不关闭
}

二、颁发token服务器

服务端配置文件必须添加

    public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
var builder = services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.ApiResources())
.AddInMemoryClients(Config.Clients())//添加客户端
.AddTestUsers(Config.Users().ToList());//添加用户支持
// rest omitted
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseIdentityServer(); app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}

如果不加则是

五、通过密码访问API的更多相关文章

  1. Identity Server 4资源拥有者密码认证控制访问API

    基于上一篇文章中的代码进行继续延伸,只需要小小的改动即可,不明白的地方可以先看看本人上一篇文章及源码: Identity Server 4客户端认证控制访问API 一.QuickStartIdenti ...

  2. C# 带用户密码访问网络共享

    原文:C# 带用户密码访问网络共享 调用WNetUseConnection API 函数详细参数参考:https://msdn.microsoft.com/en-us/library/windows/ ...

  3. Python第五天 文件访问 for循环访问文件 while循环访问文件 字符串的startswith函数和split函数 linecache模块

    Python第五天   文件访问    for循环访问文件    while循环访问文件   字符串的startswith函数和split函数  linecache模块 目录 Pycharm使用技巧( ...

  4. 五分钟入门 Dingo API

    基于 https://laravel-china.org/doc... 文档更简洁的描述Dingo,直戳重点,注重实践 Django-Book 概述 Dingo API帮助您轻松快速地构建自己的API ...

  5. realvnc viewer 5.3.2无需输入用户名和密码访问远程桌面

    我从https://www.realvnc.com/download/viewer/下载了realvnc viewer用于访问远程的Linux桌面,这个版本不需要安装,直接运行就可以了.但在访问远程桌 ...

  6. IdnentiyServer-使用客户端凭据访问API

    情景如下:一个客户端要访问一个api,不需要用户登录,但是又不想直接暴露api给外部使用,这时可以使用identityserver添加访问权限. 客户端通过clientid和secrect访问iden ...

  7. 【精】搭建redis cluster集群,JedisCluster带密码访问【解决当中各种坑】!

    转: [精]搭建redis cluster集群,JedisCluster带密码访问[解决当中各种坑]! 2017年05月09日 00:13:18 冉椿林博客 阅读数:18208  版权声明:本文为博主 ...

  8. Win10共享打印机所需要的设置(无需密码访问实现打印机共享,共享不要密码)

    原文:https://m.baidu.com/from=1086k/bd_page_type=1/ssid=0/uid=0/pu=usm@0,sz@1320_1002,ta@iphone_2_5.1_ ...

  9. [IBM][CLI Driver][DB2/NT] SQL1101N 不能以指定的授权标识和密码访问节点 "" 上的远程数据库 "LBZM"。 SQLSTATE=08004

    [IBM][CLI Driver][DB2/NT] SQL1101N  不能以指定的授权标识和密码访问节点 "" 上的远程数据库  "LBZM".  SQLST ...

随机推荐

  1. nutz包的学习

    参考资料: 1.http://www.nutzam.com/core/nutz_preface.html

  2. FORTRAN和C语言数组循环顺序

    对于数组 A(I,J,K) FORTRAN中的循环次序应该是 K J I C语言的循环次序应该是I J K

  3. 【leetcode】336. Palindrome Pairs

    题目如下: 解题思路:对于任意一个word,要找出在wordlist中是否存在与之能组成回文的其他words,有两种思路.一是遍历wordlist:二是对word本身进行分析,找出能组成回文的word ...

  4. React笔记02——React中的组件

    一个网页可以被拆分成若干小部分,每个部分都可以称为组件,即组件是网页中的一部分.组件中还可以有多个组件. 上一节中的App.js就是一个组件(继承了React.Component类的类). 一个组件的 ...

  5. django+nginx+uwsgi_cent0s7.4 部署

    django+nginx+uwsgi_cent0s7.4 部署 几条命令 # 查看是否有 uwsgi 相关的进程 ps -aux|grep "uwsgi" # 杀死有关 uwsgi ...

  6. Mybatis的运行原理

    我们写的sql语句就在statementBuilder中. 整个mapper的信息都保存到configuration 1:根据配置文件创建SQLSessionFactory 先创建SqlSession ...

  7. Yii2 $app总结

    //验证登陆 Yii::$app->user->isGuest; //当前user的id Yii::$app->user->id; //当前controller的名称 Yii: ...

  8. shell脚本学习(4)cut

    cut 的两种用法 1种是 -c list   剪切字符串中特定位置的文字, /etc/passwd中的原始数据: yuyuyu:x:1000:1000:yuyuyu,,,:/home/yuyuyu: ...

  9. PWA 应用

    1. 使用例子,vue官网,在手机浏览器器打开时,保存在桌面那个应用.还有饿了么网站也是PWA应用.

  10. 2018-2019-2 20175126谢文航 实验三《敏捷开发与XP实践》实验报告

    一.实验报告封面 课程:Java程序设计 班级:1751 班 姓名:谢文航 学号:20175126 指导教师:娄嘉鹏 实验日期:2019年5月2日 实验时间:--- 实验序号:实验三 实验名称:敏捷开 ...