2
  • 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..
5
  • Where is Dapper comes to play? Commented Oct 24, 2014 at 19:54
  • 1
    Dapper is my DAL - instead of EntityFramework. It should be irrelevant, and I hope it is Commented Oct 24, 2014 at 20:16
  • 1
    Pretty certain it is irrelevant. I am getting the same issue with EF6. Commented Oct 30, 2014 at 3:49
  • @klatzib You have the same issue with EF 6 with User<Int>? I don't - I wanted to start from scratch and can't duplicate this in EF6 when I followed asp.net/identity/overview/extensibility/… Commented Oct 30, 2014 at 12:55
  • 1
    But I know Dapper is the irrelevant item - I am now able to get Dapper to work with Identity and Claims based systems - let the interfaces build the API for userstore and be careful in ConfigureAuth in Startup.Auth.cs to ensure you store your version of ApplicationDbContext in the OwinContext, instantiate your UserManager with the same class name as your poco (ie var manager = new IdentityUserManager( new IdentityUserStore<IdentityUser>(context.Get<ApplicationDbContext>())); ) and really just follow the examples in the link I gave above Commented Oct 30, 2014 at 13:29

2 Answers 2

1

I have a solution in a solution.

From comments above:

let the interfaces build the API for userstore and be careful in ConfigureAuth in Startup.Auth.cs to ensure you store your version of ApplicationDbContext in the OwinContext, instantiate your UserManager with the same class name as your poco (ie var manager = new IdentityUserManager( new IdentityUserStore(context.Get())); ) and really just follow the examples in the link I gave above

I've seem to have solved all my Identity issues.

This is with:

  • Identity 2.1 Core & Owin ( IUser < int > )
  • Latest Dapper
  • Latest DapperExtensions (Thad Smith & Page Brooks)

Read and use the example in my GitHub repo:

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Robert, could you post what your solution was specifically here? i'll be happy to vote both your question and answer up if you do. Thanks!
Well, read the comments above and examine my Github I linked above here. The GitHub sample works with Dapper and has no User issues.
Right, but this really isn't an answer. This is you saying you have an answer and for me to go dig it up. Which I'll happily do, I just figured if you wanted the extra points, I'd ask you to provide the answer here to make this question more valuable to the community. No biggie, I've already solved my problem anyway. Writing a good answer: meta.stackexchange.com/questions/7656/…
That's what I was explaining in my last comment on the question itself...what I did..
Thanks for posting this question, it helped. Voted.
0

I was getting the same error "UserId not found".

The problem in my code was that the UserStore.CreateAsync was inserting the record in the database using Dapper, however the value of the primary key i.e. the Id column was not updated in the user object passed in to the method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.