Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
michaelviolle opened this issue Sep 2, 2021 · 1 comment

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.

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

No branches or pull requests

2 participants