Diver Dan
Diver Dan

Reputation: 9963

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 a cookie or is there a better way of doing it?

Thanks for any suggestions!

Upvotes: 1

Views: 1359

Answers (2)

ataddeini
ataddeini

Reputation: 4951

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;

Upvotes: 1

Paul Creasey
Paul Creasey

Reputation: 28894

HttpContext.User.Identity.Name

Upvotes: 1

Related Questions