Skip to content

Update Storage to track 2 SDK #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>