在asp.net web api中的ExceptionFilterAttribute中获取userId信息
问题描述:
我正在使用asp.net web api项目。我添加了一个ExceptionFilterAttribute
。在asp.net web api中的ExceptionFilterAttribute中获取userId信息
public class ExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
//How to get user id.
}
}
在我的项目中,我启用了匿名身份验证。我正在尝试使用HttpContext.Current.User.Identity.Name;
来获取userId,但该值始终是一个空字符串。我是否需要包含任何金块包以获取userId?
有时,我可能通过移动客户端获得请求,那么,如何检查userId以及如何从移动客户端获取请求?
答
首先,用于获取用户id正确的语法是:
HttpContext.Current.User.Identity.GetUserId();
更重要的是,你怎么能得到匿名身份验证的用户id?用户必须通过身份验证才能获取userId。您必须禁用匿名身份验证,并启用Windows身份验证。
你也可以去Asp.Identity UseManager,并获取用户标识,如:
RequestContext.Principal.Identity.GetUserId();