在ActionExecutedContext上获取用于MVC的用户名?

时间:2017-03-16 19:24:51

标签: asp.net-mvc logging action-filter actionfilterattribute actioncontext

如何在MVC中获取当前登录用户的名称?我试过下面的代码,我可以得到日期和方法名,但没有得到用户名。

public void OnActionExecuted(ActionExecutedContext filterContext)
{
    AcibademUniversitesiDBEntities model = new AcibademUniversitesiDBEntities();
    Logging logbilgi = new Logging();

    logbilgi.date= DateTime.Now.ToString();
    logbilgi.Method = filterContext.ActionDescriptor.ActionName;
    logbilgi.text= filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
    //logbilgi.user= filterContext.RequestContext.HttpContext.User.Identity.Name;
    //logbilgi.user= filterContext.HttpContext.User.Identity.Name;
    logbilgi.user= HttpContext.Current.User.Identity.Name;
    model.Logging.Add(logbilgi);
    model.SaveChanges();
}

public void OnActionExecuting(ActionExecutingContext filterContext)
{
    AcibademUniversitesiDBEntities model = new AcibademUniversitesiDBEntities();
    Logging logbilgi = new Logging();

    logbilgi.date= DateTime.Now.ToString();
    logbilgi.Method = filterContext.ActionDescriptor.ActionName;
    logbilgi.text= filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
    //logbilgi.user= filterContext.RequestContext.HttpContext.User.Identity.Name;
    //logbilgi.user= filterContext.HttpContext.User.Identity.Name;
    logbilgi.user= HttpContext.Current.User.Identity.Name;
    model.Logging.Add(logbilgi);
    model.SaveChanges();
}

2 个答案:

答案 0 :(得分:0)

在web.config中,在system.web中,你有这样的东西吗?

<authentication mode="Windows" />

答案 1 :(得分:0)

这是一个很长的镜头,但你会尝试这个吗?

logbilgi.user = filterContext.HttpContext.Current.User.Identity.Name;