Skip to content

Commit 1fcfaff

Browse files
author
Kalyan Krishna
committed
Code review comments addressed
1 parent e473d34 commit 1fcfaff

File tree

16 files changed

+120
-122
lines changed

16 files changed

+120
-122
lines changed

AppCreationScripts/Cleanup.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ This function removes the Azure AD applications for the sample. These applicatio
5656
{
5757
Remove-AzureADApplication -ObjectId $apps.ObjectId
5858
}
59-
Get-AzureRmADServicePrincipal -SearchString "TodoListService-ManualJwt" | ForEach-Object {Remove-AzureRmADServicePrincipal -ObjectId $_.Id -Confirm:$false}
59+
# Get-AzureRmADServicePrincipal -SearchString "TodoListService-ManualJwt" | ForEach-Object {Remove-AzureRmADServicePrincipal -ObjectId $_.Id -Confirm:$false}
6060
Write-Host "Removed TodoListService-ManualJwt."
6161

6262
Write-Host "Removing 'client' (TodoListClient-ManualJwt) if needed"
@@ -66,7 +66,7 @@ This function removes the Azure AD applications for the sample. These applicatio
6666
{
6767
Remove-AzureADApplication -ObjectId $apps.ObjectId
6868
}
69-
Get-AzureRmADServicePrincipal -SearchString "TodoListClient-ManualJwt" | ForEach-Object {Remove-AzureRmADServicePrincipal -ObjectId $_.Id -Confirm:$false}
69+
# Get-AzureRmADServicePrincipal -SearchString "TodoListClient-ManualJwt" | ForEach-Object {Remove-AzureRmADServicePrincipal -ObjectId $_.Id -Confirm:$false}
7070
Write-Host "Removed TodoListClient-ManualJwt."
7171

7272
}

AppCreationScripts/Configure.ps1

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ Function ConfigureApplications
101101
so that they are consistent with the Applications parameters
102102
#>
103103

104-
$commonendpoint = "common"
105-
106104
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
107105
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
108106

@@ -150,8 +148,8 @@ Function ConfigureApplications
150148
$owner = Get-AzureADApplicationOwner -ObjectId $serviceAadApplication.ObjectId
151149
if ($owner -eq $null)
152150
{
153-
Add-AzureADApplicationOwner -ObjectId $serviceAadApplication.ObjectId -RefObjectId $user.ObjectId
154-
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($serviceServicePrincipal.DisplayName)'"
151+
Add-AzureADApplicationOwner -ObjectId $serviceAadApplication.ObjectId -RefObjectId $user.ObjectId
152+
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($serviceServicePrincipal.DisplayName)'"
155153
}
156154

157155
Write-Host "Done creating the service application (TodoListService-ManualJwt)"
@@ -174,8 +172,8 @@ Function ConfigureApplications
174172
$owner = Get-AzureADApplicationOwner -ObjectId $clientAadApplication.ObjectId
175173
if ($owner -eq $null)
176174
{
177-
Add-AzureADApplicationOwner -ObjectId $clientAadApplication.ObjectId -RefObjectId $user.ObjectId
178-
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($clientServicePrincipal.DisplayName)'"
175+
Add-AzureADApplicationOwner -ObjectId $clientAadApplication.ObjectId -RefObjectId $user.ObjectId
176+
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($clientServicePrincipal.DisplayName)'"
179177
}
180178

181179
Write-Host "Done creating the client application (TodoListClient-ManualJwt)"
@@ -212,7 +210,7 @@ Function ConfigureApplications
212210
ReplaceSetting -configFilePath $configFile -key "ida:ClientId" -newValue $clientAadApplication.AppId
213211
ReplaceSetting -configFilePath $configFile -key "todo:TodoListResourceId" -newValue $serviceIdentifierUri
214212
ReplaceSetting -configFilePath $configFile -key "todo:TodoListBaseAddress" -newValue $serviceAadApplication.HomePage
215-
213+
216214
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html
217215
}
218216

AppCreationScripts/sample.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"Sample": {
3-
"Title": "How to manually validating a JWT access token using Microsoft identity platform (formerly Azure Active Directory for developers)",
3+
"Title": "How to manually validate a JWT access token using Microsoft identity platform (formerly Azure Active Directory for developers)",
44
"Level": 300,
55
"Client": ".NET Desktop App (WPF)",
66
"Service": "ASP.NET Web API",

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ client: .NET Desktop App (WPF)
77
service: ASP.NET Web API
88
endpoint: AAD v2.0
99
---
10-
# How to manually validating a JWT access token using Microsoft identity platform (formerly Azure Active Directory for developers)
10+
# How to manually validate a JWT access token using Microsoft identity platform (formerly Azure Active Directory for developers)
1111

1212
![Build badge](https://identitydivision.visualstudio.com/_apis/public/build/definitions/a7934fdd-dcde-4492-a406-7fad6ac00e17/18/badge)
1313

1414
## About this sample
1515

16-
A Web API is secured by [validating the token](https://docs.microsoft.com/en-us/azure/active-directory/develop/access-tokens#validating-tokens) they receive from callers. When a developer generates a skeleton Web API code using [Visual Studio](https://aka.ms/vsdownload), token validation libraries and code to carry out basic token validation is automatically generated in the project. An example of the generated code using the [asp.net security middleware](https://github.com/aspnet/Security) and [Microsoft Identity Model Extension for .NET](https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet) to validate tokens is provided below.
16+
A Web API that accepts bearer token is secured by [validating the token](https://docs.microsoft.com/en-us/azure/active-directory/develop/access-tokens#validating-tokens) they receive from callers. When a developer generates a skeleton Web API code using [Visual Studio](https://aka.ms/vsdownload), token validation libraries and code to carry out basic token validation is automatically generated in the project. An example of the generated code using the [asp.net security middleware](https://github.com/aspnet/Security) and [Microsoft Identity Model Extension for .NET](https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet) to validate tokens is provided below.
1717

1818
```CSharp
1919
public void ConfigureAuth(IAppBuilder app)
@@ -29,7 +29,7 @@ A Web API is secured by [validating the token](https://docs.microsoft.com/en-us/
2929
}
3030
```
3131

32-
The code above will validate the issuer, audience, and the signing tokens, but often the developer's requirements are more than what the defaults provide. Examples of these requirements can be:
32+
The code above will validate the issuer, audience, and the signing tokens, which is usually sufficient for most scenarios. But often the developer's requirements are more than what the defaults provide. Examples of these requirements can be:
3333

3434
- Restricting the Web API to one or more Apps (App IDs)
3535
- Restricting the Web API to just one or more tenants (Issuers)
@@ -322,7 +322,7 @@ If you are using this sample with an Azure AD B2C custom policy, you might want
322322

323323
Use [Stack Overflow](http://stackoverflow.com/questions/tagged/msal) to get support from the community.
324324
Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before.
325-
Make sure that your questions or comments are tagged with [`adal` `msal` `dotnet`].
325+
Make sure that your questions or comments are tagged with [`adal` `msal` `dotnet` `azure-active-directory`].
326326

327327
If you find a bug in the sample, please raise the issue on [GitHub Issues](../../issues).
328328

TodoListClient/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<add key="todo:TodoListResourceId" value="[Enter App ID URI of TodoListService-ManualJwt, e.g. api://{clientID}]" />
1010
<add key="ida:AADInstance" value="https://login.microsoftonline.com/{0}/v2.0" />
1111
<add key="todo:TodoListBaseAddress" value="https://localhost:44324" />
12-
<add key="todo:TodoListScope" value="user_impersonation"/>
12+
<add key="todo:TodoListScope" value="user_impersonation" />
1313
</appSettings>
1414
</configuration>

TodoListClient/MainWindow.xaml.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
The MIT License (MIT)
33
4-
Copyright (c) 2015 Microsoft Corporation
4+
Copyright (c) 2018 Microsoft Corporation
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -20,19 +20,18 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2020
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2121
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
23-
*/
24-
23+
*/
2524

2625
using Microsoft.Identity.Client;
2726
using System;
2827
using System.Collections.Generic;
2928
using System.Configuration;
29+
3030
// The following using statements were added for this sample.
3131
using System.Globalization;
3232
using System.Linq;
3333
using System.Net.Http;
3434
using System.Net.Http.Headers;
35-
using System.Runtime.InteropServices;
3635
using System.Web.Script.Serialization;
3736
using System.Windows;
3837

@@ -51,25 +50,28 @@ public partial class MainWindow : Window
5150
// The Authority is the sign-in URL of the tenant.
5251
//
5352
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
53+
5454
private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
5555
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
56-
56+
5757
private static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
5858

5959
//
6060
// To authenticate to the To Do list service, the client needs to know the service's App ID URI.
6161
// To contact the To Do list service we need it's URL as well.
6262
//
6363
private static string todoListResourceId = ConfigurationManager.AppSettings["todo:TodoListResourceId"];
64+
6465
private static string todoListBaseAddress = ConfigurationManager.AppSettings["todo:TodoListBaseAddress"];
65-
public static string [] scopes = { $"{todoListResourceId}/{ConfigurationManager.AppSettings["todo:TodoListScope"]}" };
66+
public static string[] scopes = { $"{todoListResourceId}/{ConfigurationManager.AppSettings["todo:TodoListScope"]}" };
6667

6768
private HttpClient httpClient = new HttpClient();
6869
private readonly IPublicClientApplication _app;
6970

7071
// Button strings
71-
const string signInString = "Sign In";
72-
const string clearCacheString = "Clear Cache";
72+
private const string signInString = "Sign In";
73+
74+
private const string clearCacheString = "Clear Cache";
7375

7476
public MainWindow()
7577
{
@@ -100,7 +102,7 @@ private async void GetTodoList(bool isAppStarting)
100102
// Get an access token to call the ToDo service.
101103
//
102104
AuthenticationResult result = null;
103-
105+
104106
try
105107
{
106108
result = await _app.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
@@ -145,7 +147,6 @@ private async void GetTodoList(bool isAppStarting)
145147

146148
if (response.IsSuccessStatusCode)
147149
{
148-
149150
// Read the response and databind to the GridView to display To Do items.
150151
string s = await response.Content.ReadAsStringAsync();
151152
JavaScriptSerializer serializer = new JavaScriptSerializer();
@@ -181,7 +182,7 @@ private async void AddTodoItem(object sender, RoutedEventArgs e)
181182
MessageBox.Show("Please enter a value for the To Do item name");
182183
return;
183184
}
184-
185+
185186
//
186187
// Get an access token to call the To Do service.
187188
//
@@ -215,7 +216,6 @@ private async void AddTodoItem(object sender, RoutedEventArgs e)
215216
MessageBox.Show("Unexpected error: " + message);
216217
});
217218

218-
219219
return;
220220
}
221221

@@ -304,7 +304,6 @@ private async void SignIn(object sender = null, RoutedEventArgs args = null)
304304

305305
UserName.Content = Properties.Resources.UserNotSignedIn;
306306
}
307-
308307
}
309308

310309
// Set user name to text box
@@ -323,4 +322,4 @@ private void SetUserName(IAccount userInfo)
323322
UserName.Content = userName;
324323
}
325324
}
326-
}
325+
}

TodoListClient/TodoItem.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,12 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2121
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
2323
*/
24-
using System;
25-
using System.Collections.Generic;
26-
using System.Linq;
27-
using System.Text;
28-
using System.Threading.Tasks;
2924

3025
namespace TodoListClient
3126
{
32-
class TodoItem
27+
internal class TodoItem
3328
{
3429
public string Title { get; set; }
3530
public string Owner { get; set; }
3631
}
37-
}
32+
}

TodoListClient/TodoListClient-ManualJwt.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@
108108
<SubType>Designer</SubType>
109109
</None>
110110
</ItemGroup>
111-
<ItemGroup>
112-
<Folder Include="Utils\" />
113-
</ItemGroup>
111+
<ItemGroup />
114112
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
115113
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
116114
Other similar extension points exist, see Microsoft.Common.targets.

TodoListClient/TokenCacheHelper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@
2525
//
2626
//------------------------------------------------------------------------------
2727

28+
using Microsoft.Identity.Client;
2829
using System.IO;
2930
using System.Security.Cryptography;
30-
using Microsoft.Identity.Client;
3131

3232
namespace TodoListClient
3333
{
34-
static class TokenCacheHelper
34+
internal static class TokenCacheHelper
3535
{
36-
3736
/// <summary>
3837
/// Path to the token cache
3938
/// </summary>
@@ -62,17 +61,18 @@ private static void AfterAccessNotification(TokenCacheNotificationArgs args)
6261
{
6362
// reflect changes in the persistent store
6463
File.WriteAllBytes(CacheFilePath,
65-
ProtectedData.Protect(args.TokenCache.SerializeMsalV3(),
66-
null,
64+
ProtectedData.Protect(args.TokenCache.SerializeMsalV3(),
65+
null,
6766
DataProtectionScope.CurrentUser)
6867
);
6968
}
7069
}
7170
}
71+
7272
internal static void EnableSerialization(ITokenCache tokenCache)
7373
{
7474
tokenCache.SetBeforeAccess(BeforeAccessNotification);
7575
tokenCache.SetAfterAccess(AfterAccessNotification);
7676
}
7777
}
78-
}
78+
}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Web;
5-
using System.Web.Mvc;
1+
using System.Web.Mvc;
62

73
namespace TodoListService_ManualJwt.Controllers
84
{
@@ -15,4 +11,4 @@ public ActionResult Index()
1511
return View();
1612
}
1713
}
18-
}
14+
}

0 commit comments

Comments
 (0)