ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

id4 使用 password 授权

2022-01-24 23:03:21  阅读:188  来源: 互联网

标签:res id4 token client using 授权 password scope HttpClient


1 添加 client

设置clientid,添加client密钥,设置授权类型为 “passwrod”,设置允许访问的scope(作用域) 至少添加 openid 不然 无法调用 userinfo 端点,也应该至少添加一个 api 资源关联的 scope,不然请求到的 token只能访问 identity resource (即 openid,role,profile 等等)的信息,无法访问 api resource 。

2 使用httpclient请求代码如下

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;

namespace ConsoleClient
{
    /// <summary>
    /// 使用 HttpClient 通过密码方式获取token和资源
    /// 适用场景:一般用于兼容老应用,这种方式需要用户给出自己的用户名/密码,显然风险很大,因此只适用于其他授权方式都无法采用的情况
    /// </summary>
    public class HttpClient_Password
    {
        public static void Run()
        {
            // 先获取 access token
            // 如果不设置 scope 则获取所有允许的scope
            var token_res = string.Empty;
            /// 请求token链接格式:{idsServer}/connect/token?client_id={}&client_secret={}username={}&password={}&grant_type=password
            string postData = $"client_id=password&client_secret=123456&username=admin&password=@123456Ty&grant_type=password";
            using (var httpClient = new HttpClient())
            {
                using (HttpContent httpContent = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(postData))))
                {
                    httpContent.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                    token_res = httpClient.PostAsync($"{Config.IdentityServerUri}/connect/token", httpContent).Result.Content.ReadAsStringAsync().Result;
                }
            }

            // 获取 userinfo 端点用户信息 一定要包含 openid scope 作用域
            if(JObject.Parse(token_res)["error"]?.ToString() == null)
            {
                using (HttpClient httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + JObject.Parse(token_res)["access_token"].ToString());
                    // 有token就能访问
                    var userInfoRes = httpClient.GetStringAsync($"{Config.IdentityServerUri}/connect/userinfo").Result;
                }
            }

            // 使用 token 访问资源
            if (JObject.Parse(token_res)["error"]?.ToString() == null)
            {
                using (HttpClient getClient = new HttpClient())
                {
                    getClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + JObject.Parse(token_res)["access_token"].ToString());
                    // 有token就能访问
                    var apiRes1 = getClient.GetStringAsync($"{Config.ResourceUri}/Test/Ping").Result;
                    // 有token就能访问且 client allowedscope 包含 client_credentials_apis.IdentityUserController.scope 才能访问
                    var apiRes2 = getClient.GetStringAsync($"{Config.ResourceUri}/IdentityUser/Ping").Result;
                    //// 有token就能访问且 client allowedscope 包含 client_credentials_apis.WeatherForecastController.scope 才能访问
                    //var res_res3 = getClient.GetStringAsync($"{Config.ResourceUri}/WeatherForecast/Ping").Result;
                }
            }
        }
    }
}

 

标签:res,id4,token,client,using,授权,password,scope,HttpClient
来源: https://www.cnblogs.com/tomorrow0/p/15841268.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有