简体   繁体   中英

ModelState Validation Error when get value from User.Identity.GetUserId()

in my WebApi controller post function I assign user id to my posted object through User.identity.GetUserId() but ModelState.isValid always return validation error that

user id is required

while the value is assigned to the required property. So I removed the required attribute from userId property. It is working but I need to know as to why ModelState does not know that the user id has already been provided with User.Identity.GetUserId() .

If you are using Web API 2, instead of User.identity.GetUserId() use

RequestContext.Principal.Identity.GetUserId()

or

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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