繁体   English   中英

从User.Identity.GetUserId()获取值时,ModelState验证错误

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

在我的WebApi控制器发布函数中,我通过User.identity.GetUserId()将用户ID分配给发布的对象,但是ModelState.isValid总是返回验证错误,即

用户ID为必填项

而将值分配给必需的属性。 因此,我从userId属性中删除了必需的属性。 它正在工作,但是我需要知道为什么ModelState不知道User.Identity.GetUserId()已经提供了用户ID。

如果您使用的是Web API 2,请使用User.identity.GetUserId()代替

RequestContext.Principal.Identity.GetUserId()

要么

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

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

暂无
暂无

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

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