通过密码访问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. Altium Designer 19 单层显示

    PCB视图下,按快捷键L 弹出对话框 选择 view options 选项卡 Single Layer Mode 点击为on 快捷键为Shift + s

  2. 【leetcode】981. Time Based Key-Value Store

    题目如下: Create a timebased key-value store class TimeMap, that supports two operations. 1. set(string ...

  3. kafka-server.properties

    # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreement ...

  4. 【Linux】清理缓存buffer/cache

    运行sync将dirty的内容写回硬盘 sync 通过修改proc系统的drop_caches清理free的cache echo 3 > /proc/sys/vm/drop_caches ech ...

  5. paper 154:姿态估计(Hand Pose Estimation)相关总结

    Awesome Works  !!!! Table of Contents Conference Papers 2017 ICCV 2017 CVPR 2017 Others 2016 ECCV 20 ...

  6. bugku | 你从哪里来

    题目链接 之前一直以为要用x-forwarded-for ,谁道用的是referer,Orz.在此特地记录,x-forwarded-for 和 referer的区别 X-Forwarded-For(X ...

  7. window安装activemq

    原文: https://www.cnblogs.com/donsenChen/p/8656563.html ActiveMQ5.14.5下载地址 http://activemq.apache.org/ ...

  8. iOS 获取全局唯一标示符

    这个方法用来产生一个唯一的标示符,每次调用都会不一样,所以可以用当作一些临时缓存文件的名字 NSString *identifier = [[NSProcessInfo processInfo] gl ...

  9. 解决Mac下使用root 权限依旧无法读写文件的问题

    当时在学习selenium的时候,需要配合使用chromedriver 和phantomjs 进行浏览器的自动化测试.. chromedriver下载结束后.无法移动到/user/bin下面 会提示权 ...

  10. 如何在Oracle中建表空间、建用户并导入dmp文件详解

    假设oracle有个全新的数据库orcl,现在要把数据库文件(.dmp)导入这个全新的数据库orcl中.详细步骤如下:    1. 创建表空间  例如:  create tablespace test ...