Skip to content

Commit db33599

Browse files
msmbaldwinCopilot
andcommitted
Inline code snippets from azure-sdk-for-net sharelink sample
The build system can no longer resolve the external code reference to ~/azure-sdk-for-net/sdk/keyvault/samples/sharelink/Program.cs. Inline the code to fix the invalid-code warnings. Co-authored-by: Copilot <[email protected]>
1 parent ad998fa commit db33599

1 file changed

Lines changed: 40 additions & 3 deletions

File tree

articles/key-vault/secrets/storage-keys-sas-tokens-code.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,52 @@ This article provides samples of .NET code that creates a SAS definition and fet
2525

2626
In the following example, a SAS template is created:
2727

28-
:::code language="csharp" source="~/azure-sdk-for-net/sdk/keyvault/samples/sharelink/Program.cs" range="91-97":::
28+
```csharp
29+
private static string BuildSasDefinitionTemplate(bool readOnly) =>
30+
new StringBuilder("sv=2018-03-28") // service version
31+
.Append("&spr=https") // HTTPS only
32+
.Append("&ss=bf") // blobs and files only
33+
.Append("&srt=o") // applies to objects only
34+
.Append(readOnly ? "&sp=r" : "&sp=rw") // read-only or read-write
35+
.ToString();
36+
```
2937

3038
Using this template, you can create a SAS definition by using the following code:
3139

32-
:::code language="csharp" source="~/azure-sdk-for-net/sdk/keyvault/samples/sharelink/Program.cs" range="137-156":::
40+
```csharp
41+
string sasDefinitionName = BuildSasDefinitionName(Tag, readOnly, duration);
42+
SasDefinitionAttributes sasDefinitionAttributes = new SasDefinitionAttributes
43+
{
44+
Enabled = true,
45+
};
46+
47+
Dictionary<string, string> tags = new Dictionary<string, string>
48+
{
49+
[Tag] = "1",
50+
};
51+
52+
SasDefinitionBundle createdSasDefinition = await storageClient.SetSasDefinitionAsync(
53+
storageAccountName,
54+
sasDefinitionName,
55+
sasTemplate,
56+
SasTokenType.Account,
57+
duration,
58+
sasDefinitionAttributes,
59+
tags,
60+
s_cancellationTokenSource.Token);
61+
```
3362

3463
Once the SAS definition is created, you can retrieve SAS tokens like secrets using a `SecretClient`. You need to preface the secret name with the storage account name followed by a dash:
3564

36-
:::code language="csharp" source="~/azure-sdk-for-net/sdk/keyvault/samples/sharelink/Program.cs" range="52-58":::
65+
```csharp
66+
// Build our SAS template, get an existing SAS definition, or create a new one.
67+
string sasTemplate = BuildSasDefinitionTemplate(readOnly);
68+
string sasDefinitionName = await GetOrCreateSasDefinitionAsync(storageClient, storageAccountName, sasTemplate, days, readOnly);
69+
70+
// Now we can create a SecretClient and generate a new SAS token from the storage account and SAS definition names.
71+
SecretClient secretClient = new SecretClient(vaultUri, credential, options);
72+
KeyVaultSecret sasToken = await secretClient.GetSecretAsync($"{storageAccountName}-{sasDefinitionName}", cancellationToken: s_cancellationTokenSource.Token);
73+
```
3774

3875
If your shared access signature token is about to expire, you can fetch the same secret again to generate a new one.
3976

0 commit comments

Comments
 (0)