Skip to content

Commit 4b00255

Browse files
authored
Merge pull request #686 from Azure-Samples/fix-b2c-claim-issue
Update B2C sample to use the unique sub claim as todo owner
2 parents ce2276d + 1a00b5a commit 4b00255

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

4-WebApp-your-API/4-2-B2C/Client/Views/TodoList/Create.cshtml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
<input asp-for="Title" class="form-control" />
1717
<span asp-validation-for="Title" class="text-danger"></span>
1818
</div>
19-
<div class="form-group">
20-
<label asp-for="Owner" class="control-label"></label>
21-
<input asp-for="Owner" class="form-control" value="@Html.DisplayTextFor(model => model.Owner)"/>
22-
<span asp-validation-for="Owner" class="text-danger"></span>
23-
</div>
2419
<div class="form-group">
2520
<input type="submit" value="Create" class="btn btn-primary" />
2621
</div>

4-WebApp-your-API/4-2-B2C/TodoListService/Controllers/TodoListController.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,21 @@ public class TodoListController : Controller
4646
public TodoListController(IHttpContextAccessor contextAccessor)
4747
{
4848
this._contextAccessor = contextAccessor;
49+
string owner = this._contextAccessor.HttpContext.User.Claims.FirstOrDefault(x => x.Type == "sub")?.Value;
4950

5051
// Pre-populate with sample data
5152
if (TodoStore.Count == 0)
5253
{
53-
TodoStore.Add(1, new Todo() { Id = 1, Owner = $"{this._contextAccessor.HttpContext.User.Identity.Name}", Title = "Pick up groceries" });
54-
TodoStore.Add(2, new Todo() { Id = 2, Owner = $"{this._contextAccessor.HttpContext.User.Identity.Name}", Title = "Finish invoice report" });
54+
TodoStore.Add(1, new Todo() { Id = 1, Owner = owner, Title = "Pick up groceries" });
55+
TodoStore.Add(2, new Todo() { Id = 2, Owner = owner, Title = "Finish invoice report" });
5556
}
5657
}
5758

5859
// GET: api/values
5960
[HttpGet]
6061
public IEnumerable<Todo> Get()
6162
{
62-
string owner = User.Identity.Name;
63+
string owner = User.Claims.FirstOrDefault(x => x.Type == "sub")?.Value;
6364
return TodoStore.Values.Where(x => x.Owner == owner);
6465
}
6566

@@ -80,8 +81,10 @@ public void Delete(int id)
8081
[HttpPost]
8182
public IActionResult Post([FromBody] Todo todo)
8283
{
84+
string owner = User.Claims.FirstOrDefault(x => x.Type == "sub")?.Value;
8385
int id = TodoStore.Values.OrderByDescending(x => x.Id).FirstOrDefault().Id + 1;
84-
Todo todonew = new Todo() { Id = id, Owner = HttpContext.User.Identity.Name, Title = todo.Title };
86+
87+
Todo todonew = new Todo() { Id = id, Owner = owner, Title = todo.Title };
8588
TodoStore.Add(id, todonew);
8689

8790
return Ok(todo);

4-WebApp-your-API/4-2-B2C/TodoListService/Startup.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Identity.Web;
1010
using Microsoft.AspNetCore.Authentication.JwtBearer;
11+
using System.IdentityModel.Tokens.Jwt;
1112

1213
namespace TodoListService
1314
{
@@ -25,17 +26,15 @@ public void ConfigureServices(IServiceCollection services)
2526
{
2627
// This is required to be instantiated before the OpenIdConnectOptions starts getting configured.
2728
// By default, the claims mapping will map claim names in the old format to accommodate older SAML applications.
28-
// 'http://schemas.microsoft.com/ws/2008/06/identity/claims/role' instead of 'roles'
29+
// For instance, 'http://schemas.microsoft.com/ws/2008/06/identity/claims/role' instead of 'roles' claim.
2930
// This flag ensures that the ClaimsIdentity claims collection will be built from the claims in the token
30-
// JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
31+
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
3132

3233
// Adds Microsoft Identity platform (AAD v2.0) support to protect this Api
3334
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
3435
.AddMicrosoftIdentityWebApi(options =>
3536
{
3637
Configuration.Bind("AzureAdB2C", options);
37-
38-
options.TokenValidationParameters.NameClaimType = "name";
3938
},
4039
options => { Configuration.Bind("AzureAdB2C", options); });
4140

0 commit comments

Comments
 (0)