-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHomeController.cs
166 lines (141 loc) · 6.6 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using AzureSAPODataReader.Models;
using Microsoft.Extensions.Configuration;
using Simple.OData.Client;
using AzureODataReader.Models;
using System.Net.Http;
using Microsoft.Identity.Web;
using System.Net;
using System.Text.Json;
using System.Text;
using Microsoft.AspNetCore.Authentication;
namespace AzureSAPODataReader.Controllers
{
[Authorize]
public class HomeController : Controller
{
private ITokenAcquisition TokenAcquisition;
private string baseAPIUrl;
public HomeController(IConfiguration configuration, ITokenAcquisition tokenAcquisition, ISAPTokenCache tokenCache = null)
{
_Configuration = configuration;
//token cache will be null if APIM policy used for SAP principal propagation instead of client-side caching.
TokenCache = tokenCache;
baseAPIUrl = configuration.GetValue<string>("AzureAd:ApiBaseAddress");
TokenAcquisition = tokenAcquisition;
}
public IConfiguration _Configuration { get; }
public ISAPTokenCache TokenCache { get; }
public async Task<IActionResult> Index()
{
// Acquire the access token.
string[] scopes = new string[] { _Configuration.GetValue<string>("AzureAd:ScopeForAccessToken") };
ODataClient client = await getODataClientForUsername(scopes);
var products = await client
.For<ProductViewModel>("Products")
.Top(10)
.FindEntriesAsync();
return View(products);
}
private async Task<ODataClient> getODataClientForUsername(string[] scopes)
{
string accessToken = await TokenAcquisition.GetAccessTokenForUserAsync(scopes);
ODataClient client = null;
if(TokenCache != null)
{
//If ISAPTokenCache is injected, use it to get the token cache
SAPTokenCacheContent content = await TokenCache.GetSAPTokenCacheContentAsync(accessToken, baseAPIUrl);
client = new ODataClient(content.getODataClientSettingsAsync());//SetODataToken(Configuration.GetValue<string>("AzureAd:ApiBaseAddress"), content.accessToken, true));
}else
{
//assume this is done server-side by a proxy like Azure APIM (using policy)
client = new ODataClient(getODataClientSettings(accessToken));
}
return client;
}
private ODataClientSettings getODataClientSettings(string accessToken)
{
var myClientOverride = new HttpClient() { BaseAddress = new Uri(baseAPIUrl) };
myClientOverride.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
//myClientOverride.DefaultRequestHeaders.Add("Authorization", Configuration.GetValue<string>("AzureAd:BasicAuth"));
//myClientOverride.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _Configuration.GetValue<string>("AzureAd:Ocp-Apim-Subscription-Key"));
myClientOverride.DefaultRequestHeaders.Add("Ocp-Apim-Trace", _Configuration.GetValue<string>("AzureAd:Ocp-Apim-Trace"));
var oDataClientSettings = new ODataClientSettings(myClientOverride);
oDataClientSettings.OnTrace = (x, y) => Console.WriteLine("TRACE---->" + string.Format(x, y));
return oDataClientSettings;
}
//plain http implementation for initial test
private async Task<HttpClient> getHttpClientForUsername(string[] scopes)
{
string accessToken = await TokenAcquisition.GetAccessTokenForUserAsync(scopes);
SAPTokenCacheContent content = await TokenCache.GetSAPTokenCacheContentAsync(accessToken, baseAPIUrl);
var handler = new HttpClientHandler(){
ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true
};
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {content.accessToken}");
//client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _Configuration.GetValue<string>("AzureAd:Ocp-Apim-Subscription-Key"));
client.BaseAddress = new Uri(baseAPIUrl);
return client;
}
public async Task<IActionResult> Edit(string id)
{
// Acquire the access token.
string[] scopes = new string[] { _Configuration.GetValue<string>("AzureAd:ScopeForAccessToken") };
ODataClient client = await getODataClientForUsername(scopes);
ProductViewModel product = await client
.For<ProductViewModel>("Products")
.Key(id)
.FindEntryAsync();
ViewBag.ProductId = id;
return View(product);
}
// POST: Movies/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(ProductViewModel model)
{
if (ModelState.IsValid)
{
// Acquire the access token.
string[] scopes = new string[] { _Configuration.GetValue<string>("AzureAd:ScopeForAccessToken") };
ODataClient client = await getODataClientForUsername(scopes);
ProductViewModel product = await client
.For<ProductViewModel>("Products")
.Key(model.Id)
.Set(new { Price = model.Price })
.UpdateEntryAsync();
return RedirectToAction("Index");
}
return View(model);
}
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
return View();
}
public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";
return View();
}
public IActionResult Privacy()
{
return View();
}
[AllowAnonymous]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}