Skip to content

Commit

Permalink
Add support for the AzureSDK (#542)
Browse files Browse the repository at this point in the history
See https://github.com/tamram/storage-dotnet-azure-ad-msal/tree/tamram-0818

```
[AuthorizeForScopes(Scopes = new string[] { "https://storage.azure.com/user_impersonation" })]
        public async Task<IActionResult> Blob()
        {
            var scopes = new string[] { "https://storage.azure.com/user_impersonation" }; // I guess the Blob SDK knows already?
            ViewData["Message"] = await CreateBlob(new TokenAcquisitionTokenCredential(_tokenAcquisition),);
            return View();
        }

        private static async Task<string> CreateBlob(TokenAcquisitionTokenCredential tokenCredential)
        {
            // Replace the URL below with the URL to your blob.
            Uri blobUri = new Uri("https://storagesamples.blob.core.windows.net/sample-container/blob1.txt");
            BlobClient blobClient = new BlobClient(blobUri, tokenCredential);

            // Create a blob on behalf of the user.
            string blobContents = "Blob created by Azure AD authenticated user.";
            byte[] byteArray = Encoding.ASCII.GetBytes(blobContents);

            using (MemoryStream stream = new MemoryStream(byteArray))
            {
                await blobClient.UploadAsync(stream);
            }
            return "Blob successfully created";
        }
```
  • Loading branch information
jmprieur authored Mar 31, 2021
1 parent ae61868 commit 9dc6f8d
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Microsoft.Identity.Client;

namespace Microsoft.Identity.Web
{
/// <summary>
/// Azure SDK token credential based on the ITokenAcquisition service.
/// </summary>
public class TokenAcquisitionTokenCredential : TokenCredential
{
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);
}
}
}

0 comments on commit 9dc6f8d

Please sign in to comment.