0

如何在 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();
}
4

2 回答 2

0

在 Web.config 中,在 system.web 中,你有类似的东西吗?

<authentication mode="Windows" />
于 2017-03-16T19:36:02.240 回答
0

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

logbilgi.user = filterContext.HttpContext.Current.User.Identity.Name;
于 2017-03-16T19:51:32.660 回答