在操作过滤器上获取用户名 [英] Get User Name on Action Filter
本文介绍了在操作过滤器上获取用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将MVC4 Web应用程序与Web API一起使用.我想创建一个动作过滤器,并且想知道哪个用户(登录用户)执行了该动作.我该怎么办?
I use MVC4 web application with Web API. I want to create an action filter, and I want to know which user (a logged-in user) made the action. How can I do it?
public class ModelActionLog : ActionFilterAttribute
{
public override void OnActionExecuting(SHttpActionContext actionContext)
{
string username = ??
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
??
}
}
推荐答案
您可以尝试
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
string username = HttpContext.Current.User.Identity.Name;
}
首先检查经过身份验证的用户:
Check for authenticated user first:
string userName = null;
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
userName = HttpContext.Current.User.Identity.Name;
}
尝试使用
HttpContext.Current.User.Identity.Name
希望它对您有用
这篇关于在操作过滤器上获取用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文