A-S*_*ani 3 asp.net asp.net-mvc asp.net-identity
题
如何User.Identity.GetUserId()找到当前用户的ID?
它是从Cookies中找到用户ID,还是查询数据库?还是其他方法?
问题
由于某种原因,当我将有效值添加到Http请求标头并将请求发送到控制器的端点时,User.Identity.GetUserId()返回:nullBearer Token
// MVC Controller Action Method
[Authorize]
public HttpResponseMessage(UserInfoViewModel model)
{
// Request passes the Authorization filter since a valid oauth token
// is attached to the request header.
string userId = User.Identity.GetUserId();
// However, userId is null!
// Other stuff...
}
Run Code Online (Sandbox Code Playgroud)
User.Identity.GetUserId()如何找到当前用户的ID?
ClaimTypes.NameIdentifier是函数User.Identity.GetUserId()使用的声明。
您需要在您的授权代码中添加声明,
identity.AddClaim(ClaimTypes.NameIdentifier, user.Id);
Run Code Online (Sandbox Code Playgroud)
身份类型为ClaimIdentity。
| 归档时间: |
|
| 查看次数: |
1290 次 |
| 最近记录: |