MVC5 Dapper Identity User Register错误:找不到UserId?

时间:2014-10-24 19:47:38

标签: asp.net-mvc asp.net-identity claims-based-identity

  • VS 2013 Pro
  • MVC 5
  • 小巧玲珑
  • IdentityFramework Core with ClaimsPrincipal

我有一个带有Dapper / IdentityFramework的MVC5和IUser< int>已经,但自从默认的MVC Web应用程序发生变化,包括ManageController和 SignInManager 以来,我还没有能够重现我的努力。

我有一个自定义的Identity User和Claim类(AppUser,AppClaim,[和一个链接表AppUserClaim]),它们非常接近默认的ASP.Net Identity类,但基于整数键。

我还有自定义UserManager,UserStore和ClaimsIdentityFactory ......

在致电ConfigureAuth时,我已经包含:

app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

并且,在创建用户之后发生异常,因此可能在尝试将新的名称声明关系分配给新标识时。

您可以从堆栈跟踪中看到该调用是Microsoft.AspNet.Identity.UserManagerExtensions.CreateIdentity

因此,当您查看(Identity on GitHub)的存储库时,我看到:

var identity = new ClaimsIdentity(IdentityOptions.TwoFactorUserIdCookieAuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, info.UserId));

所以也许我是对的? '信息' member的类型为TwoFactorAuthenticationInfo。将Contructor参数传递给“新版权声明”时它的名字是UserId - 这是问题所在吗?

如何让IdentityFramework实现我的自定义身份用户类没有UserId?

或者系统是否在抱怨Claim构造函数?

为什么它甚至关心UserId,因为即使是默认的[AspNetUsers]表也没有UserId!?

我在注册新用户时遇到的错误如下:

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..

2 个答案:

答案 0 :(得分:1)

我在解决方案中有一个解决方案。

来自上述评论:

  

让接口为用户构建API并小心   Startup.Auth.cs中的ConfigureAuth,以确保您存储您的版本   OwinContext中的ApplicationDbContext,实例化您的UserManager   与您的poco具有相同的类名(即var manager = new   IdentityUserManager(新的   IdentityUserStore(context.Get()));   )并且真的只是按照我上面给出的链接中的示例

我似乎已经解决了所有身份问题。

这是:

  • Identity 2.1 Core&amp; Owin(IUser&lt; int&gt;)
  • 最新Dapper
  • 最新的DapperExtensions(Thad 史密斯&amp; Page Brooks)

阅读并使用我的GitHub repo中的示例:

答案 1 :(得分:0)

我收到了相同的错误“UserId not found”。

我的代码中的问题是UserStore.CreateAsync使用Dapper在数据库中插入记录,但主键的值(即Id列)未在传入方法的用户对象中更新。