开发者

How can I find the name of the user and set current time in an MVC 3 Controller?

开发者 https://www.devze.com 2023-03-06 10:52 出处:网络
I am trying to set the name of my user as follows: _t开发者_如何学编程estEdit.Test.CreatedBy = Context.User.Identity.Name

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

0

精彩评论

暂无评论...
验证码 换一张
取 消