Skip to content

Commit 1e44386

Browse files
authored
Add Email claim (#18580)
1 parent e937884 commit 1e44386

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

src/Identity/Extensions.Core/ref/Microsoft.Extensions.Identity.Core.netcoreapp.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public AuthenticatorTokenProvider() { }
1515
public partial class ClaimsIdentityOptions
1616
{
1717
public ClaimsIdentityOptions() { }
18+
public string EmailClaimType { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
1819
public string RoleClaimType { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
1920
public string SecurityStampClaimType { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
2021
public string UserIdClaimType { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }

src/Identity/Extensions.Core/ref/Microsoft.Extensions.Identity.Core.netstandard2.0.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public AuthenticatorTokenProvider() { }
1515
public partial class ClaimsIdentityOptions
1616
{
1717
public ClaimsIdentityOptions() { }
18+
public string EmailClaimType { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
1819
public string RoleClaimType { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
1920
public string SecurityStampClaimType { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
2021
public string UserIdClaimType { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }

src/Identity/Extensions.Core/src/ClaimsIdentityOptions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ public class ClaimsIdentityOptions
2525
/// </summary>
2626
public string UserIdClaimType { get; set; } = ClaimTypes.NameIdentifier;
2727

28+
/// <summary>
29+
/// Gets or sets the ClaimType used for the user email claim. Defaults to <see cref="ClaimTypes.Email"/>.
30+
/// </summary>
31+
public string EmailClaimType { get; set; } = ClaimTypes.Email;
32+
2833
/// <summary>
2934
/// Gets or sets the ClaimType used for the security stamp claim. Defaults to "AspNet.Identity.SecurityStamp".
3035
/// </summary>
3136
public string SecurityStampClaimType { get; set; } = "AspNet.Identity.SecurityStamp";
3237
}
33-
}
38+
}

src/Identity/Extensions.Core/src/UserClaimsPrincipalFactory.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ protected virtual async Task<ClaimsIdentity> GenerateClaimsAsync(TUser user)
8181
Options.ClaimsIdentity.RoleClaimType);
8282
id.AddClaim(new Claim(Options.ClaimsIdentity.UserIdClaimType, userId));
8383
id.AddClaim(new Claim(Options.ClaimsIdentity.UserNameClaimType, userName));
84+
if (UserManager.SupportsUserEmail)
85+
{
86+
var email = await UserManager.GetEmailAsync(user);
87+
if (!string.IsNullOrEmpty(email))
88+
{
89+
id.AddClaim(new Claim(Options.ClaimsIdentity.EmailClaimType, email));
90+
}
91+
}
8492
if (UserManager.SupportsUserSecurityStamp)
8593
{
8694
id.AddClaim(new Claim(Options.ClaimsIdentity.SecurityStampClaimType,
@@ -154,4 +162,4 @@ protected override async Task<ClaimsIdentity> GenerateClaimsAsync(TUser user)
154162
return id;
155163
}
156164
}
157-
}
165+
}

src/Identity/test/Identity.Test/UserClaimsPrincipalFactoryTest.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,33 @@ await Assert.ThrowsAsync<ArgumentNullException>("user",
3030
}
3131

3232
[Theory]
33-
[InlineData(false, false, false)]
34-
[InlineData(false, true, false)]
35-
[InlineData(true, false, false)]
36-
[InlineData(true, true, false)]
37-
[InlineData(true, false, true)]
38-
[InlineData(true, true, true)]
39-
public async Task EnsureClaimsIdentityHasExpectedClaims(bool supportRoles, bool supportClaims, bool supportRoleClaims)
33+
[InlineData(true, false, false, false)]
34+
[InlineData(true, true, false, false)]
35+
[InlineData(true, false, true, false)]
36+
[InlineData(true, true, true, false)]
37+
[InlineData(false, false, false, true)]
38+
[InlineData(false, true, false, true)]
39+
[InlineData(false, false, false, false)]
40+
[InlineData(false, true, false, false)]
41+
[InlineData(true, false, false, true)]
42+
[InlineData(true, true, false, true)]
43+
[InlineData(true, false, true, true)]
44+
[InlineData(true, true, true, true)]
45+
public async Task EnsureClaimsIdentityHasExpectedClaims(bool supportRoles, bool supportClaims, bool supportRoleClaims, bool supportsUserEmail)
4046
{
4147
// Setup
4248
var userManager = MockHelpers.MockUserManager<PocoUser>();
4349
var roleManager = MockHelpers.MockRoleManager<PocoRole>();
44-
var user = new PocoUser { UserName = "Foo" };
50+
var user = new PocoUser { UserName = "Foo", Email = "foo@bar.com" };
4551
userManager.Setup(m => m.SupportsUserClaim).Returns(supportClaims);
4652
userManager.Setup(m => m.SupportsUserRole).Returns(supportRoles);
53+
userManager.Setup(m => m.SupportsUserEmail).Returns(supportsUserEmail);
4754
userManager.Setup(m => m.GetUserIdAsync(user)).ReturnsAsync(user.Id);
4855
userManager.Setup(m => m.GetUserNameAsync(user)).ReturnsAsync(user.UserName);
56+
if (supportsUserEmail)
57+
{
58+
userManager.Setup(m => m.GetEmailAsync(user)).ReturnsAsync(user.Email);
59+
}
4960
var roleClaims = new[] { "Admin", "Local" };
5061
if (supportRoles)
5162
{
@@ -90,6 +101,7 @@ public async Task EnsureClaimsIdentityHasExpectedClaims(bool supportRoles, bool
90101
Assert.Contains(
91102
claims, c => c.Type == manager.Options.ClaimsIdentity.UserNameClaimType && c.Value == user.UserName);
92103
Assert.Contains(claims, c => c.Type == manager.Options.ClaimsIdentity.UserIdClaimType && c.Value == user.Id);
104+
Assert.Equal(supportsUserEmail, claims.Any(c => c.Type == manager.Options.ClaimsIdentity.EmailClaimType && c.Value == user.Email));
93105
Assert.Equal(supportRoles, claims.Any(c => c.Type == manager.Options.ClaimsIdentity.RoleClaimType && c.Value == "Admin"));
94106
Assert.Equal(supportRoles, claims.Any(c => c.Type == manager.Options.ClaimsIdentity.RoleClaimType && c.Value == "Local"));
95107
foreach (var cl in userClaims)

0 commit comments

Comments
 (0)