【问题标题】:Geting userId information in ExceptionFilterAttribute in asp.net web api在asp.net web api的ExceptionFilterAttribute中获取用户ID信息
【发布时间】:2017-02-09 19:58:09
【问题描述】:

我正在使用 asp.net web api 项目。我添加了ExceptionFilterAttribute

public class ExceptionFilter : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext context)
        {
               //How to get user id. 
        }
    }

在我的项目中,我启用了匿名身份验证。我正在尝试使用 HttpContext.Current.User.Identity.Name; 获取 userId,但该值始终为空字符串。我是否需要包含任何 nugget 包才能获取 userId?

有时,我可能会通过移动客户端收到请求,在这种情况下,我如何检查 userId 以及如何确定请求是否来自移动客户端?

【问题讨论】:

    标签: c# asp.net asp.net-web-api asp.net-web-api2


    【解决方案1】:

    首先,获取userId的正确语法是:

    HttpContext.Current.User.Identity.GetUserId();
    

    更重要的是,如何获取匿名身份验证的 userId?用户必须经过身份验证才能获取 userId。您必须禁用匿名身份验证,并启用 Windows 身份验证。

    您也可以使用 Asp.Identity UseManager,获取 userId 如下:

    RequestContext.Principal.Identity.GetUserId();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2015-02-25
      • 1970-01-01
      • 2021-06-13
      • 2018-03-30
      • 2021-03-09
      相关资源
      最近更新 更多