0

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, "test@microsoft.com");
                //Include x-anchormailbox header
                ewsClient.HttpHeaders.Add("X-AnchorMailbox", "test@microsoft.com");
                // 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("test@microsoft.com");
                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();
            }
        }
    }
}

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.

Aaditya R Krishnan
  • 495
  • 1
  • 10
  • 31
  • You got two options: 1) run the application on Windows or 2) find a different way of talking to the server. – Lasse V. Karlsen May 10 '22 at 12:01
  • I'd recommend using a more simple `SmtpClient` to send any emails. Look at the API for any other functionality: https://learn.microsoft.com/en-us/graph/users-you-can-reach – Andrew Corrigan May 10 '22 at 12:07
  • @LasseV.Karlsen I'm executing this one on windows VDI itself. But its weird showing this error. – Aaditya R Krishnan May 10 '22 at 12:27
  • @AndrewCorrigan I need to stick with EWS due to the timeframe. – Aaditya R Krishnan May 10 '22 at 12:29
  • 1
    It could be that VDI imposes some limits and thus trips the trigger of that exception. For instance, does that class, since you mention OAuth, does it require a popup where someone have to log into the service and allow it access? I do see the client secret and tenant id, but I don't use OAuth too much unfortunately. – Lasse V. Karlsen May 10 '22 at 12:30
  • How do you reference Microsoft.Identity.Client? Normal nuget magic or did you somehow fumbled here and you are referencing a platform specific assembly from that package directly? – Ralf May 10 '22 at 14:19
  • Try it on a desktop PC, if it magically works, then it's some quirk of the Virtual Desktop (it may be using UWP - which is included as one of the mobile platforms in the link you posted). – Andrew Corrigan May 10 '22 at 14:25

0 Answers0