为什么我的 GraphServiceClient 在每次 API 调用时都要重新进行身份验证?

2024-03-04

I am using Microsoft Graph API to call some endpoints. I am using the SDK for C#. When opening a fiddler trace, I found out that my _graphClientService is issuing an authentication to get a new token at every call. Why would that happen and how to prevent it? enter image description here

它也在某些调用中导致此错误。

AADSTS50196: The server terminated an operation because it encountered a client request loop

看起来这段代码可以工作。它生成一个 GraphServiceClient,在每次调用时重用相同的令牌,而不是生成新的令牌。

 public GraphServiceClient GenerateGraphUserClient()
    {
        string userToken = GetUserAccessToken();
        GraphServiceClient client= new GraphServiceClient("https://graph.microsoft.com/v1.0", new DelegateAuthenticationProvider(async (requestMessage) =>
        {
            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", userToken);
        }));
        return client;
    }

public string GetUserAccessToken()
    {
        string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
        IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
        .Create(_clientId)
        .WithTenantId(_domain)
        .Build();
        var securePassword = new SecureString();
        foreach (char c in _password)
            securePassword.AppendChar(c);
        var result = publicClientApplication.AcquireTokenByUsernamePassword(scopes, _userName, securePassword).ExecuteAsync().Result;
        return result.AccessToken;
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我的 GraphServiceClient 在每次 API 调用时都要重新进行身份验证? 的相关文章

随机推荐