Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOException: IDX20807: Unable to retrieve document from: 'System.String'. HttpResponseMessage:

I'm using Asp.Net Core 3.1 and thru Challenge method, calling an external endpoint to authenticate

public async Task<IActionResult> Challenge(string provider, string returnUrl,string userName)
        {
           
                var props = new AuthenticationProperties
                {
                    RedirectUri = Url.Action(nameof(Callback)),
                    Items =
                    {
                        { "returnUrl", returnUrl },
                        { "scheme", provider },
                    },
                    Parameters =
                    {
                        { OidcConstants.AuthorizeRequest.LoginHint, userName }
                    }
                };

                var result = Challenge(props, provider);
                return result;
            }

Error:

An unhandled exception occurred while processing the request.
IOException: IDX20807: Unable to retrieve document from: 'System.String'. HttpResponseMessage: 'System.Net.Http.HttpResponseMessage', HttpResponseMessage.Content: 'System.String'.
Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(string address, CancellationToken cancel)

InvalidOperationException: IDX20803: Unable to obtain configuration from: 'System.String'.
Microsoft.IdentityModel.Protocols.ConfigurationManager<T>.GetConfigurationAsync(CancellationToken cancel)

Appreciate, any quick response.

like image 897
Mahendra Avatar asked Mar 30 '21 06:03

Mahendra


3 Answers

Got this exception when I created a new .NET 5 project in Visual Studio 2019.

enter image description here

IOException: IDX20807: Unable to retrieve document from: 'System.String'. HttpResponseMessage: 'System.Net.Http.HttpResponseMessage', HttpResponseMessage.Content: 'System.String'. Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(string address, CancellationToken cancel) Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.GetAsync(string address, IDocumentRetriever retriever, CancellationToken cancel) Microsoft.IdentityModel.Protocols.ConfigurationManager.GetConfigurationAsync(CancellationToken cancel)

Before starting the app I got this error:

enter image description here

Required components - dotnet msidentity tool

You can fix this in Connected Services - Service dependencies and set up Microsoft identity platform by pressing Configure.

enter image description here

For me it got stuck like this and nothing happened though:

enter image description here

In appsettings.json I then found the following code:

/*
The following identity settings need to be configured
before the project can be successfully executed.
For more info see https://aka.ms/dotnet-template-ms-identity-platform 
*/

Visited https://aka.ms/dotnet-template-ms-identity-platform and used the tool msidentity-app-sync instead. Started of by installing it and then verifying I had the tool installed.

enter image description here

enter image description here

dotnet tool install -g msidentity-app-sync
dotnet tool list -g

Then ran msidentity-app-sync in the projects folder. A new App registrations will be created in Azure Active Directory and the project now works.

enter image description here

enter image description here

You can also specify tenant and client yourself if you wish to use an existing app like this:

msidentity-app-sync --tenant-id <tenant-id> --username <username> --client-id <client-id>
like image 168
Ogglas Avatar answered Oct 03 '22 20:10

Ogglas


Not sure how much this relates but someone may find it useful for dealing with this exception.

I am using Asp.Net Core 3.1, trying to implement JWT authentication. After setting Authority I began receiving similar error.

Fix of the issue was setting 'ValidIssuer' parameter, instead of setting Authority.

like image 20
Dušan Avatar answered Oct 03 '22 22:10

Dušan


Confirmed also with Dusan's answer. From https://devblogs.microsoft.com/dotnet/jwt-validation-and-authorization-in-asp-net-core/

Authority is the address of the token-issuing authentication server. The JWT bearer authentication middleware will use this URI to find and retrieve the public key that can be used to validate the token’s signature. It will also confirm that the iss parameter in the token matches this URI.

I am not using a public token-issuing auth server.

like image 33
heyyy Avatar answered Oct 03 '22 21:10

heyyy