Exemple #1
0
        public void AuthServiceMsgs_SerializeAuthenticationResult()
        {
            AuthenticationResult result;

            result = new AuthenticationResult(AuthenticationStatus.BadAccount, "Hello World!", TimeSpan.FromMinutes(55));
            result = AuthenticationResult.Parse(result.ToString());
            Assert.AreEqual(AuthenticationStatus.BadAccount, result.Status);
            Assert.AreEqual("Hello World!", result.Message);
            Assert.AreEqual(TimeSpan.FromMinutes(55), result.MaxCacheTime);
        }
Exemple #2
0
        private static async Task RunAsync()
        {
            AuthenticationConfig config = AuthenticationConfig.ReadFromJsonFile("appsettings.json");

            // You can run this sample using ClientSecret or Certificate. The code will differ only when instantiating the IConfidentialClientApplication
            bool isUsingClientSecret = AppUsesClientSecret(config);

            Console.WriteLine("isUsingClientSecret: " + isUsingClientSecret);

            // Even if this is a console application here, a daemon application is a confidential client application
            IConfidentialClientApplication app;

            if (isUsingClientSecret)
            {
                app = ConfidentialClientApplicationBuilder.Create(config.ClientId)
                      .WithClientSecret(config.ClientSecret)
                      .WithAuthority(new Uri(config.Authority))
                      .Build();
                Console.WriteLine("app: " + app + ", authority: " + app.Authority);
            }

            else
            {
                X509Certificate2 certificate = ReadCertificate(config.CertificateName);
                app = ConfidentialClientApplicationBuilder.Create(config.ClientId)
                      .WithCertificate(certificate)
                      .WithAuthority(new Uri(config.Authority))
                      .Build();
            }

            // With client credentials flows the scopes is ALWAYS of the shape "resource/.default", as the
            // application permissions need to be set statically (in the portal or by PowerShell), and then granted by
            // a tenant administrator
            //string[] scopes = new string[] { "https://{CRM_URL}.crm.dynamics.com/.default" };
            string[] scopes = new string[] { "https://curateurdevfr.crm3.dynamics.com/.default" };

            AuthenticationResult result = null;

            try
            {
                result = await app.AcquireTokenForClient(scopes)
                         .ExecuteAsync();

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Token acquired, result: " + result + ", result.AccessToken: [" + result.AccessToken + "], toString: " + result.ToString());
                Console.ResetColor();
            }
            catch (MsalServiceException ex) when(ex.Message.Contains("AADSTS70011"))
            {
                // Invalid scope. The scope has to be of the form "https://resourceurl/.default"
                // Mitigation: change the scope to be as expected
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Scope provided is not supported");
                Console.ResetColor();
            }

            if (result != null)
            {
                Console.WriteLine("result is not null!, will try to call the web api and process the result asynch..");
                var httpClient = new HttpClient();
                var apiCaller  = new ProtectedApiCallHelper(httpClient);
                await apiCaller.CallWebApiAndProcessResultASync("https://curateurdevfr.crm3.dynamics.com/api/data/v9.2/", result.AccessToken, Display);
            }
        }