4

I have an azure function that uses the Azure context. When I execute my function from visual studio 2019 on my machine, it executes correctly. However when I publish this to my Azure account, I get an error that the my.azureauth file cannot be found.

Could not find file 'D:\Program Files (x86)\SiteExtensions\Functions\2.0.12950\32bit\my.azureauth'

The code that is used:

var authFilePath = "my.azureauth";
Console.WriteLine($"Authenticating with Azure using credentials in file at {authFilePath}");
azure = Azure.Authenticate(authFilePath).WithDefaultSubscription();
sub = azure.GetCurrentSubscription();

Console.WriteLine($"Authenticated with subscription '{sub.DisplayName}' (ID: {sub.SubscriptionId})");

This is code that I found on one of the Microsoft tutorials. I have set my my.azureauth file to "Copy Always".

Could anyone point my in the right direction?

James87262
  • 41
  • 1
  • 2

4 Answers4

4

You are get this file path because the Directory.GetCurrentDirectory() would return D:\Program Files (x86)\SiteExtensions\Functions\2.0.12950\32bit instead of D:\home\site\wwwroot\ or D:\home\site\wwwroot\FunctionName.

And if you want to get the wwwroot folder or the function app directory you should use ExecutionContext. Further more information you could refer to this wiki doc.

So the right file path should be context.FunctionDirectory+"\my.azureauth" or context.FunctionAppDirectory+"\my.azureauth", which one to use depends on where your file is stored.

Community
  • 1
  • 1
George Chen
  • 13,703
  • 2
  • 11
  • 26
1

I have found that Kudu is extremely useful in seeing what has been deployed to Azure.

Navigate to your function in the Azure portal. The instructions here will help get to the kudu console.

https://www.gslab.com/blogs/kudu-azure-web-app

From there you can browse the files which have been deployed into your function's file system.

If you add " , ExecutionContext context)" at the end of the function's run entry point, you can then get the folder which your function is running from with "var path = context.FunctionAppDirectory;

PS apologies for any formatting I am editing this on my phone.

0

Welcome to Stackoverflow.

Firstly, I'd recommend strongly against using file-based authentication as shown in your question.

From notes:

Note, file-based authentication is an experimental feature that may or may not be available in later releases. The file format it relies on is subject to change as well.

Instead, I would personally store the connection string details (AzureCredentials) in the config file (Web/SiteSettings) and use the provided constructor...

Again, the below are taken from the documentation notes:

Similarly to the file-based approach, this method requires a service principal registration, but instead of storing the credentials in a local file, the required inputs can be supplied directly via an instance of the AzureCredentials class:

var creds = new AzureCredentialsFactory().FromServicePrincipal(client, key, tenant, AzureEnvironment.AzureGlobalCloud);
var azure = Azure.Authenticate(creds).WithSubscription(subscriptionId);

or

var creds = new AzureCredentialsFactory().FromServicePrincipal(client, pfxCertificatePath, password, tenant, AzureEnvironment.AzureGlobalCloud);
var azure = Azure.Authenticate(creds).WithSubscription(subscriptionId);

where client, tenant, subscriptionId, and key or pfxCertificatePath and password are strings with the required pieces of information about your service principal and subscription. The last parameter, AzureEnvironment.AzureGlobalCloud represents the Azure worldwide public cloud. You can use a different value out of the currently supported alternatives in the AzureEnvironment enum.

The first example is most likely the one you should be looking at.

The notes I got this information from can be accessed here.

Adrian
  • 8,271
  • 2
  • 26
  • 43
  • That's really useful, thank you. I am doing some reading into the AzureCredentialsFactory now. I've managed to find my tenant id (by going to Azure AD and then 'Default Directory', but how do I get the client and key? – James87262 Dec 30 '19 at 20:27
  • @James87262 Sorry, I am not entirely sure, I'd start by looking [here](https://stackoverflow.com/questions/57587054/how-to-authenticate-to-an-azure-function-using-function-auth-or-azure-ad-service) though. – Adrian Dec 30 '19 at 20:38
  • @James87262 I took some screenshots, hoping to help you get started quickly. – Cindy Pau Dec 31 '19 at 09:46
0

If you have some problems with AAD, these screenshots may help you.

enter image description here

Client ID:

enter image description here

Key:

enter image description here

Please note that the Key value can only be copied when it is created, after which it will be hidden.

Hope this helps you get started with AAD quickly.

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27