如何处理系统。

发布于 2025-01-27 14:31:12 字数 4664 浏览 3 评论 0 原文

首先,是C#&的新手VB.NET开发。

在尝试连接到Exchange Server时,会收到以下错误。

System.PlatformNotSupportedException
  HResult=0x80131539
  Message=Confidential Client flows are not available on mobile platforms or on Mac.See https://aka.ms/msal-net-confidential-availability for details.
  Source=Microsoft.Identity.Client
  StackTrace:
   at Microsoft.Identity.Client.ConfidentialClientApplicationBuilder.Create(String clientId)
   at DataLoadLibrary.Email.OAuthTokenProviders.<connect_to_exchange>d__1.MoveNext() in C:\Users\path\to\file\OAuthTokenProviders.cs:line 20

我指的是

using System;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Identity.Client;



namespace DataLoadLibrary.Email
{
    public class OAuthTokenProviders
    {
        public  OAuthTokenProviders()
        {
            Console.WriteLine("TESTING INSIDE CONSTRUCT");
            connect_to_exchange();
        }

        static async void connect_to_exchange()
        {
            // Using Microsoft.Identity.Client
            var cca = ConfidentialClientApplicationBuilder
                .Create("")      //client Id
                .WithClientSecret("")
                .WithTenantId("")
                .Build();
            var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
            try
            {
                // Get token
                var authResult = await cca.AcquireTokenForClient(ewsScopes)
                    .ExecuteAsync();
                // Configure the ExchangeService with the access token
                var ewsClient = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
                ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
                ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
                ewsClient.ImpersonatedUserId =
                    new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "[email protected]");
                //Include x-anchormailbox header
                ewsClient.HttpHeaders.Add("X-AnchorMailbox", "[email protected]");
                // Make an EWS call to list folders on exhange online
                var folders = ewsClient.FindFolders(WellKnownFolderName.MsgFolderRoot, new FolderView(10));
                foreach (var folder in folders)
                {
                    Console.WriteLine($"Folder: {folder.DisplayName}");
                }
                // Make an EWS call to read 50 emails (last 5 days) from Inbox folder
                TimeSpan ts = new TimeSpan(-5, 0, 0, 0);
                DateTime date = DateTime.Now.Add(ts);
                SearchFilter.IsGreaterThanOrEqualTo filter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, date);
                var findResults = ewsClient.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(50));
                foreach (var mailItem in findResults)
                {
                    Console.WriteLine($"Subject: {mailItem.Subject}");
                }
                EmailMessage email = new EmailMessage(ewsClient);
                email.ToRecipients.Add("[email protected]");
                email.Subject = "HelloWorld";
                email.Body = new MessageBody("This is a test email using MS Exchg we services");
                email.Send();
            }
            catch (MsalException ex)
            {
                Console.WriteLine($"Error acquiring access token: {ex}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex}");
            }
            if (System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("Hit any key to exit...");
                Console.ReadKey();
            }
        }
    }
}

当我在vs Studio中执行上述代码时,要 开发相同的信息移动平台或Mac上的流量不可用。参见 https://aka.ms/msal-net-net-net-net-net-confidential-avialdial-availability 有关详细信息 。请证明一些暗示为什么它会显示出这种例外以及我们如何处理它。提前致谢。

First of all, a newbie to C# & Vb.net development.

While am trying to connect to an exchange server am getting the below error.

System.PlatformNotSupportedException
  HResult=0x80131539
  Message=Confidential Client flows are not available on mobile platforms or on Mac.See https://aka.ms/msal-net-confidential-availability for details.
  Source=Microsoft.Identity.Client
  StackTrace:
   at Microsoft.Identity.Client.ConfidentialClientApplicationBuilder.Create(String clientId)
   at DataLoadLibrary.Email.OAuthTokenProviders.<connect_to_exchange>d__1.MoveNext() in C:\Users\path\to\file\OAuthTokenProviders.cs:line 20

I'm referring to https://github.com/sndpkj/ews-oauth2-dotnet-core/blob/main/EWS-OAuth2/Program.cs for developing the same

using System;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Identity.Client;



namespace DataLoadLibrary.Email
{
    public class OAuthTokenProviders
    {
        public  OAuthTokenProviders()
        {
            Console.WriteLine("TESTING INSIDE CONSTRUCT");
            connect_to_exchange();
        }

        static async void connect_to_exchange()
        {
            // Using Microsoft.Identity.Client
            var cca = ConfidentialClientApplicationBuilder
                .Create("")      //client Id
                .WithClientSecret("")
                .WithTenantId("")
                .Build();
            var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
            try
            {
                // Get token
                var authResult = await cca.AcquireTokenForClient(ewsScopes)
                    .ExecuteAsync();
                // Configure the ExchangeService with the access token
                var ewsClient = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
                ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
                ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
                ewsClient.ImpersonatedUserId =
                    new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "[email protected]");
                //Include x-anchormailbox header
                ewsClient.HttpHeaders.Add("X-AnchorMailbox", "[email protected]");
                // Make an EWS call to list folders on exhange online
                var folders = ewsClient.FindFolders(WellKnownFolderName.MsgFolderRoot, new FolderView(10));
                foreach (var folder in folders)
                {
                    Console.WriteLine(
quot;Folder: {folder.DisplayName}");
                }
                // Make an EWS call to read 50 emails (last 5 days) from Inbox folder
                TimeSpan ts = new TimeSpan(-5, 0, 0, 0);
                DateTime date = DateTime.Now.Add(ts);
                SearchFilter.IsGreaterThanOrEqualTo filter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, date);
                var findResults = ewsClient.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(50));
                foreach (var mailItem in findResults)
                {
                    Console.WriteLine(
quot;Subject: {mailItem.Subject}");
                }
                EmailMessage email = new EmailMessage(ewsClient);
                email.ToRecipients.Add("[email protected]");
                email.Subject = "HelloWorld";
                email.Body = new MessageBody("This is a test email using MS Exchg we services");
                email.Send();
            }
            catch (MsalException ex)
            {
                Console.WriteLine(
quot;Error acquiring access token: {ex}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(
quot;Error: {ex}");
            }
            if (System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("Hit any key to exit...");
                Console.ReadKey();
            }
        }
    }
}

When I execute the above code in VS Studio am getting the Message=Confidential Client flows are not available on mobile platforms or on Mac. See https://aka.ms/msal-net-confidential-availability for details. Kindly prove some hints on why it is getting showing this exception and how we can handle it. Thanks in Advance.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文