|
1 | | -using Microsoft.AspNetCore.Authorization; |
| 1 | +using Azure.Storage.Blobs; |
| 2 | +using Microsoft.AspNetCore.Authorization; |
2 | 3 | using Microsoft.AspNetCore.Mvc; |
| 4 | +using Microsoft.Identity.Client; |
3 | 5 | using Microsoft.Identity.Web; |
4 | | -using Microsoft.WindowsAzure.Storage.Auth; |
5 | | -using Microsoft.WindowsAzure.Storage.Blob; |
6 | 6 | using System; |
7 | 7 | using System.Diagnostics; |
| 8 | +using System.IO; |
| 9 | +using System.Text; |
8 | 10 | using System.Threading.Tasks; |
9 | | -using Constants = WebApp_OpenIDConnect_DotNet.Infrastructure.Constants; |
10 | 11 | using WebApp_OpenIDConnect_DotNet.Models; |
11 | 12 | using WebApp_OpenIDConnect_DotNet.Services.Arm; |
12 | 13 | using WebApp_OpenIDConnect_DotNet.Services.GraphOperations; |
@@ -34,11 +35,11 @@ public IActionResult Index() |
34 | 35 | return View(); |
35 | 36 | } |
36 | 37 |
|
37 | | - [AuthorizeForScopes(Scopes = new[] {Constants.ScopeUserRead})] |
| 38 | + [AuthorizeForScopes(Scopes = new[] { WebApp_OpenIDConnect_DotNet.Infrastructure.Constants.ScopeUserRead})] |
38 | 39 | public async Task<IActionResult> Profile() |
39 | 40 | { |
40 | 41 | var accessToken = |
41 | | - await tokenAcquisition.GetAccessTokenForUserAsync(new[] {Constants.ScopeUserRead}); |
| 42 | + await tokenAcquisition.GetAccessTokenForUserAsync(new[] { WebApp_OpenIDConnect_DotNet.Infrastructure.Constants.ScopeUserRead}); |
42 | 43 |
|
43 | 44 | var me = await graphApiOperations.GetUserInformation(accessToken); |
44 | 45 | var photo = await graphApiOperations.GetPhotoAsBase64Async(accessToken); |
@@ -73,21 +74,30 @@ public async Task<IActionResult> Tenants() |
73 | 74 |
|
74 | 75 | public async Task<IActionResult> Blob() |
75 | 76 | { |
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"; |
85 | 78 | // replace the URL below with your storage account URL |
86 | 79 | 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; |
91 | 101 | return View(); |
92 | 102 | } |
93 | 103 |
|
|
0 commit comments