Skip to content

Commit 0fd10f2

Browse files
authored
Merge pull request #96 from Azure-Samples/jmprieur/syncMicrosoftIdentityWebFromWebAppTutorial
Synching Microsoft.Identity.Web from the ASP.NET Core Web App tutorial
2 parents d9c3bff + 04eba0a commit 0fd10f2

File tree

72 files changed

+3595
-2494
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+3595
-2494
lines changed

1. Desktop app calls Web API/TodoListService/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public Startup(IConfiguration configuration)
4343
// This method gets called by the runtime. Use this method to add services to the container.
4444
public void ConfigureServices(IServiceCollection services)
4545
{
46-
services.AddProtectWebApiWithMicrosoftIdentityPlatformV2(Configuration);
46+
services.AddProtectedWebApi(Configuration);
4747

4848
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
4949
}

2. Web API now calls Microsoft Graph/README-incremental-instructions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,12 @@ Update `Startup.cs` file:
195195
by
196196

197197
```csharp
198-
services.AddProtectWebApiWithMicrosoftIdentityPlatformV2(Configuration)
198+
services.AddProtectedWebApi(Configuration)
199199
.AddProtectedApiCallsWebApis(Configuration, new string[] { "user.read" })
200200
.AddInMemoryTokenCaches();
201201
```
202202

203-
`AddProtectWebApiWithMicrosoftIdentityPlatformV2` does the following:
203+
`AddProtectedWebApi` does the following:
204204
- add the **Jwt**BearerAuthenticationScheme (Note the replacement of BearerAuthenticationScheme by **Jwt**BearerAuthenticationScheme)
205205
- set the authority to be the Microsoft identity platform v2.0 identity
206206
- sets the audiences to validate

2. Web API now calls Microsoft Graph/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,12 @@ Update `Startup.cs` file:
323323
by
324324

325325
```csharp
326-
services.AddProtectWebApiWithMicrosoftIdentityPlatformV2(Configuration)
326+
services.AddProtectedWebApi(Configuration)
327327
.AddProtectedApiCallsWebApis(Configuration, new string[] { "user.read" })
328328
.AddInMemoryTokenCaches();
329329
```
330330

331-
`AddProtectWebApiWithMicrosoftIdentityPlatformV2` does the following:
331+
`AddProtectedWebApi` does the following:
332332
- add the **Jwt**BearerAuthenticationScheme (Note the replacement of BearerAuthenticationScheme by **Jwt**BearerAuthenticationScheme)
333333
- set the authority to be the Microsoft identity platform identity
334334
- sets the audiences to validate

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using Microsoft.AspNetCore.Http;
1010
using Microsoft.AspNetCore.Mvc;
1111
using Microsoft.Identity.Client;
12-
using Microsoft.Identity.Web.Client;
12+
using Microsoft.Identity.Web;
1313
using Microsoft.Identity.Web.Resource;
1414
using Newtonsoft.Json;
1515
using System;
@@ -92,13 +92,13 @@ public async Task<string> CallGraphApiOnBehalfOfUser()
9292
// we use MSAL.NET to get a token to call the API On Behalf Of the current user
9393
try
9494
{
95-
string accessToken = await _tokenAcquisition.GetAccessTokenOnBehalfOfUser(HttpContext, scopes);
95+
string accessToken = await _tokenAcquisition.GetAccessTokenOnBehalfOfUserAsync(scopes);
9696
dynamic me = await CallGraphApiOnBehalfOfUser(accessToken);
9797
return me.userPrincipalName;
9898
}
9999
catch (MsalUiRequiredException ex)
100100
{
101-
_tokenAcquisition.ReplyForbiddenWithWwwAuthenticateHeader(HttpContext, scopes, ex);
101+
_tokenAcquisition.ReplyForbiddenWithWwwAuthenticateHeader(scopes, ex);
102102
return string.Empty;
103103
}
104104
}

2. Web API now calls Microsoft Graph/TodoListService/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using Microsoft.Extensions.Configuration;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Identity.Web;
10-
using Microsoft.Identity.Web.Client.TokenCacheProviders;
10+
using Microsoft.Identity.Web.TokenCacheProviders.InMemory;
1111

1212
namespace TodoListService
1313
{
@@ -23,7 +23,7 @@ public Startup(IConfiguration configuration)
2323
// This method gets called by the runtime. Use this method to add services to the container.
2424
public void ConfigureServices(IServiceCollection services)
2525
{
26-
services.AddProtectWebApiWithMicrosoftIdentityPlatformV2(Configuration)
26+
services.AddProtectedWebApi(Configuration)
2727
.AddProtectedApiCallsWebApis(Configuration)
2828
.AddInMemoryTokenCaches();
2929

3.-Web-api-call-Microsoft-graph-for-personal-accounts/README-incremental-instructions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,12 @@ Update `Startup.cs` file:
195195
by
196196

197197
```csharp
198-
services.AddProtectWebApiWithMicrosoftIdentityPlatformV2(Configuration)
198+
services.AddProtectedWebApi(Configuration)
199199
.AddProtectedApiCallsWebApis(Configuration, new string[] { "user.read" })
200200
.AddInMemoryTokenCaches();
201201
```
202202

203-
`AddProtectWebApiWithMicrosoftIdentityPlatformV2` does the following:
203+
`AddProtectedWebApi` does the following:
204204
- add the **Jwt**BearerAuthenticationScheme (Note the replacement of BearerAuthenticationScheme by **Jwt**BearerAuthenticationScheme)
205205
- set the authority to be the Microsoft identity platform v2.0 identity
206206
- sets the audiences to validate

3.-Web-api-call-Microsoft-graph-for-personal-accounts/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public class Startup
245245
// This method gets called by the runtime. Use this method to add services to the container.
246246
public void ConfigureServices(IServiceCollection services)
247247
{
248-
services.AddProtectWebApiWithMicrosoftIdentityPlatformV2(Configuration)
248+
services.AddProtectedWebApi(Configuration)
249249
.AddProtectedApiCallsWebApis(Configuration, new string[] { "user.read" })
250250
.AddInMemoryTokenCaches();
251251
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
@@ -262,7 +262,7 @@ public class Startup
262262
...
263263
public void ConfigureServices(IServiceCollection services)
264264
{
265-
services.AddProtectWebApiWithMicrosoftIdentityPlatformV2(Configuration)
265+
services.AddProtectedWebApi(Configuration)
266266
.AddProtectedApiCallsWebApis(Configuration, new string[] { "user.read", "offline_access" })
267267
.AddInMemoryTokenCaches();
268268

3.-Web-api-call-Microsoft-graph-for-personal-accounts/TodoListService/Controllers/TodoListController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2626
using Microsoft.AspNetCore.Http;
2727
using Microsoft.AspNetCore.Mvc;
2828
using Microsoft.Identity.Client;
29-
using Microsoft.Identity.Web.Client;
29+
using Microsoft.Identity.Web;
3030
using Microsoft.Identity.Web.Resource;
3131
using Newtonsoft.Json;
3232
using System;
@@ -110,13 +110,13 @@ public async Task<string> CallGraphApiOnBehalfOfUser()
110110
// we use MSAL.NET to get a token to call the API On Behalf Of the current user
111111
try
112112
{
113-
string accessToken = await _tokenAcquisition.GetAccessTokenOnBehalfOfUser(HttpContext, scopes);
113+
string accessToken = await _tokenAcquisition.GetAccessTokenOnBehalfOfUserAsync(scopes);
114114
dynamic me = await CallGraphApiOnBehalfOfUser(accessToken);
115115
return me.userPrincipalName;
116116
}
117117
catch (MsalUiRequiredException ex)
118118
{
119-
_tokenAcquisition.ReplyForbiddenWithWwwAuthenticateHeader(HttpContext, scopes, ex);
119+
_tokenAcquisition.ReplyForbiddenWithWwwAuthenticateHeader(scopes, ex);
120120
return string.Empty;
121121
}
122122
}

3.-Web-api-call-Microsoft-graph-for-personal-accounts/TodoListService/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2828
using Microsoft.Extensions.Configuration;
2929
using Microsoft.Extensions.DependencyInjection;
3030
using Microsoft.Identity.Web;
31-
using Microsoft.Identity.Web.Client.TokenCacheProviders;
31+
using Microsoft.Identity.Web.TokenCacheProviders.InMemory;
3232

3333
namespace TodoListService
3434
{
@@ -44,7 +44,7 @@ public Startup(IConfiguration configuration)
4444
// This method gets called by the runtime. Use this method to add services to the container.
4545
public void ConfigureServices(IServiceCollection services)
4646
{
47-
services.AddProtectWebApiWithMicrosoftIdentityPlatformV2(Configuration)
47+
services.AddProtectedWebApi(Configuration)
4848
.AddProtectedApiCallsWebApis(Configuration, new string[] { "user.read", "offline_access" })
4949
.AddInMemoryTokenCaches();
5050

Microsoft.Identity.Web.Test/AadIssuerValidatorTests.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
14
using Microsoft.Identity.Web.Resource;
25
using Microsoft.IdentityModel.Tokens;
36
using System;
@@ -12,7 +15,6 @@ public class AadIssuerValidatorTests
1215
{
1316
private const string Tid = "9188040d-6c67-4c5b-b112-36a304b66dad";
1417
private static readonly string Iss = $"https://login.microsoftonline.com/{Tid}/v2.0";
15-
private static readonly string Iss2 = $"https://sts.windows.net/{Tid}/v2.0";
1618
private static readonly IEnumerable<string> s_aliases = new[] { "login.microsoftonline.com", "sts.windows.net" };
1719

1820
[Fact]
@@ -45,20 +47,6 @@ public void PassingValidation()
4547
}
4648

4749

48-
[Fact]
49-
public void PassingValidationWithAlias()
50-
{
51-
// Arrange
52-
AadIssuerValidator validator = new AadIssuerValidator(s_aliases);
53-
Claim issClaim = new Claim("tid", Tid);
54-
Claim tidClaim = new Claim("iss", Iss2); // sts.windows.net
55-
JwtSecurityToken jwtSecurityToken = new JwtSecurityToken(issuer: Iss2, claims: new[] { issClaim, tidClaim });
56-
57-
// Act & Assert
58-
validator.Validate(Iss2, jwtSecurityToken,
59-
new TokenValidationParameters() { ValidIssuers = new[] { "https://login.microsoftonline.com/{tenantid}/v2.0" } });
60-
}
61-
6250
[Fact]
6351
public void TokenValidationParameters_ValidIssuer()
6452
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Identity.Client;
5+
using System.Security.Claims;
6+
7+
namespace Microsoft.Identity.Web
8+
{
9+
/// <summary>
10+
/// Extension methods dealing with IAccount instances.
11+
/// </summary>
12+
public static class AccountExtensions
13+
{
14+
/// <summary>
15+
/// Creates the <see cref="ClaimsPrincipal"/> from the values found
16+
/// in an <see cref="IAccount"/>
17+
/// </summary>
18+
/// <param name="account">The IAccount instance</param>
19+
/// <returns>A <see cref="ClaimsPrincipal"/> built from IAccount</returns>
20+
public static ClaimsPrincipal ToClaimsPrincipal(this IAccount account)
21+
{
22+
if (account != null)
23+
{
24+
return new ClaimsPrincipal(
25+
new ClaimsIdentity(new Claim[]
26+
{
27+
new Claim(ClaimConstants.Oid, account.HomeAccountId.ObjectId),
28+
new Claim(ClaimConstants.Tid, account.HomeAccountId.TenantId),
29+
new Claim(ClaimTypes.Upn, account.Username)
30+
})
31+
);
32+
}
33+
34+
return null;
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)