Exemplo n.º 1
0
        internal static async Task LoggerTest(Sts sts)
        {
            var traceSourceListener = new TestTraceListner();

            AdalTrace.TraceSource.Listeners.Add(traceSourceListener);

            traceSourceListener.Filter = new SourceFilter("Microsoft.IdentityModel.Clients.ActiveDirectory");

            Trace.TraceInformation("$$$$$");

            var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority, TokenCacheType.Null);

            var credential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret);
            await context.AcquireTokenAsync(sts.ValidResource, credential);

            var invalidCredential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret + "x");
            await context.AcquireTokenAsync(sts.ValidResource, invalidCredential);

            Verify.IsTrue(traceSourceListener.TraceBuffer.IndexOf("$$") < 0);
            Verify.IsTrue(traceSourceListener.TraceBuffer.IndexOf("Correlation ID") > 0);

            traceSourceListener.TraceBuffer = string.Empty;

            var traceListener = new TestTraceListner();

            Trace.Listeners.Add(traceListener);

            credential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret);
            await context.AcquireTokenAsync(sts.ValidResource, credential);

            invalidCredential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret + "x");
            await context.AcquireTokenAsync(sts.ValidResource, invalidCredential);

            Verify.IsFalse(string.IsNullOrEmpty(traceListener.TraceBuffer));
            Verify.IsFalse(string.IsNullOrEmpty(traceSourceListener.TraceBuffer));
            Verify.AreEqual(traceListener.TraceBuffer, traceSourceListener.TraceBuffer);


            traceSourceListener.TraceBuffer   = string.Empty;
            traceListener.TraceBuffer         = string.Empty;
            AdalTrace.LegacyTraceSwitch.Level = TraceLevel.Off;

            credential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret);
            await context.AcquireTokenAsync(sts.ValidResource, credential);

            invalidCredential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret + "x");
            await context.AcquireTokenAsync(sts.ValidResource, invalidCredential);

            Verify.IsTrue(string.IsNullOrEmpty(traceListener.TraceBuffer));
            Verify.IsFalse(string.IsNullOrEmpty(traceSourceListener.TraceBuffer));
        }
        internal static async Task LoggerTest(Sts sts)
        {
            var traceSourceListener = new TestTraceListner();
            AdalTrace.TraceSource.Listeners.Add(traceSourceListener);

            traceSourceListener.Filter = new SourceFilter("Microsoft.IdentityModel.Clients.ActiveDirectory");

            Trace.TraceInformation("$$$$$");

            var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority, TokenCacheType.Null);

            var credential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret);
            await context.AcquireTokenAsync(sts.ValidResource, credential);
            var invalidCredential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret + "x");
            await context.AcquireTokenAsync(sts.ValidResource, invalidCredential);

            Verify.IsTrue(traceSourceListener.TraceBuffer.IndexOf("$$") < 0);

            traceSourceListener.TraceBuffer = string.Empty;

            var traceListener = new TestTraceListner();
            Trace.Listeners.Add(traceListener);

            credential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret);
            await context.AcquireTokenAsync(sts.ValidResource, credential);
            invalidCredential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret + "x");
            await context.AcquireTokenAsync(sts.ValidResource, invalidCredential);

            Verify.IsFalse(string.IsNullOrEmpty(traceListener.TraceBuffer));
            Verify.IsFalse(string.IsNullOrEmpty(traceSourceListener.TraceBuffer));
            Verify.AreEqual(traceListener.TraceBuffer, traceSourceListener.TraceBuffer);


            traceSourceListener.TraceBuffer = string.Empty;
            traceListener.TraceBuffer = string.Empty;
            AdalTrace.LegacyTraceSwitch.Level = TraceLevel.Off;

            credential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret);
            await context.AcquireTokenAsync(sts.ValidResource, credential);
            invalidCredential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret + "x");
            await context.AcquireTokenAsync(sts.ValidResource, invalidCredential);

            Verify.IsTrue(string.IsNullOrEmpty(traceListener.TraceBuffer));
            Verify.IsFalse(string.IsNullOrEmpty(traceSourceListener.TraceBuffer));
        }