Input string was not in a correct format - wrong context is used

Viewed 787

After changing ASP.NET Core Identity to use int rather than GUID (as per this blog post) I am receiving the following error:

ArgumentException: 395438ed-1cd9-4420-8a58-3f3b1f550bfc is not a valid value for Int32. Parameter name: value System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) Microsoft.AspNetCore.Identity.UserStoreBase.ConvertIdFromString(string id) Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore.FindByIdAsync(string userId, CancellationToken cancellationToken) Microsoft.AspNetCore.Identity.UserManager.FindByIdAsync(string userId) Microsoft.AspNetCore.Identity.UserManager.GetUserAsync(ClaimsPrincipal principal) Microsoft.AspNetCore.Identity.SignInManager.ValidateSecurityStampAsync(ClaimsPrincipal principal) Microsoft.AspNetCore.Identity.SecurityStampValidator.ValidateAsync(CookieValidatePrincipalContext context) Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler.HandleAuthenticateAsync() Microsoft.AspNetCore.Authentication.AuthenticationHandler.AuthenticateAsync() Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, string scheme) Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

when trying to query the database. Any clues as to why that might be?

1 Answers

As per one of the comments in the blog post:

Thank you, this article was really useful. One gotcha for me was that after swapping to use an int when I first loaded identity server again I got an error on this line

Microsoft.AspNetCore.Identity.UserStoreBase.ConvertIdFromString(string id).

Turns out I had a cookie using the old guid format, simple fix was to clear cookies.

The issue is that some of your previous logins were likely done when the ID was a GUID. You changed it to int, and so now it doesn't know how to handle the GUID any more. The simplest solution will be to delete your cookies, effectively logging you out and forcing you to login again (using int instead).

Related