Input string was not in a correct format error on using int keys

Viewed 3764

First i want to assure you that i have already read many posts with similar title on SO.

I have created an ASP.NET MVC project and changed the keys of template tables to int following this article

http://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity

But i am getting a runtime error in this line of StartUp.Auth.cs

getUserIdCallback: (id) => (id.GetUserId<int>()))

Error = System.FormatException: Input string was not in a correct format. Any help is appreciated.

4 Answers

I had a similar issue and got sick or resetting my cache. I think either my cookie was becoming invalid or the cookie changed and for some reason it couldn't be parsed anymore.

I found a solution in a GitHub repository issue: https://github.com/TypecastException/AspNet-Identity-2-With-Integer-Keys/issues/2#issuecomment-128280883

Using the following code fixed my issue

getUserIdCallback: (id) => GrabUserId(id))

public static int GrabUserId(System.Security.Claims.ClaimsIdentity claim)
{
    if (!int.TryParse(claim.GetUserId(), out int id))
        return 0;
    else
        return id;
}

When returning 0 it just logs you out because it couldn't recognize you as a user.

I really didn't want to change the taste of my browser experience. So, in my case, from the Chrome DevTools (F12) > Application > Clear Storage > Clear site data fixed it.

enter image description here

Related