![](/img/trans.png)
[英]Why is my SOAP call from ASP.net Core 2.2 returning a null result?
[英]ASP.NET Core 2 - GetUserIdAsync returning null Result with Guid Ids
我目前正在將一個站點移植到 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()
調用UserManager.GetUserIdAsync()
,它返回Result
的null
值, Write()
抱怨,即使我通過的User
實例有07c0423e-f36b-1410-8007-800000000000
在Id
。
如果我直接在調用GenerateEmailConfirmationTokenAsync()
的同一個UserManager
實例上調用userManager.GetUserIdAsync(user)
,我會在Result
得到相同的null
值。
有沒有人有任何想法如何解決這個問題?
編輯:根據要求,可以在github.com/BenjaminNolan/GetUserIdAsyncNullExample找到一個功能示例
您遇到的問題是您的User
類中的這些行:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[DefaultValue("NEWSEQUENTIALID()")]
new public Guid Id { get; set; }
您在此處使用new 修飾符會抑制以下編譯器警告:
“User.Id”隱藏繼承的成員“IdentityUser.Id”。
此警告通知您,如果您通過User
引用(而不是IdentityUser<Guid>
引用 - 基類)訪問Id
,您將使用不同的Id
屬性。 如果您檢查控制器中user.Id
的值並將其與((IdentityUser<Guid>)user).Id
的值進行比較,您可以看到它的樣子(轉換到基類以訪問其Id
屬性)。
在您的情況下很容易修復 - 只需從您的User
類中刪除Id
屬性:您已經指定IdentityUser<TKey>
中的現有Id
屬性將是Guid
類型,並且它已經配置為密鑰等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.