There is already an object named ‘AspNetRoles’ in the database

Add-Migration InitialMigrations -IgnoreChanges This should generate a blank “InitialMigration” file. Now, add any desired changes to the class you want. Once changes are added, run the update command again: update-database -verbose Now the automatic migration will be applied and the table will be altered with your changes. Edit: Here is a solution to migrate identity … Read more

Change User Id type to int in ASP.NET Identity in VS2015

IdentityModels.cs change to this: // New derived classes public class UserRole : IdentityUserRole<int> { } public class UserClaim : IdentityUserClaim<int> { } public class UserLogin : IdentityUserLogin<int> { } public class Role : IdentityRole<int, UserRole> { public Role() { } public Role(string name) { Name = name; } } public class UserStore : UserStore<ApplicationUser, Role, … Read more

Get ExtraData from MVC5 framework OAuth/OWin identity provider with external auth provider

Recently I had to get access to Google profile’s picture as well and here is how I solve it… If you just enable the code app.UseGoogleAuthentication(); in the Startup.Auth.cs file it’s not enough because in this case Google doesn’t return any information about profile’s picture at all (or I didn’t figure out how to get … Read more

Disable User in ASPNET identity 2.0

When you create a site with the Identity bits installed, your site will have a file called “IdentityModels.cs”. In this file is a class called ApplicationUser which inherits from IdentityUser. // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://devblogs.microsoft.com/aspnet/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates/ to learn more. public class … Read more

“Context cannot be used while the model is being created” exception with ASP.NET Identity

The problem was that we were NOT using the factory pattern that MS recommends. You can use Factory implementation to get an instance of UserManager from the OWIN context. … This is a recommended way of getting an instance of UserManager per request for the application. As a result, “the same context instance is accessed … Read more

Get UserID of logged-in user in Asp.Net MVC 5

The answer is right there in your code. What does this return? var userID = User.Identity.GetUserId(); If you are using ASP.NET Identity, then after logging in (and redirecting to another page), the IPrincipal.IIdentity should be a ClaimsIdentity. You can try this: var claimsIdentity = User.Identity as ClaimsIdentity; if (claimsIdentity != null) { // the principal … Read more

Register IAuthenticationManager with Simple Injector

As TrailMax already mentioned, the exception you got probably got raised during the call to container.Verify(). At application start-up time there is no HttpContext, hence the exception. Although the removal of the call to container.Verify() will ‘solve’ the problem, I would advise against doing this and I will suggest a better solution below. NightOwl888 references … Read more

Unable to edit db entries using EFCore, EntityState.Modified: “Database operation expected to affect 1 row(s) but actually affected 0 row(s).”

Unless there is a hidden exception that is hiding behind this as a dumb random exception, the reason is clearly stated in the exception. Check the Id on the role object as you receive it on your Edit action and try to lookup that id in the database. The exception message you see states that, … Read more