asp.net-mvc - ASP.NET MVC 在自定义操作过滤器中查找当前用户名

标签 asp.net-mvc action-filter

我正在为 asp.net MVC 创建一个自定义操作过滤器。

OnActionExecuting()方法。

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    string userName =  ?????// how can I get this?
}   

我需要找出当前用户名(我正在使用表单例份验证)

在 Controller 中,我可以简单地执行 User.Identity.Name
有没有办法在 ActionFilter 中获取用户名?

最佳答案

string userName = filterContext.HttpContext.User.Identity.Name;

如果您想先检查是否有经过身份验证的用户:
string userName = null;
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
    userName = filterContext.HttpContext.User.Identity.Name;
}

关于asp.net-mvc - ASP.NET MVC 在自定义操作过滤器中查找当前用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7825104/

相关文章:

asp.net - IConfiguration 不包含 Get() 的定义(EF7、vNext)

asp.net-mvc - 一种在 ASP.NET MVC 中排除操作过滤器的方法?

.net - ASP.NET MVC、ActionFilters、静态类和传递数据

asp.net-mvc-3 - MVC 3 依赖注入(inject)与 Ninject 2.2 + 全局 Action 过滤器

asp.net - 调试时导出 IIS 进程内 session 数据

c# - 在 ASP.NET MVC3 中混合 Javascript 和 Razor 时对变量范围感到困惑

c# - 使用 CaSTLe Windsor 将依赖项注入(inject) CustomAttribute

scala - 如何在 Play 2.1 中过滤访问代码请求

asp.net-mvc - 为 IIS6 设置正在 build 的页面

javascript - 通过单击动态插入的链接来调用函数