tags:

views:

28

answers:

1

I know I can use the attribute [Authorize(Roles="client")] to make sure that only authenticated clients can access a page. But after that, how can I access this user's personal data? How do I programmatically get that user's account ID from that action controller?

For example, on Stack Overflow, how does my personal account page access my personal data which is stored in a database?

So far, googling "authentication", "authorization" only helped me limit access to a controller action, not how to access that user's stuff.

+1  A: 

Assuming you're using the membership provider (which I'd assume so if you're using the attribute), the simplest way is:

var membership_user = Membership.GetUser();

which will return a MembershipUser for the currently logged-in user. From that, you can access any profile information you've set up, and also get the primary key to access anything else you've set up.

James

James S
Do you mean with "using System.Web.Security;"? I wasn't using that although I added that line to make your code compile. I just read a tutorial on authorization which said to use an attribute to block access and little more. Your code seems to be what I needed unless there is a prefered way of doing so in MVC!
PRINCESS FLUFF
Yes. It's part of System.Web.Security. No, not that I know of. Despite a lot of "special" things like the authorization attributes, a lot of functionality is still shared between asp.net and asp.net MVC -- no need to reinvent the wheel.
James S