2016-05-15 33 views
0

在我的WebAPI控制器的功能後,通過我分配User.identity.GetUserId()用戶ID給我貼的對象,但始終ModelState.isValid返回的ModelState驗證錯誤時,從User.Identity.GetUserId()

用戶ID需要驗證錯誤獲取價值

將值分配給所需的屬性。所以我從userId屬性中刪除了必需的屬性。它正在工作,但我需要知道爲什麼ModelState不知道用戶標識已提供User.Identity.GetUserId()

+0

也許'User'爲空? – demo

+0

不是,因爲它保存在數據庫中。問題是我無法在此屬性上使用[必需的]屬性。但是刪除這個屬性並不能確定userId是否被傳遞。 – Khalil

回答

1

如果您正在使用Web API 2,而不是User.identity.GetUserId()使用

RequestContext.Principal.Identity.GetUserId() 

using Microsoft.AspNet.Identity; 
using Microsoft.AspNet.Identity.Owin; 

Request.GetOwinContext().GetUserManager<ApplicationUserManager>() 
+0

謝謝...它的工作。 – Khalil