我正在为 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/