简体   繁体   English

ASP.NET Core 2 - GetUserIdAsync 返回带有 Guid Id 的空结果

[英]ASP.NET Core 2 - GetUserIdAsync returning null Result with Guid Ids

I'm currently porting a site to ASP.NET Core 2 and am getting the following Exception thrown when I call userManager.GenerateEmailConfirmationTokenAsync(user) with a user class that extends IdentityUser<Guid> :我目前正在将一个站点移植到 ASP.NET Core 2,当我使用扩展IdentityUser<Guid>的用户类调用userManager.GenerateEmailConfirmationTokenAsync(user) ,抛出以下异常:

System.ArgumentNullException: Value cannot be null.
Parameter name: value
   at System.IO.BinaryWriter.Write(String value)
   at Microsoft.AspNetCore.Identity.DataProtectorTokenProvider`1.<GenerateAsync>d__11.MoveNext()

GenerateAsync() makes a call to UserManager.GetUserIdAsync() , which is returning the null value in Result , which Write() is complaining about, even though the User instance I'm passing through has 07c0423e-f36b-1410-8007-800000000000 in Id . GenerateAsync()调用UserManager.GetUserIdAsync() ,它返回Resultnull值, Write()抱怨,即使我通过的User实例有07c0423e-f36b-1410-8007-800000000000Id

If I call userManager.GetUserIdAsync(user) directly on the same UserManager instance on which I'm calling GenerateEmailConfirmationTokenAsync() I get this same null value in Result .如果我直接在调用GenerateEmailConfirmationTokenAsync()的同一个UserManager实例上调用userManager.GetUserIdAsync(user) ,我会在Result得到相同的null值。

Does anyone have any ideas how to resolve this?有没有人有任何想法如何解决这个问题?

Edit : As requested, a functional example can be found at github.com/BenjaminNolan/GetUserIdAsyncNullExample编辑:根据要求,可以在github.com/BenjaminNolan/GetUserIdAsyncNullExample找到一个功能示例

The problem you have is with these lines from your User class:您遇到的问题是您的User类中的这些行

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[DefaultValue("NEWSEQUENTIALID()")]
new public Guid Id { get; set; }

Your use of the new modifier here is suppressing the following compiler warning:您在此处使用new 修饰符会抑制以下编译器警告:

'User.Id' hides inherited member 'IdentityUser.Id'. “User.Id”隐藏继承的成员“IdentityUser.Id”。

This warning informs you that if you access Id via a User reference (rather than eg a IdentityUser<Guid> reference - the base class), you will be working with a different Id property.此警告通知您,如果您通过User引用(而不是IdentityUser<Guid>引用 - 基类)访问Id ,您将使用不同的Id属性。 You can see what this looks like if you inspect the value of user.Id in your controller and compare that with the value of ((IdentityUser<Guid>)user).Id (casting to the base class in order to access its Id property).如果您检查控制器中user.Id的值并将其与((IdentityUser<Guid>)user).Id的值进行比较,您可以看到它的样子(转换到基类以访问Id属性)。

It's easy to fix in your case - just remove the Id property from your User class: you've already specified that the existing Id property from IdentityUser<TKey> will be of type Guid and its already configured to be the key, etc.在您的情况下很容易修复 - 只需从您的User类中删除Id属性:您已经指定IdentityUser<TKey>中的现有Id属性将是Guid类型,并且它已经配置为密钥等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM