How do I retrieve a user id from mvc3
This might be a silly question but whats the best practise for getting a userid. I have created an mvc app using the membership provider and have it all wired up and working. I have a number of controllers that require the userID to perform crud actions and need to be able to use it.
Do I store it in开发者_JS百科 a cookie or is there a better way of doing it?
Thanks for any suggestions!
You shouldn't have to worry about messing around with cookies or anything like that--Membership should take care of all that for you. All you need to do is check if the user is authenticated, and if so pull out the username
HttpContext.Current.User.Identity.IsAuthenticated;
and
var userName = HttpContext.Current.User.Identity.Name;
HttpContext.User.Identity.Name
精彩评论