[英]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()
,它返回Result
的null
值, Write()
抱怨,即使我通过的User
实例有07c0423e-f36b-1410-8007-800000000000
在Id
。
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.