Skip to content

Commit feb5043

Browse files
committed
Updating the client with suggestions from Jenny
The controller now only accepts access_as_user as the manual creation and the creation by scripts both create that scope now;
1 parent 2ff6162 commit feb5043

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

2. Web API now calls Microsoft Graph/TodoListClient/MainWindow.xaml.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ await _app.AcquireTokenInteractive(scopes)
188188
else if (proposedAction == "consent")
189189
{
190190
if (System.Windows.MessageBox.Show("You need to consent to the Web API. If you press Ok, you'll be redirected to a browser page to consent",
191-
"Consent needed for the Web API",
191+
"Consent needed for the Web API",
192192
MessageBoxButton.OKCancel) == MessageBoxResult.OK)
193193
{
194194
Process.Start(consentUri);
@@ -265,6 +265,7 @@ private async void AddTodoItem(object sender, RoutedEventArgs e)
265265
return;
266266
}
267267

268+
// Call the To Do service.
268269
// Once the token has been returned by MSAL, add it to the http authorization header, before making the call to access the To Do service.
269270
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
270271

@@ -321,6 +322,14 @@ private async void SignIn(object sender = null, RoutedEventArgs args = null)
321322
var result = await _app.AcquireTokenSilent(Scopes, accounts.FirstOrDefault())
322323
.ExecuteAsync()
323324
.ConfigureAwait(false);
325+
326+
Dispatcher.Invoke(() =>
327+
{
328+
SignInButton.Content = ClearCacheString;
329+
SetUserName(result.Account);
330+
GetTodoList();
331+
}
332+
);
324333
}
325334
catch (MsalUiRequiredException)
326335
{

2. Web API now calls Microsoft Graph/TodoListService/Controllers/TodoListController.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ public TodoListController(ITokenAcquisition tokenAcquisition)
3636
static readonly ConcurrentBag<TodoItem> TodoStore = new ConcurrentBag<TodoItem>();
3737

3838
/// <summary>
39-
/// The Web API will only accept tokens 1) for users, 2) having the user_impersonation scope for
40-
/// this API (if you created the app using the App creation script) or 'access_as_user' if you created
41-
/// it following the README.md.
39+
/// /// The Web API will only accept tokens 1) for users, and
40+
/// 2) having the access_as_user scope for this API
4241
/// </summary>
43-
static string[] scopeRequiredByAPI = new string[] { "user_impersonation", "access_as_user" };
42+
static readonly string[] scopeRequiredByAPI = new string[] { "access_as_user" };
4443

4544
// GET: api/values
4645
[HttpGet]
@@ -71,7 +70,7 @@ public async void Post([FromBody]TodoItem todo)
7170
{
7271
HttpContext.Response.ContentType = "text/plain";
7372
HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
74-
await HttpContext.Response.WriteAsync("An authentication error occurred while acquiring a token for downstream API\n" + ex.ErrorCode + "\n"+ ex.Message);
73+
await HttpContext.Response.WriteAsync("An authentication error occurred while acquiring a token for downstream API\n" + ex.ErrorCode + "\n" + ex.Message);
7574
}
7675
catch (Exception ex)
7776
{

0 commit comments

Comments
 (0)