Skip to content

Commit 8ddb4a6

Browse files
authored
Add Token Credential Samples with DTFx.AzureStorage v1 and v2 (#1118)
* initial commit * Update ReadMe.md * update by comments * remove "en-us" in the link * remove en-us in the links * update by comments * udpate token credential sample * update by comment * Update ConsoleApp.csproj * Update ConsoleApp.csproj * remove duplicate method
1 parent 2ba65e8 commit 8ddb4a6

File tree

6 files changed

+190
-0
lines changed

6 files changed

+190
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Azure.Identity" Version="1.11.0" />
12+
<PackageReference Include="Microsoft.Azure.DurableTask.AzureStorage" Version="1.17.3" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using Azure.Core;
2+
using Azure.Identity;
3+
using DurableTask.AzureStorage;
4+
using DurableTask.Core;
5+
using Microsoft.WindowsAzure.Storage.Auth;
6+
7+
internal class Program
8+
{
9+
private static async Task Main(string[] args)
10+
{
11+
// Create credential based on the configuration
12+
var credential = new DefaultAzureCredential();
13+
string[] scopes = new string[] { "https://storage.azure.com/.default" }; // Scope for Azure Storage
14+
15+
static Task<NewTokenAndFrequency> RenewTokenFuncAsync(object state, CancellationToken cancellationToken)
16+
{
17+
var credential = new DefaultAzureCredential();
18+
var initialToken = credential.GetToken(new TokenRequestContext(new[] { "https://storage.azure.com/.default" }));
19+
var expiresAfter = initialToken.ExpiresOn - DateTimeOffset.UtcNow - TimeSpan.FromMinutes(10);
20+
return Task.FromResult(new NewTokenAndFrequency(initialToken.Token, expiresAfter));
21+
}
22+
23+
// Get the token
24+
var accessToken = await credential.GetTokenAsync(new Azure.Core.TokenRequestContext(scopes));
25+
26+
var service = new AzureStorageOrchestrationService(new AzureStorageOrchestrationServiceSettings
27+
{
28+
StorageAccountDetails = new StorageAccountDetails
29+
{
30+
AccountName = "YourStorageAccount",
31+
EndpointSuffix = "core.windows.net",
32+
StorageCredentials = new StorageCredentials(new Microsoft.WindowsAzure.Storage.Auth.TokenCredential(
33+
accessToken.Token,
34+
RenewTokenFuncAsync,
35+
null,
36+
TimeSpan.FromMinutes(5)))
37+
}
38+
});
39+
40+
var client = new TaskHubClient(service);
41+
var worker = new TaskHubWorker(service);
42+
43+
worker.AddTaskOrchestrations(typeof(SampleOrchestration));
44+
worker.AddTaskActivities(typeof(SampleActivity));
45+
46+
await worker.StartAsync();
47+
48+
var instance = await client.CreateOrchestrationInstanceAsync(typeof(SampleOrchestration), "World");
49+
50+
var result = await client.WaitForOrchestrationAsync(instance, TimeSpan.FromMinutes(1));
51+
52+
Console.WriteLine($"Orchestration result : {result.Output}");
53+
54+
await worker.StopAsync();
55+
}
56+
}
57+
58+
public class SampleOrchestration : TaskOrchestration<string, string>
59+
{
60+
public override async Task<string> RunTask(OrchestrationContext context, string input)
61+
{
62+
return await context.ScheduleTask<string>(typeof(SampleActivity), input);
63+
}
64+
}
65+
66+
public class SampleActivity : TaskActivity<string, string>
67+
{
68+
protected override string Execute(TaskContext context, string input)
69+
{
70+
return "Hello, " + input + "!";
71+
}
72+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Token Credential Sample
2+
3+
This sample demonstrates how to configure a Identity based connection when using DurableTask.AzureStorage v1.x as the orchestration provider for a Durable Task project.
4+
5+
> Note:
6+
> Identity based connection **is not supported** with .NET framework 4.x with DurableTask.AzureStorage v1.x
7+
8+
## Configuration Prerequisites
9+
10+
Before running this sample, you must
11+
12+
1. Create a new Azure Storage account or reuse an existing one
13+
2. Create your identity in the Azure Portal. Detailed instructions can be found [here](https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-register-app?tabs=certificate)
14+
3. Assign Role-based Access Controls (RBAC) of the storage account created in step 1 to the identity created in step 2 with [these instructions](https://learn.microsoft.com/azure/role-based-access-control/role-assignments-portal-managed-identity#Overview).
15+
* Storage Queue Data Contributor
16+
* Storage Blob Data Contributor
17+
* Storage Table Data Contributor
18+
4. Add the identity required information to your app's configuration.
19+
5. Set `AccountName` to the name of the storage account. AccountName can be replaced with Storage Account BlobServiceUri, TableServiceUri and QueueServiceUri.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Azure.DurableTask.AzureStorage" Version="2.0.0-rc.3" />
12+
<PackageReference Include="Azure.Identity" Version="1.11.0" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using DurableTask.AzureStorage;
2+
using DurableTask.Core;
3+
using Azure.Identity;
4+
5+
internal class Program
6+
{
7+
private static async Task Main(string[] args)
8+
{
9+
var credential = new DefaultAzureCredential();
10+
11+
// Pass the credential created to the StorageAccountClientProvider to start an AzureStorageOrchestrationService
12+
var service = new AzureStorageOrchestrationService(new AzureStorageOrchestrationServiceSettings
13+
{
14+
StorageAccountClientProvider = new StorageAccountClientProvider("AccountName", credential),
15+
});
16+
17+
var client = new TaskHubClient(service);
18+
var worker = new TaskHubWorker(service);
19+
20+
worker.AddTaskOrchestrations(typeof(SampleOrchestration));
21+
worker.AddTaskActivities(typeof(SampleActivity));
22+
23+
await worker.StartAsync();
24+
25+
var instance = await client.CreateOrchestrationInstanceAsync(typeof(SampleOrchestration), "World");
26+
27+
var result = await client.WaitForOrchestrationAsync(instance, TimeSpan.FromMinutes(1));
28+
29+
Console.WriteLine($"Orchestration result : {result.Output}");
30+
31+
await worker.StopAsync();
32+
}
33+
}
34+
35+
public class SampleOrchestration : TaskOrchestration<string, string>
36+
{
37+
public override async Task<string> RunTask(OrchestrationContext context, string input)
38+
{
39+
await context.ScheduleTask<string>(typeof(SampleActivity), input);
40+
41+
return "Orchestrator Finished!";
42+
}
43+
}
44+
45+
public class SampleActivity : TaskActivity<string, string>
46+
{
47+
protected override string Execute(TaskContext context, string input)
48+
{
49+
Console.WriteLine("saying hello to " + input);
50+
return "Hello " + input + "!";
51+
}
52+
}
53+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Token Credential Sample
2+
3+
This sample demonstrates how to configure a Identity based connection when using DurableTask.AzureStorage v2.x as the orchestration provider for a Durable Task project.
4+
5+
## Configuration Prerequisites
6+
7+
Before running this sample, you must
8+
9+
1. Create a new Azure Storage account or reuse an existing one
10+
2. Create your identity in the Azure Portal. Detailed instructions can be found [here](https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-register-app?tabs=certificate)
11+
3. Assign Role-based Access Controls (RBAC) of the storage account created in step 1 to the identity created in step 2 with [these instructions](https://learn.microsoft.com/azure/role-based-access-control/role-assignments-portal-managed-identity#Overview).
12+
* Storage Queue Data Contributor
13+
* Storage Blob Data Contributor
14+
* Storage Table Data Contributor
15+
4. Add the identity information to your app or configuration. In the sample here, client secret credential requires clientId, clientSecret and tenantId.
16+
5. Set `AccountName` to the name of the storage account. AccountName can be replaced with Storage Account BlobServiceUri, TableServiceUri and QueueServiceUri.

0 commit comments

Comments
 (0)