Skip to content
This repository was archived by the owner on Jan 23, 2020. It is now read-only.

Commit 4fa0e82

Browse files
committed
AcquireToken for Graph API first, best practice
1 parent 8c5262e commit 4fa0e82

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

TodoListWebApp/App_Start/Startup.Auth.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public partial class Startup
1818
public static string AppKey = String.Empty;
1919
public static string TodoListResourceId = String.Empty;
2020
public static string TodoListBaseAddress = String.Empty;
21+
public static string GraphResourceId = String.Empty;
2122

2223
public void ConfigureAuth(IApplicationBuilder app)
2324
{
@@ -27,6 +28,7 @@ public void ConfigureAuth(IApplicationBuilder app)
2728
AppKey = Configuration.Get("AzureAd:AppKey");
2829
TodoListResourceId = Configuration.Get("AzureAd:TodoListResourceId");
2930
TodoListBaseAddress = Configuration.Get("AzureAd:TodoListBaseAddress");
31+
GraphResourceId = Configuration.Get("AzureAd:GraphResourceId");
3032

3133
// Configure the Session Middleware, Used for Storing Tokens
3234
app.UseSession();
@@ -47,13 +49,12 @@ public void ConfigureAuth(IApplicationBuilder app)
4749

4850
public async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification notification)
4951
{
50-
// Acquire a Token for the TodoList Web API, and Cache it For Later Use
52+
// Acquire a Token for the Graph API and cache it. In the TodoListController, we'll use the cache to acquire a token to the Todo List API
5153
string userObjectId = notification.AuthenticationTicket.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
5254
ClientCredential clientCred = new ClientCredential(ClientId, AppKey);
5355
AuthenticationContext authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectId, notification.HttpContext.Session));
5456
AuthenticationResult authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(
55-
notification.Code, new Uri(notification.RedirectUri), clientCred, Startup.TodoListResourceId);
56-
57+
notification.Code, new Uri(notification.RedirectUri), clientCred, Startup.GraphResourceId);
5758
}
5859
}
5960
}

TodoListWebApp/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"AadInstance": "https://login.windows.net/{0}", // This is the public instance of Azure AD
77
"PostLogoutRedirectUri": "https://localhost:44322/",
88
"TodoListResourceId": "[Enter App ID URI of TodoListService, e.g. https://contoso.onmicrosoft.com/TodoListService]",
9-
"TodoListBaseAddress": "https://localhost:44321"
9+
"TodoListBaseAddress": "https://localhost:44321",
10+
"GraphResourceId": "https://graph.windows.net"
1011
}
1112
}

0 commit comments

Comments
 (0)