繁体   English   中英

在 C# 中使用 Microsoft Graph 刷新令牌证书

[英]Use Certificate for Microsoft Graph Refresh Token in C#

我最近面临一个问题。 因为一开始我使用客户端密码来生成访问令牌和刷新令牌,如下所示:

  public TokenModel RefreshToken(string refreshToken, string tenantId, string clientId, string clientSecret)
        {
            string url = string.Format(TOKEN_ENDPOINT_URL, tenantId);

            Dictionary<string, string> values = new Dictionary<string, string>
            {
                { "client_id", clientId },
                { "scope", ALL_SCOPE_AUTHORIZATIONS },
                { "client_secret", clientSecret },
                { "grant_type", "refresh_token" },
                { "refresh_token", refreshToken }
            };

            FormUrlEncodedContent data = new FormUrlEncodedContent(values);

            HttpClient client = new HttpClient();
            HttpResponseMessage response = client.PostAsync(url, data).Result;
            string jsonToken = response.Content.ReadAsStringAsync().Result;
            return ExtractToken(jsonToken);
        }

  public TokenModel GetAccessTokenByAuthorizationCode(string authorizationCode, string tenantId, string clientId, string clientSecret, string redirectUrl)
        {
            string url = string.Format(TOKEN_ENDPOINT_URL, tenantId);

            Dictionary<string, string> values = new Dictionary<string, string>
            {
                { "client_id", clientId },
                { "scope", ALL_SCOPE_AUTHORIZATIONS },
                { "client_secret", clientSecret },
                { "grant_type", "authorization_code" },
                { "redirect_uri", "https://mycompany.com/" },
                { "code", authorizationCode }
            };

            FormUrlEncodedContent data = new FormUrlEncodedContent(values);

            HttpClient client = new HttpClient();
            HttpResponseMessage response = client.PostAsync(url, data).Result;
            string jsonToken = response.Content.ReadAsStringAsync().Result;
            return ExtractToken(jsonToken);
        }

TokenModel 包含访问令牌和刷新令牌

但是现在我需要通过证书并且永远不要使用客户端机密。 有人知道如何使用 HttpClient Request 执行此操作吗? 我已经阅读了文章: Microsoft Graph:如何在客户端凭据流中获取带有证书的访问令牌? (而不是使用 client_secret)

但我不知道如何在我的 C# 代码中生成 client_assertion

最好的问候阿德里安

用于生成访问和刷新令牌的代码部分

好吧,生成客户端断言是一项非常复杂的任务。 我找到了一些资源,您可以在其中找到 C# 的更多详细信息和示例。

创建签名断言

使用 Microsoft.IdentityModel.JsonWebTokens 创建断言的替代方法

断言格式

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM