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

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

在OnActionExecuting()方法中。

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

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

在控制器中,我可以简单地做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;
}

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...