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