How do I get the current user in an MVC Application? [duplicate]
Asked Answered
U

2

0

Possible Duplicate:
How to get current user in Asp.Net MVC

I've tried a bunch of different things, I know I've done it before when I used Webforms, but I can't seem to figure out how to do it in MVC.

Membership doesn't work User gives me an IPrincipal or something User.Identity gives me the same type of variable...

Anyone know? Is there any GetCurrentUser() method or something similar?

Uroscopy answered 25/6, 2009 at 17:27 Comment(6)
duplicate of #263986Pahari
wow that's hilarious, I always check the list of recommended questions after I type in the title and the title is almost the exact same... my apologiesUroscopy
Just tried Page.User.Identity as well as Page.CurrentUser. Both of them give me an error saying System.Web.UI.Page does not contain a definition for "CurrentUser" or "User.Identity" in the case of the first oneUroscopy
check do you have any class named "Page" within your projectAssessor
No I don't. The error says it's referencing System.Web.UI.PageUroscopy
I'm trying to access it from within a Control, does that matter?Uroscopy
U
10

See Mehrdad's answer here: System.Web.UI.Page won't let me access CurrentUser or User.Identity from a Controller in ASP.Net-MVC

He explains that you can to use

User.Identity // in the controller
HttpContext.User.Identity // in the controller
System.Web.HttpContext.Current.User.Identity // anywhere
Uroscopy answered 26/6, 2009 at 6:46 Comment(0)
A
0
Page.User.Identity

Page should be referencing System.Web.UI.Page

check do you have any class named "Page" within your project

Assessor answered 25/6, 2009 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.