Skip to content

Commit 1377167

Browse files
authored
Merge pull request #2 from AzureADQuickStarts/desktopCallingAnotherV2WebApi
Updating the code to show a V2 application
2 parents 2945c69 + 8275c4c commit 1377167

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

TodoListClient/App.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
</startup>
66
<appSettings>
77
<add key="ida:ClientId" value="{Enter the Application Id that you copied from the App Registration Portal.}" />
8+
<add key="todo:Scope" value="{Enter the scope of the Web API, as copied from the App Registration Portal, for instance api://[WebApi-AppId]/access_as_user where [WebApi-AppId] is a GUID" />
9+
<!--
10+
Note that the [WebAPI-AppId], which is the Application Id of the called Web API can be the same as the ida:ClientId
11+
as V2 apps enable several platforms for a same application. But this can also be a different applications
12+
-->
813
<add key="todo:TodoListBaseAddress" value="https://localhost:44321/" />
914
</appSettings>
1015
</configuration>

TodoListClient/MainWindow.xaml.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ public partial class MainWindow : Window
4343

4444
// The todoListBaseAddress is the address of your Web API
4545
private static string todoListBaseAddress = ConfigurationManager.AppSettings["todo:TodoListBaseAddress"];
46+
private static string todoListScope = ConfigurationManager.AppSettings["todo:Scope"];
4647

4748
private HttpClient httpClient = new HttpClient();
4849
private PublicClientApplication app = null;
4950

50-
private string Scope
51+
private string[] Scopes
5152
{
5253
get
5354
{
54-
return $"api://{clientId}/access_as_user";
55+
return new string[] { todoListScope };
5556
}
5657
}
5758
protected override async void OnInitialized(EventArgs e)
@@ -69,13 +70,17 @@ protected override async void OnInitialized(EventArgs e)
6970
// get a token for the user without showing a UI.
7071
try
7172
{
72-
result = await app.AcquireTokenSilentAsync(new string[] { Scope }, app.Users.FirstOrDefault());
73+
result = await app.AcquireTokenSilentAsync(Scopes, app.Users.FirstOrDefault());
7374
// If we got here, a valid token is in the cache - or MSAL was able to get a new oen via refresh token.
7475
// Proceed to fetch the user's tasks from the TodoListService via the GetTodoList() method.
7576

7677
SignInButton.Content = "Clear Cache";
7778
GetTodoList();
7879
}
80+
catch (MsalUiRequiredException)
81+
{
82+
// The app should take no action and simply show the user the sign in button.
83+
}
7984
catch (MsalException ex)
8085
{
8186
if (ex.ErrorCode == "failed_to_acquire_token_silently")
@@ -116,7 +121,7 @@ private async void GetTodoList()
116121
// without invoking any UI prompt. AcquireTokenSilentAsync forces
117122
// MSAL to throw an exception if it cannot get a token silently.
118123

119-
result = await app.AcquireTokenSilentAsync(new string[] { Scope }, app.Users.FirstOrDefault());
124+
result = await app.AcquireTokenSilentAsync(Scopes, app.Users.FirstOrDefault());
120125
}
121126
catch (MsalException ex)
122127
{
@@ -180,7 +185,7 @@ private async void AddTodoItem(object sender, RoutedEventArgs e)
180185
AuthenticationResult result = null;
181186
try
182187
{
183-
result = await app.AcquireTokenSilentAsync(new string[] { Scope }, app.Users.FirstOrDefault());
188+
result = await app.AcquireTokenSilentAsync(Scopes, app.Users.FirstOrDefault());
184189
}
185190
catch (MsalException ex)
186191
{
@@ -255,7 +260,7 @@ private async void SignIn(object sender = null, RoutedEventArgs args = null)
255260
AuthenticationResult result = null;
256261
try
257262
{
258-
result = await app.AcquireTokenAsync(new string[] { Scope });
263+
result = await app.AcquireTokenAsync(Scopes);
259264
SignInButton.Content = "Clear Cache";
260265
GetTodoList();
261266
}

0 commit comments

Comments
 (0)