Azure-Samples / digital-twins-samples

Azure Digital Twins building scenario sample code
MIT License
105 stars 88 forks source link

403 errors when authenticating with service principle with Azure Functions #74

Open moizhraj opened 3 years ago

moizhraj commented 3 years ago

Hi Team, I have a sample functions app (dotnet-isolated) and I am using an app (client id, client secret) to authenticate to a twin instance. When I run my function locally it works just fine but once deployed it just fails with a 403 error. I have verified that my client has the required access control as per this doc.

here is the sample code I use for authentication

string adtInstanceUrl = Environment.GetEnvironmentVariable("clientConfig:digitalTwins:instanceUrl");
string tenantId = Environment.GetEnvironmentVariable("clientConfig:servicePrinciple:tenantId");
string clientId = Environment.GetEnvironmentVariable("clientConfig.servicePrinciple.clientId");
string clientSecret = Environment.GetEnvironmentVariable("clientConfig:servicePrinciple:clientSecret");

Environment.SetEnvironmentVariable("AZURE_CLIENT_SECRET", clientSecret);
Environment.SetEnvironmentVariable("AZURE_CLIENT_ID", clientId);
Environment.SetEnvironmentVariable("AZURE_TENANT_ID", tenantId);

TokenCredential tokenCredential = new DefaultAzureCredential();

var client = new DigitalTwinsClient(new Uri(adtInstanceUrl),
                tokenCredential);