Пример #1
0
        public override async Task <ClientCertificateToken> GetToken(ClientCertificateInfo clientInfo, IHttpClientFactory clientFactory)
        {
            System.Threading.Interlocked.Increment(ref exeCount);
            await Task.Delay(100);

            return(new ClientCertificateToken()
            {
                access_token = "1",
                expires_in = 60,
                token_type = "1"
            });
        }
        public override async Task <ClientCertificateToken> GetToken(ClientCertificateInfo clientInfo, IHttpClientFactory clientFactory)
        {
            var httpClient = clientFactory.CreateClient(_appId);

            TicketClient ticketClient = new TicketClient(httpClient);
            var          r            = await ticketClient.GetTicket("jsapi");

            if (r != null && r.ErrCode == 0)
            {
                return(new ClientCertificateToken()
                {
                    access_token = r.Ticket,
                    expires_in = r.Expires,
                    token_type = "ticket"
                });
            }

            return(null);
        }
        public void DistributedCache_Test()
        {
            IServiceCollection sc = new ServiceCollection()
                                    .AddLogging()
                                    .AddHttpClient()
                                    .AddDistributedMemoryCache();
            var sp = sc.BuildServiceProvider();

            ClientCertificateInfo ci = new ClientCertificateInfo()
            {
                ClientID     = "1",
                AuthUrl      = "1",
                ClientSecret = "1"
            };
            MockTokenProcessor           processor    = new MockTokenProcessor();
            CheckResponseProcessor       checker      = CheckResponseProcessor.NormalChecker;
            DistributedTokenCacheManager cacheManager = new DistributedTokenCacheManager(
                sp.GetRequiredService <IDistributedCache>(),
                ci.ClientID);
            ClientCertificateManager ccm = new ClientCertificateManager(
                ci, processor,
                SetTokenProcessor.Bearer,
                checker,
                cacheManager,
                sp.GetRequiredService <ILogger <ClientCertificateManager> >(),
                sp.GetRequiredService <IHttpClientFactory>());


            List <Task> taskList = new List <Task>();

            for (int i = 0; i < 10; i++)
            {
                taskList.Add(Task.Run(async() =>
                {
                    await ccm.GetAccessToken();
                }));
            }

            Task.WaitAll(taskList.ToArray());

            Assert.Equal(1, processor.exeCount);
        }
 public static IServiceCollection AddTokenClient(this IServiceCollection serviceDescriptors, ClientCertificateInfo ci)
 {
     if (ci != null && !string.IsNullOrEmpty(ci.ClientID) && !string.IsNullOrEmpty(ci.ClientSecret))
     {
         return(AddClientTokenProvider(serviceDescriptors, (options) =>
         {
             options.AddClient(ci.AuthUrl, ci.ClientID, ci.ClientSecret, ci.ClientName);
         }));
     }
     return(serviceDescriptors);
 }