‘Authority’ Uri should have at least one segment in the path Error

In this azure tutorial, we will discuss how to fix, Authority’ Uri should have at least one segment in the path Error. This error I got while trying to execute the Azure function Code to implement the Azure AD authentication to secure the  Azure function using the Visual Studio 2019.

‘Authority’ Uri should have at least one segment in the path Error

I was working for the functionality to implement the Azure AD Authentication for the Azure Function for securing the Azure Function.

Below was my Azure Function, I got the error on the line which is highlighted below.

Authority Uri should have at least one segment in the path Error
public static HttpResponseMessage UpdateMessage(string body)
        {
            HttpResponseMessage newresponse;
            AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/" + Environment.GetEnvironmentVariable("TenantID"));
            ClientCredential clntCred = new ClientCredential(Environment.GetEnvironmentVariable("ClientID"), Environment.GetEnvironmentVariable("ClientSecret"));
            AuthenticationResult authRes = authContext.AcquireTokenAsync(Environment.GetEnvironmentVariable("AudienceID"), clntCred).Result;

When I ran the Project, I got the below error

‘Authority’ Uri should have at least one segment in the path

The complete error message was as below

Executed ‘MyCallerFunction’ (Failed, Id=eb2d3d38-3c8d-4c3f-8dc6-18bd06b70ab4, Duration=705ms)
System.Private.CoreLib: Exception while executing function: MyCallerFunction. Microsoft.IdentityModel.Clients.ActiveDirectory: ‘authority’ Uri should have at least one segment in the path (i.e. https:////…)
Parameter name: authority.

You can see below for the reference

'Authority' Uri should have at least one segment in the path

Why this Error?

This error, I got because of Somewhat I have deleted wrongly, the application settings Parameter values (TenantID, ClientID, ClientSecret, AudienceID, etc ) in my local.settings.json file. I didn’t notice that. Then after seeing that, I have added them back with the Key-Value Pair. Now I could able to fix this Locally.

Now, I Published the Azure Function to the Azure Portal, after that, when I ran the Azure Function, again I got the same error in the Azure Portal this is because as we all know local.settings.json file changes will work for Our Local machine only. We will not get the reference of local.settings.json file in Azure. So what I did I manually added all these Application settings key-value pairs for the Azure Function App.

Authority’ Uri should have at least one segment in the path Error [Solved]

To fix this error locally in Visual Studio 2019, you need to add all the Application settings key-value pairs in your local.settings.json file along with the above Azure Function Code

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "TenantID": "5d9d690a-####-474d-ae8b-#############",
    "ClientID": "8dd5dd1a-cf07-4df3-bf02-522067679212",
    "ClientSecret": "25Fbw-zf5T451Ke-QP5l7Pg1Tr_wG.UimW",
    "AudienceID": "c63889d3-d0b5-4d7c-9060-b86e9e215da5",
    "TargetURL": "https://secureapp.azurewebsites.net"



  }
}

To fix this error in the Azure Portal after you Publish the Azure Function, You need to Navigate to the Azure Function App —-> Click on the Configuration link from the left Navigation ——> Select the Application Settings tab —-> Click on the + New application setting button

authority' uri should have at least one segment in the path c#

Now Add each app settings with the name and value as shown below and don’t forget to click on the Save Button finally.

Enable authentication on Azure Function

These changes will fix the ‘Authority’ Uri should have at least one segment in the path Error in the Azure Portal after you publish the Azure Function into the Azure Portal.

You may also like following the below articles

Wrapping Up

In this Article, We discussed, How to fix,  Authority’ Uri should have at least one segment in the path Error that I got while trying to execute the Azure function Code to implement the Azure AD authentication to secure the  Azure function using the Visual Studio 2019.

Leave a Comment