10.第三方ClientCredential模式调用
<div id="post_detail">
10.第三方ClientCredential模式调用
10.第三方ClientCredential模式调用
IdentityModel的官方文档:
https://identitymodel.readthedocs.io/en/latest/index.html
ThirdPartyDemo
创建第三方的应用程序,相当于它来请求我们的API
创建控制台的应用程序
dotnet new console --name ThirdPartyDemo:常见控制台程序ThirdPartyDemo
添加nuget包:IdentityModel
首先我们需要访问以下这个IdentityServer,是否可以来实现
运行测试
运行:IdentityServerSample
D:\MyDemos\jesse\IdentityServerSample\IdentityServerCenter
在运行:ClientCredentialApi
D:\MyDemos\jesse\ClientCredentialApi
在运行我们的控制台应用程序
修正代码
继续代码,运行结果
这样access_token就返回了。还有我们的api/Values里面输出的值
D:\MyDemos\jesse\ThirdPartyDemo>dotnet run
Program.cs(11,24): warning CS0618: '“DiscoveryClient”已过时:“This type will be deprecated or changed in a future version. It is recommended that you switch to the new extension methods for HttpClient. They give you much more control over the HttpClient lifetime and configuration. See the docs here: https://identitymodel.readthedocs.io” [D:\MyDemos\jesse\ThirdPartyDemo\ThirdPartyDemo.csproj]
Program.cs(16,35): warning CS0618: '“TokenClient”已过时:“This type will be deprecated or changed in a future version. It is recommended that you switch to the new extension methods for HttpClient. They give you much more control over the HttpClient lifetime and configuration. See the docs here: https://identitymodel.readthedocs.io” [D:\MyDemos\jesse\ThirdPartyDemo\ThirdPartyDemo.csproj]
{
"access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6ImE3MGZkOGQyYjVjMmVlNDUzMWU1ZGUyNWJmYTViNmE4IiwidHlwIjoiSldUIn0.eyJuYmYiOjE1NTIyNzk3MzAsImV4cCI6MTU1MjI4MzMzMCwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwIiwiYXVkIjpbImh0dHA6Ly9sb2NhbGhvc3Q6NTAwMC9yZXNvdXJjZXMiLCJhcGkiXSwiY2xpZW50X2lkIjoiY2xpZW50Iiwic2NvcGUiOlsiYXBpIl19.hPUdUBWxUd2R3xHb4rfGgLFx4Y5KtfK3NFLf3pICzyYnpI8gcfvyzwzWFkY1ZNCwDwq8KVGZsPe_Yu6hRYvlVk8-vsJTXC4W0UJnfEmlFABDfXkao_LlyVZ7ULksg8gZbPje0AVqLtiyKWl66E4iYvsBRfuTBsTVYzStFO4-g2GNsfsyK6rc0iugQo9Gw9hQG3wpumvnq7LJI2SG42GzoGqhWbHvAj5JLvmOY5Mh0ccNR971Z4Q97pp_DXoFSqaLIPfuLN3gD16iQVMzSGMv6tawtpoGZu3XygpOR6T70rXBNTw0StWSdXGyrvI5j3ROKgWy8m9QflzaWr3ElN9Qzw",
"expires_in": 3600,
"token_type": "Bearer"
}
["value1","value2"]
流程
1.先检测地址是否正常:
2.然后通过tokenClient去拿到我们的token
3.拿到token后用httpClient访问我们的API
这就是通过第三方应用程序代码的形式,实现了API的token的获取和api的请求
using System; using IdentityModel.Client; using System.Net.Http;namespace ThirdPartyDemo
{
class Program
{
static void Main(string[] args)
{
var diso = DiscoveryClient.GetAsync("http://localhost:5000").Result;
if (diso.IsError)
{
Console.WriteLine(diso.Error);
}
var tokenClient = new TokenClient(diso.TokenEndpoint, "client", "secret");
var tokenResponse = tokenClient.RequestClientCredentialsAsync("api").Result;
if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
}
else
{
Console.WriteLine(tokenResponse.Json);//输出返回的json
}
//获取到token 相当于从认证中心拿到了许可 再去访问 api的程序
var httpClient = new HttpClient();
httpClient.SetBearerToken(tokenResponse.AccessToken);
var respone = httpClient.GetAsync("http://localhost:5001/api/values").Result;
if (respone.IsSuccessStatusCode)
{
Console.WriteLine(respone.Content.ReadAsStringAsync().Result);
}
Console.WriteLine();
}
}
}
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
</div>
<div class="postDesc">posted @ <span id="post-date">2019-03-11 01:05</span> <a href="https://www.cnblogs.com/wangjunwei/">GASA</a> 阅读(<span id="post_view_count">28</span>) 评论(<span id="post_comment_count">0</span>) <a href="https://i.cnblogs.com/EditPosts.aspx?postid=10508279" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(10508279);return false;">收藏</a></div>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=60752,cb_entryId=10508279,cb_blogApp=currentBlogApp,cb_blogUserGuid='29a68de3-869e-de11-ba8f-001cf0cd104b',cb_entryCreatedDate='2019/3/11 1:05:00';loadViewCount(cb_entryId);var cb_postType=1;var isMarkdown=false;</script>
</div>
昵称:
不改了 退出 订阅评论
[Ctrl+Enter快捷键提交]
【活动】戴尔助力小企业,商务爆品5折秒,最低低至¥2399
【推荐】程序员问答平台,解决您开发中遇到的技术难题
· 10-第三方ClientCredential模式调用
· .NET设计模式(10):装饰模式(Decorator Pattern)
· java代码调用第三方接口
· Java异步调用模式
· Java设计模式10:观察者模式
· 西数宣布闪存工厂断电事故生产已经恢复 损失3.4亿美元
· AMD CEO Lisa Su 更高端的Navi显卡已经在路上了
· 波兰科研团队称银河系是弯的,网友:呵呵,中国早就发现了!
· 日本公司开发手机气味传感器:性能媲美犬类
· 85%的比特币已被开采 目前仅剩300多万枚比特币
» 更多新闻...
2014-03-11 8 个很有用的 jQuery 技巧(转)