- VS 2013 Pro
- MVC 5
- Dapper
- IdentityFramework Core with ClaimsPrincipal
I have an MVC5 with Dapper/IdentityFramework with IUser < int > already, but ever since the default MVC Web app has changed, to include the ManageController, and SignInManager, I haven't been able to reproduce my efforts.
I have a custom Identity User and Claim class (AppUser, AppClaim, [and a link table AppUserClaim]), which are very close to the default ASP.Net Identity classes, but based on integer keys.
I also have a custom UserManager, UserStore, and ClaimsIdentityFactory...
When calling ConfigureAuth, I've included:
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
and, the exception occurs after the user is created, so probably when a new Name claim relationship is attempted to be assigned to the new Identity.
You can see from the stack trace that the call is Microsoft.AspNet.Identity.UserManagerExtensions.CreateIdentity
So, when you look into the repository for (Identity on GitHub), I see:
var identity = new ClaimsIdentity(IdentityOptions.TwoFactorUserIdCookieAuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, info.UserId));
So perhaps I'm right? The 'info' member is of type TwoFactorAuthenticationInfo. When passing the Contructor param to 'new Claim' it's name is UserId - so is this the problem?
How do I get the IdentityFramework to realize my Custom Identity User class doesn't have UserId?
Or is it that the system is complaining about the Claim constructor?
Why does it even care about UserId because even the default [AspNetUsers] table doesn't have a UserId!?
The error I get, when registering a new user, is below:
UserId not found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: UserId not found.
Source Error:
Line 19: {
Line 20: // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
Line 21: var userIdentity = manager.CreateIdentity<FreeAppUser, int>(this, DefaultAuthenticationTypes.ApplicationCookie);
Line 22: // Add custom user claims here
Line 23: return userIdentity;
Source File: c:\Projects\FreeWeb\FreeIdentity\Models\FreeAppUser.cs Line: 21
Stack Trace:
[InvalidOperationException: UserId not found.]
Microsoft.AspNet.Identity.<GetSecurityStampAsync>d__42.MoveNext() +817
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24
Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +123
Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +1395
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24
Microsoft.AspNet.Identity.AsyncHelper.RunSync(Func`1 func) +409
Microsoft.AspNet.Identity.UserManagerExtensions.CreateIdentity(UserManager`2 manager, TUser user, String authenticationType) +260
FreeIdentity.Models.FreeAppUser.GenerateUserIdentity(FreeAppUserManager manager) in c:\Projects\FreeWeb\FreeIdentity\Models\FreeAppUser.cs:21
FreeIdentity.Models.FreeAppUser.GenerateUserIdentityAsync(FreeAppUserManager manager) in c:\Projects\FreeWeb\FreeIdentity\Models\FreeAppUser.cs:15
FreeWeb.ApplicationSignInManager.CreateUserIdentityAsync(FreeAppUser user) in c:\Projects\FreeWeb\FreeWeb\App_Start\IdentityConfig.cs:30
etc..