aspnet / Identity

[Archived] ASP.NET Core Identity is the membership system for building ASP.NET Core web applications, including membership, login, and user data. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
1.97k stars 871 forks source link

Updating Email fails with Unexpected Error #1969

Closed bhosale-ajay closed 5 years ago

bhosale-ajay commented 5 years ago

Framework Version - .Net Core 2.1

Project Creation

  1. Create a Asp .Net Core Web project with Authentication
  2. Run Migrations

Start Application

  1. Register a user with email - abc@abc.com
  2. Register a user with email - abc@xyz.com
  3. Try to update the second user profile and change the email to abc@abc.com. On submit the code throws an unexpected error. The DB query shows email has updated.

StackTrace -

InvalidOperationException: Unexpected error occurred setting name for user with ID '56172fdd-bb5c-48eb-89b7-3beeff546f92'.
Microsoft.AspNetCore.Identity.UI.Pages.Account.Manage.Internal.IndexModel<TUser>.OnPostAsync()

Overall Email and UserName are confusing, Updating UserName on updating email is very specific implementation.

jamiewest commented 5 years ago

The UI code uses the email address for the user name, which when set, updates the NormalizedUserName field which has a unique constraint set (see here). In your case, when you update your email address to match your first record, you are getting an error from the database for violating the constraint.

Here is the UI code that is using the Input.Email field for the username.

bhosale-ajay commented 5 years ago

In this case, it will be helpful to show a validation error instead of throwing an exception. If I override the default UI, and allow user modifying the email without changing username, the login fails if username and email does not match. Team should reconsider this behavior, or provide a way to override this,

blowdart commented 5 years ago

This is planned for 3.0, so closing as dupe - https://github.com/aspnet/Identity/issues/1721