Home > Software design >  Get OAuth2 token for EWS email in VB.Net
Get OAuth2 token for EWS email in VB.Net

Time:09-16

I have two application, one in implemented using c# and another one legacy app built using vb.net.

I have registered the application for OAuth and given "full_access_as_app" Application permission as well.

In C# project using MSIL able to get token and able to send email as well

        // Using Microsoft.Identity.Client 4.22.0
        var cca = ConfidentialClientApplicationBuilder
            .Create(ConfigurationManager.AppSettings["appId"])
            .WithClientSecret(ConfigurationManager.AppSettings["clientSecret"])
            .WithTenantId(ConfigurationManager.AppSettings["tenantId"])
            .Build();

        var ewsScopes = new string[] { "https://outlook.office365.com/.default" };

        try
        {
            var authResult = await cca.AcquireTokenForClient(ewsScopes)
                .ExecuteAsync();

            // Configure the ExchangeService with the access token
            var ewsClient = new ExchangeService();
            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
            var folders = ewsClient.FindFolders(WellKnownFolderName.MsgFolderRoot, new FolderView(10));
            foreach(var folder in folders)
            {
                Console.WriteLine($"Folder: {folder.DisplayName}");
            }
        }
        catch (MsalException ex)
        {
            Console.WriteLine($"Error acquiring access token: {ex}");
        }

But Microsoft.Identity.Client won't support for vb.net and not sure how to achieve this.

https://www.nuget.org/packages/Microsoft.Identity.Client/

I tried below code in VB.Net

Dim cCA = ConfidentialClientApplicationBuilder
        .Create("AppId")
        .WithClientSecret("ClientSecret")
        .WithTenantId("TenantID")
        .WithHttpClientFactory(HttpClientFactory)
        .Build();

It's throwing error -> ConfidentialClientApplicationBuilder is a class type and cannot be used as a expression" ->leading .or can only appear inside a with statement

CodePudding user response:

In VB.NET, it's a bit more difficult to spread statements over several lines. If you put the creation of the builder in a single line, the error should be fixed:

Dim cCA = ConfidentialClientApplicationBuilder.Create("AppId").WithClientSecret("ClientSecret").WithTenantId("TenantID").WithHttpClientFactory(HttpClientFactory).Build()

Alternatively, you can use an underscore to signal that the code is continued on the next line:

Dim cCA = ConfidentialClientApplicationBuilder _
    .Create("AppId") _
    .WithClientSecret("ClientSecret") _
    .WithTenantId("TenantID") _
    .WithHttpClientFactory(HttpClientFactory) _
    .Build()
  • Related