簡體   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