Skip to content

Commit

Permalink
Update Storage to track 2 SDK (#413)
Browse files Browse the repository at this point in the history
* 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) <v-cheli@microsoft.com>
  • Loading branch information
FrankieTF and Frank Li (Wicresoft North America Ltd) authored Oct 10, 2020
1 parent 585c1c8 commit d80098e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 28 deletions.
48 changes: 29 additions & 19 deletions 3-WebApp-multi-APIs/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Microsoft.AspNetCore.Authorization;
using Azure.Storage.Blobs;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Identity.Client;
using Microsoft.Identity.Web;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Constants = WebApp_OpenIDConnect_DotNet.Infrastructure.Constants;
using WebApp_OpenIDConnect_DotNet.Models;
using WebApp_OpenIDConnect_DotNet.Services.Arm;
using WebApp_OpenIDConnect_DotNet.Services.GraphOperations;
Expand Down Expand Up @@ -34,11 +35,11 @@ public IActionResult Index()
return View();
}

[AuthorizeForScopes(Scopes = new[] {Constants.ScopeUserRead})]
[AuthorizeForScopes(Scopes = new[] { WebApp_OpenIDConnect_DotNet.Infrastructure.Constants.ScopeUserRead})]
public async Task<IActionResult> Profile()
{
var accessToken =
await tokenAcquisition.GetAccessTokenForUserAsync(new[] {Constants.ScopeUserRead});
await tokenAcquisition.GetAccessTokenForUserAsync(new[] { WebApp_OpenIDConnect_DotNet.Infrastructure.Constants.ScopeUserRead});

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

public async Task<IActionResult> Blob()
{
var scopes = new string[] { "https://storage.azure.com/user_impersonation" };

var accessToken =
await tokenAcquisition.GetAccessTokenForUserAsync(scopes);

// create a blob on behalf of the user
TokenCredential tokenCredential = new TokenCredential(accessToken);
StorageCredentials storageCredentials = new StorageCredentials(tokenCredential);

string message = "Blob failed to create";
// replace the URL below with your storage account URL
Uri blobUri = new Uri("https://blobstorageazuread.blob.core.windows.net/sample-container/Blob1.txt");
CloudBlockBlob blob = new CloudBlockBlob(blobUri, storageCredentials);
await blob.UploadTextAsync("Blob created by Azure AD authenticated user.");

ViewData["Message"] = "Blob successfully created";
BlobClient blobClient = new BlobClient(blobUri, new TokenAcquisitionTokenCredential(tokenAcquisition));

string blobContents = "Blob created by Azure AD authenticated user.";
byte[] byteArray = Encoding.ASCII.GetBytes(blobContents);
using (MemoryStream stream = new MemoryStream(byteArray))
{
try
{
await blobClient.UploadAsync(stream);
message = "Blob successfully created";
}
catch (MsalUiRequiredException ex)
{
throw ex;
}
catch (Exception)
{
}
}

ViewData["Message"] = message;
return View();
}

Expand Down
9 changes: 4 additions & 5 deletions 3-WebApp-multi-APIs/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ public void ConfigureServices(IServiceCollection services)
options.HandleSameSiteCookieCompatibility();
});

services.AddOptions();

services.AddMicrosoftIdentityWebAppAuthentication(Configuration)
.EnableTokenAcquisitionToCallDownstreamApi( new string[] { Constants.ScopeUserRead })
.AddInMemoryTokenCaches();
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi()
.AddInMemoryTokenCaches();

// Add APIs
services.AddGraphService(Configuration);
Expand Down
38 changes: 38 additions & 0 deletions 3-WebApp-multi-APIs/TokenAcquisitionTokenCredential.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Microsoft.Identity.Client;
using Microsoft.Identity.Web;

namespace WebApp_OpenIDConnect_DotNet
{
public class TokenAcquisitionTokenCredential : TokenCredential
{
readonly private ITokenAcquisition _tokenAcquisition;

/// <summary>
/// Constructor from an ITokenAcquisition service.
/// </summary>
/// <param name="tokenAcquisition">Token acquisition.</param>
public TokenAcquisitionTokenCredential(ITokenAcquisition tokenAcquisition)
{
_tokenAcquisition = tokenAcquisition;
}

/// <inheritdoc/>
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken)
{
AuthenticationResult result = _tokenAcquisition.GetAuthenticationResultForUserAsync(requestContext.Scopes)
.GetAwaiter()
.GetResult();
return new AccessToken(result.AccessToken, result.ExpiresOn);
}

/// <inheritdoc/>
public override async ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken)
{
AuthenticationResult result = await _tokenAcquisition.GetAuthenticationResultForUserAsync(requestContext.Scopes).ConfigureAwait(false);
return new AccessToken(result.AccessToken, result.ExpiresOn);
}
}
}
7 changes: 7 additions & 0 deletions 3-WebApp-multi-APIs/Views/Home/Blob.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@{
ViewData["Title"] = "Blob";
}

<h2>@ViewData["Title"]</h2>
<h3>@ViewData["Message"]</h3>

7 changes: 4 additions & 3 deletions 3-WebApp-multi-APIs/WebApp-OpenIDConnect-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Identity.Web" Version="1.0.0" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.0.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.6.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.19.0" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.1.0" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.1.0" />
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion 4-WebApp-your-API/4-2-B2C/Client/TodoListClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="3.1.1" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.0.0" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.0.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>

</Project>

0 comments on commit d80098e

Please sign in to comment.