Passing User Agent in MSGraph ,NET SDK

New Contributor

I'm reading customer 365 email accounts using MSGraph SDK 4.27.0 in C# and it works fine, but one customer is using EWS allowlists which work by User Agent. How do I pass the User Agent string using the SDK? The code is from the MS Example using Azure ID

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

            // Multi-tenant apps can use "common",
            // single-tenant apps must use the tenant ID from the Azure portal
            var tenantId = "organizations";

            // Value from app registration
            var theClientId = clientID;

            // using Azure.Identity;
            var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
            };

            var userName = txtUserName.Text;
            var password = txtPwd.Text ;

            // https://docs.microsoft.com/dotnet/api/azure.identity.usernamepasswordcredential
            var userNamePasswordCredential = new UsernamePasswordCredential(
                userName, password, tenantId, theClientId, options);

            var graphClient = new GraphServiceClient(userNamePasswordCredential, scopes);
            try
            {
                if (string.IsNullOrEmpty(txtSharedAccount.Text.Trim())) 
                {
                    rootFolder = await graphClient.Me.MailFolders["msgfolderroot"]
                    .Request()
                    .GetAsync();
                }
1 Reply

 

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();