Allow IUserValidator.ValidateAsync return IdentityResult.Failed() without IdentityError
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
## Summary
UserManager.ValidateUserAsync will ignored failed if IUserValidator.ValidateAsync return IdentityResult.Failed() without error,
## Motivation and goals
By docs `https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.identityresult.failed?view=aspnetcore-7.0`,
IdentityError is optional, but empty error collection may lead to ValidateUserAsync ignored the fail.
Make ValidateUserAsync' returned value depending on failed is exists or not, not count of error.
Can avoid unexpected store writing operate when validator already returned fail.
## In scope
Check user entity meet all condition for DB schema, but not need to provide the descriptions of failure.
## Risks / unknowns
Exists project may configure to depend the count of error and ignored success status. If so, this design may break the exists code.
## Examples
```
public async Task ValidateAsync(UserManager manager, ApplicationUser user)
{
if (!user.Roles.Any()) { return IdentityResult.Success; }
var roleIds = user.Roles.Select(x => x.Id).ToArray();
var exists = await _context.Roles.CountAsync(x => roleIds.Contains(x.Id));
if (roleIds.Length == exists) { return IdentityResult.Success; }
return IdentityResult.Failed(); // Returned Failed without IdentityError
}
```
The user's role may be a mock entity and attached to ef. Coder may want to ensure all role is exists in db
`Context.Role.Attach(role);`
Current version of UserManager.ValidateUserAsync will ignored this failed and reutrn IdentityResult.Success because they can not collect any error.
https://github.com/dotnet/aspnetcore/blob/f49c1c7f7467c184ffb630086afac447772096c6/src/Identity/Extensions.Core/src/UserManager.cs#L2319-L2327
Contributor guide
Assessment
This issue has not been assessed yet.