Problem with code sample

Yusra Munawar 36 Reputation points
2022-05-16T11:13:54.787+00:00

I am following this code sample step by step

https://learn.microsoft.com/en-us/azure/active-directory-b2c/configure-authentication-sample-web-app-with-api?tabs=visual-studio

but the web API is throwing error for localhost not found. Web app is working fine and after authentication when it try to call Web API it throws error because its not running on local host.

Developer technologies ASP.NET ASP.NET Core
Microsoft Security Microsoft Entra Microsoft Entra External ID
Microsoft Security Microsoft Entra Microsoft Entra ID
{count} votes

Accepted answer
  1. Shweta Mathur 30,296 Reputation points Microsoft Employee Moderator
    2022-05-16T14:42:59.787+00:00

    Hi @Yusra Munawar ,

    Thanks for reaching out.

    I understand you are trying to call web API from web application but getting error as "https://localhost:44332" web API address is not found.

    I configured the same applications in my lab and able to call web API successfully. If you have already started the localhost server of web API while calling from web application but still getting the error. The reason might be you have not trusted the certificate to call https endpoint.

    .NET Core SDK already has certificate installed to the local user certificate store, but that also need to be trusted to call https.

    You can trust the certificate with this command dotnet dev-certs https --trust

    Make sure both the service and client server are up using command line rather than PowerShell.

    202979-image.png

    As per documentation,
    service server is running on https://localhost:44332
    client server is running on https://localhost:5000.

    Make sure the addresses are configured correctly.

    Hope this will help.

    Thanks,
    Shweta

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

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


1 additional answer

Sort by: Most helpful
  1. Shweta Mathur 30,296 Reputation points Microsoft Employee Moderator
    2022-05-17T10:38:02.117+00:00

    Hi @Yusra Munawar

    Below are the configuration files I am using in my lab and able to call the API successfully

    202725-image.png

    Client Configuration File

    {
    "AzureAdB2C": {
    "Scopes": "access_as_user",
    "Instance": "https://xxxx.b2clogin.com",
    "ClientId": "xxxx",
    "Domain": "xxx.onmicrosoft.com",
    "SignedOutCallbackPath": "/B2C_1_signupsignin//oauth2/v2.0/logout/",
    "SignUpSignInPolicyId": "B2C_1_signupsignin",
    "ClientSecret": "xxx"
    },
    "TodoList": {
    "TodoListScope": "https://xxxx.onmicrosoft.com/my-api/access_as_user",
    "TodoListBaseAddress": "https://localhost:44332"

    },
    "Logging": {
    "LogLevel": {
    "Default": "Information",
    "Microsoft": "Warning",
    "Microsoft.Hosting.Lifetime": "Information"
    }
    },
    "AllowedHosts": "*"
    }

    Server Configuration File:
    {
    "AzureAdB2C": {
    "Instance": "https://xxx.b2clogin.com",
    "ClientId": "xxx",
    "Domain": "xxx.onmicrosoft.com",
    "SignedOutCallbackPath": "/signout/B2C_1_SignIn",
    "SignUpSignInPolicyId": "B2C_1_signupsignin"

    },
    "Kestrel": {
    "Endpoints": {
    "Http": {
    "Url": "https://localhost:44332"
    }
    }
    },
    "Logging": {
    "LogLevel": {
    "Default": "Warning"
    }
    },
    "AllowedHosts": "*"
    }

    If you are following the github sample https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/4-WebApp-your-API/4-2-B2C to sync with the document then there is a wrong port mentioned here in client application to call web API. It should be 44332 in both web API and client application where Web API base address needs to call.

    Thanks,
    Shweta


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.