Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IdentityModel SetBearerToken System

Tags:

c#

Windows Service application using .net 4.7 and IdentityModel. It is a windows service: it works perfectly on my machine (windows 10) but not on 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()<---

I've already seen Precompiled Azure Function throwing error on HttpClientExtensions.SetBearerToken use, CSX doesn't but it does not resolve my problem: if i use that solution i get another exception.

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 ---
like image 705
Stefano Zambonin Avatar asked Nov 15 '25 20:11

Stefano Zambonin


1 Answers

You might be missing the following using directive.

using IdentityModel.Client;

It's available in the IdentityModel NuGet, which you install in the following way.

dotnet add package IdentityModel

like image 84
SandstormNick Avatar answered Nov 18 '25 12:11

SandstormNick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!