Skip to content

EF UserStore FindByEmail will throw on dupes #8220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Identity/EntityFrameworkCore/src/UserOnlyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ public async override Task<TUser> FindByLoginAsync(string loginProvider, string
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
return Users.FirstOrDefaultAsync(u => u.NormalizedEmail == normalizedEmail, cancellationToken);
return Users.SingleOrDefaultAsync(u => u.NormalizedEmail == normalizedEmail, cancellationToken);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Identity/EntityFrameworkCore/src/UserStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ public async override Task<TUser> FindByLoginAsync(string loginProvider, string
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
return Users.FirstOrDefaultAsync(u => u.NormalizedEmail == normalizedEmail, cancellationToken);
return Users.SingleOrDefaultAsync(u => u.NormalizedEmail == normalizedEmail, cancellationToken);
}

/// <summary>
Expand Down
23 changes: 23 additions & 0 deletions src/Identity/EntityFrameworkCore/test/EF.Test/UserOnlyTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder.Internal;
using Microsoft.AspNetCore.Identity.Test;
Expand Down Expand Up @@ -65,5 +66,27 @@ public async Task EnsureStartupUsageWorks()
IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password));
IdentityResultAssert.IsSuccess(await userManager.DeleteAsync(user));
}

[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
[OSSkipCondition(OperatingSystems.Linux)]
[OSSkipCondition(OperatingSystems.MacOSX)]
public async Task FindByEmailThrowsWithTwoUsersWithSameEmail()
{
var userStore = _builder.ApplicationServices.GetRequiredService<IUserStore<IdentityUser>>();
var manager = _builder.ApplicationServices.GetRequiredService<UserManager<IdentityUser>>();

Assert.NotNull(userStore);
Assert.NotNull(manager);

var userA = new IdentityUser(Guid.NewGuid().ToString());
userA.Email = "[email protected]";
const string password = "1qaz@WSX";
IdentityResultAssert.IsSuccess(await manager.CreateAsync(userA, password));
var userB = new IdentityUser(Guid.NewGuid().ToString());
userB.Email = "[email protected]";
IdentityResultAssert.IsSuccess(await manager.CreateAsync(userB, password));
await Assert.ThrowsAsync<InvalidOperationException>(async () => await manager.FindByEmailAsync("[email protected]"));
}
}
}
17 changes: 17 additions & 0 deletions src/Identity/EntityFrameworkCore/test/EF.Test/UserStoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,23 @@ public async Task TwoUsersSamePasswordDifferentHash()
Assert.NotEqual(userA.PasswordHash, userB.PasswordHash);
}

[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
[OSSkipCondition(OperatingSystems.Linux)]
[OSSkipCondition(OperatingSystems.MacOSX)]
public async Task FindByEmailThrowsWithTwoUsersWithSameEmail()
{
var manager = CreateManager();
var userA = new IdentityUser(Guid.NewGuid().ToString());
userA.Email = "[email protected]";
IdentityResultAssert.IsSuccess(await manager.CreateAsync(userA, "password"));
var userB = new IdentityUser(Guid.NewGuid().ToString());
userB.Email = "[email protected]";
IdentityResultAssert.IsSuccess(await manager.CreateAsync(userB, "password"));
await Assert.ThrowsAsync<InvalidOperationException>(async () => await manager.FindByEmailAsync("[email protected]"));

}

[ConditionalFact]
[FrameworkSkipCondition(RuntimeFrameworks.Mono)]
[OSSkipCondition(OperatingSystems.Linux)]
Expand Down
2 changes: 2 additions & 0 deletions src/Identity/Extensions.Core/src/UserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,8 @@ public virtual async Task<IdentityResult> SetEmailAsync(TUser user, string email

/// <summary>
/// Gets the user, if any, associated with the normalized value of the specified email address.
/// Note: Its recommended that identityOptions.User.RequireUniqueEmail be set to true when using this method, otherwise
/// the store may throw if there are users with duplicate emails.
/// </summary>
/// <param name="email">The email address to return the user for.</param>
/// <returns>
Expand Down