我正在为asp.net MVC创建一个自定义动作过滤器。
在OnActionExecuting()方法中。
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- string userName = ?????// how can I get this?
- }
我需要找出当前的用户名(我正在使用表单身份验证)
在控制器中,我可以简单地做User.Identity.Name
解决方法
- string userName = filterContext.HttpContext.User.Identity.Name;
如果你想检查是否有一个经过身份验证的用户:
- string userName = null;
- if (filterContext.HttpContext.User.Identity.IsAuthenticated)
- {
- userName = filterContext.HttpContext.User.Identity.Name;
- }