IdentityModel SetBearerToken系统

时间:2017-06-28 05:37:46

标签: c#

使用.net 4.7和IdentityModel的Windows服务应用程序。 这是一个Windows服务:它在我的机器上完美运行(Windows 10),但不适用于Windows Server 2008 r2。

    public async Task<bool> ConnectAsync()
    {
        bool retValue = true;

        try
        {
            var client = new DiscoveryClient(FileWatcherDefinitions.PortalBaseAddress);
            var doc = await client.GetAsync();

            retValue = !doc.IsError;

            if (retValue)
            {
                var result = doc.TokenEndpoint;
                var tokenClient = new TokenClient(doc.TokenEndpoint, 
                    FileWatcherDefinitions.PortalClientName, 
                    FileWatcherDefinitions.PortalClientPassword);
                var tokenResponse = await tokenClient.RequestClientCredentialsAsync(FileWatcherDefinitions.PortalWebApiResource);

                retValue = !tokenResponse.IsError;

                if (retValue)
                {
                    _httpClient = new HttpClient();
                    _httpClient.SetBearerToken(tokenResponse.AccessToken);
                    // _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", tokenResponse.AccessToken);
                }
            }
        }
        catch(Exception exc)
        {
            retValue = false;
        }

        return retValue;
    }


exception: System.AggregateException: One or more errors occurred. ---> System.MissingMethodException: Method not found: 'Void System.Net.Http.HttpClientExtensions.SetBearerToken(System.Net.Http.HttpClient, System.String)'.
   at RICustomerServiceFileWatcher.RICustomerServicePortalWebApiClient.<ConnectAsync>d__1.MoveNext()
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)
   at RICustomerServiceFileWatcher.RICustomerServicePortalWebApiClient.ConnectAsync()
   at RICustomerServiceFileWatcher.FileWatcherTimeManager.<>c__DisplayClass8_0.<<InitialOrderCheck>b__10>d.MoveNext()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at RICustomerServiceFileWatcher.FileWatcherTimeManager.InitialOrderCheck()
---> (Inner Exception #0) System.MissingMethodException: Method not found: 'Void System.Net.Http.HttpClientExtensions.SetBearerToken(System.Net.Http.HttpClient, System.String)'.
   at RICustomerServiceFileWatcher.RICustomerServicePortalWebApiClient.<ConnectAsync>d__1.MoveNext()
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)
   at RICustomerServiceFileWatcher.RICustomerServicePortalWebApiClient.ConnectAsync()
   at RICustomerServiceFileWatcher.FileWatcherTimeManager.<>c__DisplayClass8_0.<<InitialOrderCheck>b__10>d.MoveNext()<---

我已经看过Precompiled Azure Function throwing error on HttpClientExtensions.SetBearerToken use, CSX doesn't 但它并没有解决我的问题:如果我使用该解决方案,我会得到另一个例外。

System.AggregateException: One or more errors occurred. ---> System.MissingMethodException: Method not found: 'Void IdentityModel.Client.DiscoveryClient..ctor(System.String, System.Net.Http.HttpMessageHandler)'.
   at RICustomerServiceFileWatcher.RICustomerServicePortalWebApiClient.<ConnectAsync>d__1.MoveNext()
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)
   at RICustomerServiceFileWatcher.RICustomerServicePortalWebApiClient.ConnectAsync()
   at RICustomerServiceFileWatcher.FileWatcherTimeManager.<>c__DisplayClass8_0.<<InitialOrderCheck>b__10>d.MoveNext()
   --- End of inner exception stack trace ---

0 个答案:

没有答案