Unable to obtain configuration from: 'System.String'

Greg Gum 1 Reputation point
2022-07-02T12:26:32.92+00:00

I am trying to get up to speed using Azure Active Direct B2C + Blazor Server.

So, using the very latest Visual Studio 2022, I create a test project using the Template and hook it up to an instance of AAD B2C.

I then run the project in VS. It gives me the following exception:

System.InvalidOperationException: IDX20803: Unable to obtain configuration from: 'System.String'.  
 ---> System.IO.IOException: IDX20807: Unable to retrieve document from: 'System.String'. HttpResponseMessage: 'System.Net.Http.HttpResponseMessage', HttpResponseMessage.Content: 'System.String'.  
   at Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(String address, CancellationToken cancel)  
   at Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.GetAsync(String address, IDocumentRetriever retriever, CancellationToken cancel)  
   at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.GetConfigurationAsync(CancellationToken cancel)  
   --- End of inner exception stack trace ---  
   at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.GetConfigurationAsync(CancellationToken cancel)  
   at Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler.HandleChallengeAsyncInternal(AuthenticationProperties properties)  
   at Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler.HandleChallengeAsync(AuthenticationProperties properties)  
   at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.ChallengeAsync(AuthenticationProperties properties)  
   at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties)  
   at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)  
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)  
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)  
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)  

From what I can find on the web, it means the configuration is incorrect. However, I don't know what the issue is. The configuration was set by the project template and look like this:

"AzureAd": {  
    "Instance": "https://MyHumbleLife.b2clogin.com/",  
    "Domain": "MyHumbleLife.onmicrosoft.com",  
    "TenantId": "
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
21,656 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Shweta Mathur 29,746 Reputation points Microsoft Employee
    2022-07-04T05:26:34.103+00:00

    Hi @Greg Gum ,

    Thanks for reaching out.

    I understand you are trying to configure ASP.Net Blazor WebAssembly app with Azure Active Directory B2C and facing issues.

    The error you are getting is due to your application's configuration has not been set up properly to recognize B2C policies.

    If you are calling the protected Web API, AddMicrosoftIdentityWebApi method in startup.cs configures services to protect the web API which expects an AzureAdB2C section in the app's configuration with the necessary settings to initialize authentication options.

    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)  
        .AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAdB2C"));  
    

    To configure the Blazor Server, you need to specify SignUp or SignIn Policy in your Azure AD B2C configration in appsettings.json file

    {  
      "AzureAdB2C": {  
        "Instance": "https://{TENANT}.b2clogin.com/",  
        "ClientId": "{SERVER API APP CLIENT ID}",  
        "Domain": "{TENANT DOMAIN}",  
        "SignUpSignInPolicyId": "{SIGN UP OR SIGN IN POLICY}"  
      }  
    }  
    

    However, to configure WebAssembly Client application using Azure AD B2C, appsettings.json of client application must include authority in configration files as:

    {  
      "AzureAdB2C": {  
        "Authority": "{AAD B2C INSTANCE}{TENANT DOMAIN}/{SIGN UP OR SIGN IN POLICY}",  
        "ClientId": "{CLIENT APP CLIENT ID}",  
        "ValidateAuthority": false  
      }  
    }  
    

    Please find the reference to configure WebAssembly which is supported Azure AD B2C as mentioned here for more details.

    Hope this will help.

    Thanks,
    Shweta

    ----------------------------

    Please remember to "Accept Answer" if answer helped you.

    1 person found this answer helpful.

  2. Francis Yankey 1 Reputation point
    2022-07-28T06:05:53.817+00:00

    It has to do with your configurations. Kindly make sure the following are correct
    "Instance","Domain","TenantId","ClientId"

    0 comments No comments

  3. Peng Jiang (姜 鹏) 0 Reputation points
    2023-10-09T13:18:49.33+00:00

    I fixed this issue after 60 mins.

    I guess you are using swagger, if the order is

    app.MapControllers();
    app.UseSwagger();
    

    You will get this extremely strange issue.

    So, the anwser is switch above order.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.