Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

HttpClient.GetAsync throw an Unhandled Exception when i use 'new HttpClient(_result.RefreshTokenHandler);' #115

Open
michaelviolle opened this issue Sep 2, 2021 · 2 comments

Comments

@michaelviolle
Copy link

Hello,

I wanted to add the management of the resfresh token () to Oidc sample - XamarinForms (UWP).

For that, like in sample UWP, I have add a HttpClient that uses RefreshTokenHandler recovered with a LoginAsync(...), but I get an Unhandled Exception during the GetAsync(....).

My code

        public MainPage ()
		{
			InitializeComponent ();

            Login.Clicked += Login_Clicked;
            CallApi.Clicked += CallApi_Clicked;

            var browser = DependencyService.Get<IBrowser>();

            var options = new OidcClientOptions
            {
                Authority = "https://demo.identityserver.io",
                ClientId = "interactive.public",
                Scope = "openid profile email api offline_access",
                RedirectUri = "xamarinformsclients://callback",
                Browser = browser,

                ResponseMode = OidcClientOptions.AuthorizeResponseMode.Redirect
            };

            _client = new OidcClient(options);
            //Default client
            _lazyapiClient.Value.BaseAddress = new Uri("https://demo.identityserver.io/");
        }
        private async void Login_Clicked(object sender, EventArgs e)
        {
            _result = await _client.LoginAsync(new LoginRequest());

            if (_result.IsError)
            {
                OutputText.Text = _result.Error;
                return;
            }

            var sb = new StringBuilder(128);
            foreach (var claim in _result.User.Claims)
            {
                sb.AppendFormat("{0}: {1}\n", claim.Type, claim.Value);
            }

            sb.AppendFormat("\n{0}: {1}\n", "refresh token", _result?.RefreshToken ?? "none");
            sb.AppendFormat("\n{0}: {1}\n", "access token", _result.AccessToken);

            OutputText.Text = sb.ToString();

            _lazyapiClient.Value.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _result?.AccessToken ?? "");
           //My new APIClient compatible RefreshToken
            _apiClient = new HttpClient(_result.RefreshTokenHandler);
            _apiClient.BaseAddress = new Uri("https://demo.identityserver.io/");
        }
        private async void CallApi_Clicked(object sender, EventArgs e)
        {
            
            var result = await _lazyapiClient.Value.GetAsync("api/test");
            var result2 = await _apiClient.GetAsync("api/test"); //==> Throw Unhandled exception

            if (result.IsSuccessStatusCode)
            {
                OutputText.Text = JArray.Parse(await result.Content.ReadAsStringAsync()).ToString();
            }
            else
            {
                OutputText.Text = result.ReasonPhrase;
            }
        }

Would I have made a mistake?

@NuarcaMColorado
Copy link

I'm experiencing the same problem. Is it a 'The inner handler has not been assigned.' exception? I was wondering if it may be related to the HttpClient version I am using.

@ceuser1
Copy link

ceuser1 commented Dec 30, 2024

Old topic but still... I don't know about the UnhandledException, but if you get the 'The inner handler has not been assigned.' exception, then you probably forgot to set the RefreshTokenInnerHttpHandler for OidcClientOptions, for example:

var options = new OidcClientOptions
{
    ...
    RefreshTokenInnerHttpHandler = new SocketsHttpHandler()
};

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants