-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathHomeController.cs
66 lines (57 loc) · 2.12 KB
/
HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Threading.Tasks;
using WebApp_OpenIDConnect_DotNet.Models;
using Microsoft.Identity.Web;
using WebAppOpenIDConnectDotNet;
using Azure.Storage.Blobs;
using System.Text;
using System.IO;
namespace WebApp_OpenIDConnect_DotNet.Controllers
{
[Authorize]
public class HomeController : Controller
{
ITokenAcquisition _tokenAcquisition;
public HomeController(ITokenAcquisition tokenAcquisition)
{
_tokenAcquisition = tokenAcquisition;
}
public IActionResult Index()
{
return View();
}
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
return View();
}
[AuthorizeForScopes(Scopes = new string[] { "https://storage.azure.com/user_impersonation" })]
public async Task<IActionResult> Blob()
{
string message = await CreateBlob(new TokenAcquisitionTokenCredential(_tokenAcquisition));
ViewData["Message"] = message;
return View();
}
private static async Task<string> CreateBlob(TokenAcquisitionTokenCredential tokenCredential)
{
Uri blobUri = new Uri("https://blobstorageazuread123.blob.core.windows.net/sample-container/Blob1.txt");
BlobClient blobClient = new BlobClient(blobUri, tokenCredential);
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";
}
[AllowAnonymous]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}