Question

I am calling this method from within a Controller method while logged in as a user, with the standard DefaultMembershipProvider code in MVC5 that comes completely out of the box. Like this:

 string LoggedInUsersId = User.Identity.GetUserId();

When debugging, this method call returns a GUID that is completely different to the "Id" field GUID stored in the AspNetUsers table. As I only have one user in this table, I have no idea where this other GUID is coming from?

I thought that the Id field value from the AspNetUsers table would be returned for the currently logged in user when you call User.Identity.GetUserId()?

I am definitely checking the same database/environment and looking at the right place.

Was it helpful?

Solution

Solved!

Thanks to this blog post (see point #2): https://digitaltoolfactory.net/blog/2013/10/five-things-should-should-know-about-the-new-asp-net-identity-membership-system/

The new MVC5 membership stuff had automatically created a new local database on my laptop called "DefaultConnection" where it had generated and placed all the membership tables and data. So GetUserId() was returning a GUID that lived locally in the AspNetUsers table from that local database which did not exist in my other main database that the application was using! Gah.

So the fix was changing this line inside of IdentityModels.cs:

 public ApplicationDbContext() : base("DefaultConnection") { }

to:

 public ApplicationDbContext() : base("MyConnectionString") { }

Now it works and everything is saved centrally into my application's main database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top