示例#1
0
        private static async Task <AzureAuthenticationHandler> CreateAzureAuthenticationHandler(string principal, string authProvider)
        {
            AzureAuthenticationOptions options = new AzureAuthenticationOptions();
            IOptionsMonitor <AzureAuthenticationOptions> optionsMonitor = Substitute.For <IOptionsMonitor <AzureAuthenticationOptions> >();

            optionsMonitor.CurrentValue.Returns(options);

            ILoggerFactory logger  = Substitute.For <ILoggerFactory>();
            UrlEncoder     encoder = Substitute.For <UrlEncoder>();
            ISystemClock   clock   = new SystemClock();

            HeaderDictionary headers = new HeaderDictionary
            {
                { "X-MS-CLIENT-PRINCIPAL-IDP", authProvider },
                { "X-MS-CLIENT-PRINCIPAL", principal }
            };

            HttpContext context = Substitute.For <HttpContext>();

            context.Request.Headers.Returns(headers);
            context.Request.Scheme.Returns("https");
            context.Request.Host.Returns(new HostString("test.com"));

            AuthenticationScheme scheme = new AuthenticationScheme(AzureAuthenticationDefaults.AuthenticationScheme, AzureAuthenticationDefaults.DisplayName, typeof(AzureAuthenticationHandler));

            AzureAuthenticationHandler handler = new AzureAuthenticationHandler(optionsMonitor, logger, encoder, clock);
            await handler.InitializeAsync(scheme, context);

            return(handler);
        }
        private static async Task <AzureAuthenticationHandler> CreateAzureAuthenticationHandler(MockHttpMessageHandler authMessageHandler, MockHttpMessageHandler graphMessageHandler, DateTime accessTokenExpiryTime)
        {
            AzureAuthenticationOptions options = new AzureAuthenticationOptions();
            IOptionsMonitor <AzureAuthenticationOptions> optionsMonitor = Substitute.For <IOptionsMonitor <AzureAuthenticationOptions> >();

            optionsMonitor.CurrentValue.Returns(options);

            ILoggerFactory logger  = Substitute.For <ILoggerFactory>();
            UrlEncoder     encoder = Substitute.For <UrlEncoder>();
            ISystemClock   clock   = new SystemClock();

            HttpClient authHttpClient = authMessageHandler.ToHttpClient();

            HttpClient graphHttpClient = graphMessageHandler.ToHttpClient();

            IHttpClientFactory httpClientFactory = Substitute.For <IHttpClientFactory>();

            httpClientFactory
            .CreateClient(Arg.Is(AzureAuthenticationHandler.AzureAuthenticationHttpClientName))
            .Returns(authHttpClient);
            httpClientFactory
            .CreateClient(Arg.Is(AzureAuthenticationHandler.GraphHttpClientName))
            .Returns(graphHttpClient);

            HeaderDictionary headers = new HeaderDictionary
            {
                { "X-MS-TOKEN-AAD-EXPIRES-ON", accessTokenExpiryTime.ToString() }
            };

            HttpContext context = Substitute.For <HttpContext>();

            context.Request.Headers.Returns(headers);
            context.Request.Scheme.Returns("https");
            context.Request.Host.Returns(new HostString("test.com"));

            AuthenticationScheme scheme = new AuthenticationScheme(AzureAuthenticationDefaults.AuthenticationScheme, AzureAuthenticationDefaults.DisplayName, typeof(AzureAuthenticationHandler));

            AzureAuthenticationHandler handler = new AzureAuthenticationHandler(optionsMonitor, logger, encoder, clock, httpClientFactory);
            await handler.InitializeAsync(scheme, context);

            return(handler);
        }