首页 > 其他分享 >NetCore Identityserver4 客户端配置

NetCore Identityserver4 客户端配置

时间:2024-01-25 14:56:24浏览次数:25  
标签:Console NetCore client Result WriteLine var response Identityserver4 客户端

1.客户端模式

       var client = new HttpClient();

            var response = client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address = "http://localhost:5000/connect/token",
                ClientId = "client",
                ClientSecret = "secret",
                Scope = "api"
            }).Result;
            Console.WriteLine("=============================Token==============================");

            Console.WriteLine(response.AccessToken ?? response.Error);

            Console.WriteLine("=============================Token==============================");
            // 调用API
            client.SetBearerToken(response.AccessToken);

            var responses = client.GetAsync("http://localhost:5001/weatherforecast/get").Result;
            if (response.IsError)
            {
                Console.WriteLine(response.HttpStatusCode);
            }
            else
            {
                Console.WriteLine("=============================服务返回值==============================");
                var content = responses.Content.ReadAsStringAsync().Result;
               
                Console.WriteLine(content);
            }
            Console.WriteLine("Hello World!");
            Console.ReadKey();

2.密码模式

        var client = new HttpClient();

            var response = client.RequestPasswordTokenAsync(new PasswordTokenRequest
            {
                Address = "http://localhost:5000/connect/token",
                ClientId = "apiClientPassword",
                ClientSecret = "apiSecret",
                Scope = "secretapi",
                UserName="123",
                Password="123"
                
            }).Result;
            Console.WriteLine("=============================Token==============================");

            Console.WriteLine(response.AccessToken ?? response.Error);

            Console.WriteLine("=============================Token==============================");
            // 调用API
            client.SetBearerToken(response.AccessToken);

            var responses = client.GetAsync("http://localhost:5001/weatherforecast/get").Result;
            if (response.IsError)
            {
                Console.WriteLine(response.HttpStatusCode);
            }
            else
            {
                Console.WriteLine("=============================服务返回值==============================");
                var content = responses.Content.ReadAsStringAsync().Result;

                Console.WriteLine(content);
            }
            Console.WriteLine("Hello World!");
            Console.ReadKey();

 3.刷新refresh_token

        var client = new HttpClient();

            var response = client.RequestPasswordTokenAsync(new PasswordTokenRequest
            {
                Address = "http://localhost:5000/connect/token",
                ClientId = "apiClientPassword",
                ClientSecret = "apiSecret",
                Scope = "secretapi",
                UserName="123",
                Password="123"
                
            }).Result;
            Console.WriteLine("=============================Token==============================");

            Console.WriteLine(response.AccessToken ?? response.Error);

            Console.WriteLine("=============================Token==============================");
            // 调用API
            client.SetBearerToken(response.AccessToken);

            var responses = client.GetAsync("http://localhost:5001/weatherforecast/get").Result;
            if (response.IsError)
            {
                Console.WriteLine(response.HttpStatusCode);
            }
            else
            {
                Console.WriteLine("=============================服务返回值==============================");
                var content = responses.Content.ReadAsStringAsync().Result;

                Console.WriteLine(content);
            }
            Console.WriteLine("Hello World!");
            Console.ReadKey();

 

标签:Console,NetCore,client,Result,WriteLine,var,response,Identityserver4,客户端
From: https://www.cnblogs.com/microsoft-zh/p/17987148

相关文章

  • 如何手工制作绿色免安装单文件同花顺免费版Windows客户端 2024-01-25
    如何手工制作绿色免安装单文件同花顺免费版Windows客户端  2024-01-25第1步、下载同花顺免费版http://download.10jqka.com.cn/第2步、安装同花顺免费版第3步、移动同花顺免费版软件到文件夹 D:\Prog\同花顺第4步、新建批处理脚本文件 D:\Prog\同花顺\一键打包\一键打......
  • odbc客户端添加错误检查
    在本节中,我将添加一些简单的错误处理函数,并向您展示如何正确释放您创建的句柄资源。为了使您的ODBC编程更轻松一些,您可以使用以下函数来检查成功或失败:staticboolSQL_OK(SQLRETURNresult){ if(result==SQL_SUCCESS||result==SQL_SUCCESS_WITH_INFO) return(TRUE); els......
  • ODBC客户端查询处理
    本节,我将介绍一种新的句柄类型——SQLHSTMT语句句柄。SQLHSTMT的父级是连接句柄。您必须先释放所有子语句句柄,然后才能释放连接句柄。ODBC结果处理模型比其他PostgreSQLAPI更复杂。在libpq、libpq++和libpgeasyAPI中,您向服务器发送查询,然后调用函数来访问结果集中的每个......
  • odbc客户端连接到服务器
    让我们看一个示例代码client1.c。第一个客户端应用程序连接到数据库,然后退出。#include<sql.h>#include<sqlext.h>#include<stdio.h>#include<stdlib.h>intmain(intargc,char*argv[]){ SQLRETURNresult; SQLHENVhenv; SQLHDBChdbc; //1.申请......
  • NetCore获取文件内容
    1、注入IWebHostEnvironmentprivateIWebHostEnvironment_hostEnvironment;publicFormBaseController(IWebHostEnvironmenthostEnvironment){_hostEnvironment=hostEnvironment;} 2、读取根目录下文件内容(Templates/Index.html)publicasyncTask<IActionRes......
  • 服务器上mysql安装 ,以及客户端Navicat连接
    1.官网下载mysql8.0https://dev.mysql.com/downloads/installer/ 2.安装mysql8.0参考https://blog.csdn.net/weixin_47406082/article/details/131867849?ops_request_misc=&request_id=&biz_id=102&utm_term=mysql%E6%9C%80%E6%96%B0%E7%89%88%E5%AE%89%E8%A3%85%E......
  • 前端歌谣-第六十五课-express之服务端渲染和客户端渲染
    前言我是歌谣微信公众号关注前端小歌谣一起学习前端知识今天继续给大家讲解服务端渲染和客户端渲染静态资源的讲解案列index.html<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,init......
  • elasticsearchjava客户端
    elasticsearchjava客户端1.引用maven配置<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><exclusions><exclusion>&l......
  • 使用Go进行HTTP客户端认证
    在Go语言中,HTTP客户端认证可以通过net/http包来实现。下面是一个简单的示例,展示如何使用Go进行HTTP客户端认证。首先,确保你已经安装了Go语言环境,并设置好了相关的环境变量。Go语言中的HTTP客户端认证主要涉及到设置请求头中的认证信息。以下是一个简单的示例代码,展示了如何使用Go发......
  • k8s_client-go 构建客户端的几种方式
    kubernetesclient-go构建客户端的几种方式packagecallk8simport( "context" "log" metav1"k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/t......