我正在为asp.net MVC创建一个自定义操作过滤器。
在OnActionExecuting()
方法中。
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string userName = ?????// how can I get this?
}
我需要找出当前用户名(我使用的是表单身份验证)
在控制器中,我可以简单地执行User.Identity.Name
有没有办法在ActionFilter中获得用户名?
发布于 2011-10-20 01:03:16
string userName = filterContext.HttpContext.User.Identity.Name;
如果您想首先检查是否有经过身份验证的用户:
string userName = null;
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
userName = filterContext.HttpContext.User.Identity.Name;
}
https://stackoverflow.com/questions/7825104
复制相似问题