How can I find the name of the user and set current time in an MVC 3 Controller?
I am trying to set the name of my user as follows:
_t开发者_如何学编程estEdit.Test.CreatedBy = Context.User.Identity.Name
_testEdit.Test.Created = now();
Seems that the controller has no knowledge of Context and no knowledge of now().
Does anyone know how I can set the user name and the current time (into a datetime field).
Thanks,
Take this:
_testEdit.Test.CreatedBy = User.Identity.Name;
_testEdit.Test.Created = DateTime.Now;
Controller has a User
property so no requirements to use Context
. For the current date/time use the static getter from DateTime
.
When you are using Form authentication and you will log in, then HttpContext.User.Identity.Name (you could use just User.Identity.Name) will always be filled.
To get current time user DateTime.Now
精彩评论