如何在MSGRAPH .NET SDK中设置用户代理字符串

发布于 2025-01-27 06:51:49 字数 776 浏览 2 评论 0原文

我正在使用C#中的MSGraph SDK v4.27.0读取客户电子邮件帐户。它运行良好,但一位客户坚持使用允许清单来访问电子邮件。授予对提供用户代理字符串的应用程序的访问,但是如何使用SDK调用将其包含在MSGraph标题中?
代码取自MS示例

var scopes = new[] { "User.Read","Mail.ReadWrite","Mail.ReadWrite.Shared" };

var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
            };

var userName = strAccount;

var password = strPWD ;


var userNamePasswordCredential = new UsernamePasswordCredential(
                userName, password, tenantId, theClientId, options);


var graphClient = new GraphServiceClient(userNamePasswordCredential, scopes);

try
{
   rootFolder = await graphClient.Me.MailFolders["msgfolderroot"]
               .Request()
               .GetAsync();
}..

I'm reading customer email accounts using the MSGraph SDK v4.27.0 in C#. It works fine but one customer insists on using allowlists for EWS access to email. That grants access to apps that supply a User Agent string but how do I include it in the MSGraph header using the SDK calls?
The code is taken from the MS example

var scopes = new[] { "User.Read","Mail.ReadWrite","Mail.ReadWrite.Shared" };

var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
            };

var userName = strAccount;

var password = strPWD ;


var userNamePasswordCredential = new UsernamePasswordCredential(
                userName, password, tenantId, theClientId, options);


var graphClient = new GraphServiceClient(userNamePasswordCredential, scopes);

try
{
   rootFolder = await graphClient.Me.MailFolders["msgfolderroot"]
               .Request()
               .GetAsync();
}..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

八巷 2025-02-03 06:51:49

请注意,V5的此更改

是通过标头,不再使用标题类。使用RequestConfiguration修饰符添加标题如下

var user = await graphServiceClient
.Users["{user-id}"]
.GetAsync(requestConfiguration => requestConfiguration.Headers.Add("ConsistencyLevel","eventual"));

Note this change in v5

To pass headers, the HeaderOption class is no longer used. Headers are added using the requestConfiguration modifier as follows

var user = await graphServiceClient
.Users["{user-id}"]
.GetAsync(requestConfiguration => requestConfiguration.Headers.Add("ConsistencyLevel","eventual"));
浮生未歇 2025-02-03 06:51:49

最终发现了GitHubs文档的答案,尽管确实进行了一些实验,发现标题选项名称为“用户代理”
您创建一个选项列表并添加在用户代理选项中

 List<Option> theOptions = new List<Option>();
        theOptions.Add(new HeaderOption("User-Agent", "MyUserAgentName"));

,然后每个.request()调用必须将选项作为参数

 rootFolder = await graphClient.Me.MailFolders["msgfolderroot"]
                .Request(theOptions)
                .GetAsync();

Found the answer from the GitHubs docs eventually although it did take some experimentation finding out that the header option name is "User-Agent"
You create an options list and add in the user agent option

 List<Option> theOptions = new List<Option>();
        theOptions.Add(new HeaderOption("User-Agent", "MyUserAgentName"));

Then every .Request() call has to have the options as a parameter e.g.

 rootFolder = await graphClient.Me.MailFolders["msgfolderroot"]
                .Request(theOptions)
                .GetAsync();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文