Skip to content

Commit d80098e

Browse files
FrankieTFFrank Li (Wicresoft North America Ltd)
and
Frank Li (Wicresoft North America Ltd)
authored
Update Storage to track 2 SDK (#413)
* Updated storage track 2 SDK. * Take back readonly * Add catch when failed to create Blob and Blob view. * Remove redundant CreateBlob(), create Blob directly in Blob(). * Catch MsalUiRequiredException. * Updating to latest GA package. Co-authored-by: Frank Li (Wicresoft North America Ltd) <[email protected]>
1 parent 585c1c8 commit d80098e

File tree

6 files changed

+82
-28
lines changed

6 files changed

+82
-28
lines changed

3-WebApp-multi-APIs/Controllers/HomeController.cs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using Microsoft.AspNetCore.Authorization;
1+
using Azure.Storage.Blobs;
2+
using Microsoft.AspNetCore.Authorization;
23
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.Identity.Client;
35
using Microsoft.Identity.Web;
4-
using Microsoft.WindowsAzure.Storage.Auth;
5-
using Microsoft.WindowsAzure.Storage.Blob;
66
using System;
77
using System.Diagnostics;
8+
using System.IO;
9+
using System.Text;
810
using System.Threading.Tasks;
9-
using Constants = WebApp_OpenIDConnect_DotNet.Infrastructure.Constants;
1011
using WebApp_OpenIDConnect_DotNet.Models;
1112
using WebApp_OpenIDConnect_DotNet.Services.Arm;
1213
using WebApp_OpenIDConnect_DotNet.Services.GraphOperations;
@@ -34,11 +35,11 @@ public IActionResult Index()
3435
return View();
3536
}
3637

37-
[AuthorizeForScopes(Scopes = new[] {Constants.ScopeUserRead})]
38+
[AuthorizeForScopes(Scopes = new[] { WebApp_OpenIDConnect_DotNet.Infrastructure.Constants.ScopeUserRead})]
3839
public async Task<IActionResult> Profile()
3940
{
4041
var accessToken =
41-
await tokenAcquisition.GetAccessTokenForUserAsync(new[] {Constants.ScopeUserRead});
42+
await tokenAcquisition.GetAccessTokenForUserAsync(new[] { WebApp_OpenIDConnect_DotNet.Infrastructure.Constants.ScopeUserRead});
4243

4344
var me = await graphApiOperations.GetUserInformation(accessToken);
4445
var photo = await graphApiOperations.GetPhotoAsBase64Async(accessToken);
@@ -73,21 +74,30 @@ public async Task<IActionResult> Tenants()
7374

7475
public async Task<IActionResult> Blob()
7576
{
76-
var scopes = new string[] { "https://storage.azure.com/user_impersonation" };
77-
78-
var accessToken =
79-
await tokenAcquisition.GetAccessTokenForUserAsync(scopes);
80-
81-
// create a blob on behalf of the user
82-
TokenCredential tokenCredential = new TokenCredential(accessToken);
83-
StorageCredentials storageCredentials = new StorageCredentials(tokenCredential);
84-
77+
string message = "Blob failed to create";
8578
// replace the URL below with your storage account URL
8679
Uri blobUri = new Uri("https://blobstorageazuread.blob.core.windows.net/sample-container/Blob1.txt");
87-
CloudBlockBlob blob = new CloudBlockBlob(blobUri, storageCredentials);
88-
await blob.UploadTextAsync("Blob created by Azure AD authenticated user.");
89-
90-
ViewData["Message"] = "Blob successfully created";
80+
BlobClient blobClient = new BlobClient(blobUri, new TokenAcquisitionTokenCredential(tokenAcquisition));
81+
82+
string blobContents = "Blob created by Azure AD authenticated user.";
83+
byte[] byteArray = Encoding.ASCII.GetBytes(blobContents);
84+
using (MemoryStream stream = new MemoryStream(byteArray))
85+
{
86+
try
87+
{
88+
await blobClient.UploadAsync(stream);
89+
message = "Blob successfully created";
90+
}
91+
catch (MsalUiRequiredException ex)
92+
{
93+
throw ex;
94+
}
95+
catch (Exception)
96+
{
97+
}
98+
}
99+
100+
ViewData["Message"] = message;
91101
return View();
92102
}
93103

3-WebApp-multi-APIs/Startup.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ public void ConfigureServices(IServiceCollection services)
3838
options.HandleSameSiteCookieCompatibility();
3939
});
4040

41-
services.AddOptions();
42-
43-
services.AddMicrosoftIdentityWebAppAuthentication(Configuration)
44-
.EnableTokenAcquisitionToCallDownstreamApi( new string[] { Constants.ScopeUserRead })
45-
.AddInMemoryTokenCaches();
41+
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
42+
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"))
43+
.EnableTokenAcquisitionToCallDownstreamApi()
44+
.AddInMemoryTokenCaches();
4645

4746
// Add APIs
4847
services.AddGraphService(Configuration);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using Azure.Core;
4+
using Microsoft.Identity.Client;
5+
using Microsoft.Identity.Web;
6+
7+
namespace WebApp_OpenIDConnect_DotNet
8+
{
9+
public class TokenAcquisitionTokenCredential : TokenCredential
10+
{
11+
readonly private ITokenAcquisition _tokenAcquisition;
12+
13+
/// <summary>
14+
/// Constructor from an ITokenAcquisition service.
15+
/// </summary>
16+
/// <param name="tokenAcquisition">Token acquisition.</param>
17+
public TokenAcquisitionTokenCredential(ITokenAcquisition tokenAcquisition)
18+
{
19+
_tokenAcquisition = tokenAcquisition;
20+
}
21+
22+
/// <inheritdoc/>
23+
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken)
24+
{
25+
AuthenticationResult result = _tokenAcquisition.GetAuthenticationResultForUserAsync(requestContext.Scopes)
26+
.GetAwaiter()
27+
.GetResult();
28+
return new AccessToken(result.AccessToken, result.ExpiresOn);
29+
}
30+
31+
/// <inheritdoc/>
32+
public override async ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken)
33+
{
34+
AuthenticationResult result = await _tokenAcquisition.GetAuthenticationResultForUserAsync(requestContext.Scopes).ConfigureAwait(false);
35+
return new AccessToken(result.AccessToken, result.ExpiresOn);
36+
}
37+
}
38+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@{
2+
ViewData["Title"] = "Blob";
3+
}
4+
5+
<h2>@ViewData["Title"]</h2>
6+
<h3>@ViewData["Message"]</h3>
7+

3-WebApp-multi-APIs/WebApp-OpenIDConnect-DotNet.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
</ItemGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="Microsoft.Identity.Web" Version="1.0.0" />
22-
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.0.0" />
23-
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
21+
<PackageReference Include="Azure.Storage.Blobs" Version="12.6.0" />
22+
<PackageReference Include="Microsoft.Identity.Client" Version="4.19.0" />
23+
<PackageReference Include="Microsoft.Identity.Web" Version="1.1.0" />
24+
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.1.0" />
2425
</ItemGroup>
2526

2627
</Project>

4-WebApp-your-API/4-2-B2C/Client/TodoListClient.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="3.1.1" />
2626
<PackageReference Include="Microsoft.Identity.Web" Version="1.0.0" />
2727
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.0.0" />
28-
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
2928
</ItemGroup>
3029

3130
</Project>

0 commit comments

Comments
 (0)