美文网首页
IdentityServer4调用获取token接口/conne

IdentityServer4调用获取token接口/conne

作者: Messix_1102 | 来源:发表于2021-07-13 18:26 被阅读0次

当调用IdentityServer4认证服务获取token时,请求头参数Authorization需要通过处理客户端 id 和 secret 字符串来获得,获取方法如下:

请求token结果
    static void Main(string[] args)
    {
        // 获取请求 接口 /connect/token 的Header参数 Authorization 的值
        var res = new BasicAuthenticationHeaderValue("ro.client", "secret");
        Console.WriteLine(res.ToString());
    }

    /// <summary>
    /// HTTP Basic Authentication authorization header
    /// </summary>
    /// <seealso cref="System.Net.Http.Headers.AuthenticationHeaderValue" />
    public class BasicAuthenticationHeaderValue : AuthenticationHeaderValue
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="BasicAuthenticationHeaderValue"/> class.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        public BasicAuthenticationHeaderValue(string userName, string password)
            : base("Basic", EncodeCredential(userName, password))
        { }

        /// <summary>
        /// Encodes the credential.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">userName</exception>
        public static string EncodeCredential(string userName, string password)
        {
            if (string.IsNullOrWhiteSpace(userName)) throw new ArgumentNullException(nameof(userName));
            if (password == null) password = string.Empty;

            Encoding encoding = Encoding.UTF8;
            string credential = String.Format("{0}:{1}", userName, password);

            return Convert.ToBase64String(encoding.GetBytes(credential));
        }
    }

相关文章

网友评论

      本文标题:IdentityServer4调用获取token接口/conne

      本文链接:https://www.haomeiwen.com/subject/vcmspltx.html