1

In an mvc .net web application that uses forms authentication, how to know the current user identity in controllers?

Kira
  • 1,153
  • 4
  • 28
  • 63

2 Answers2

2

You could use User.Identity.Name:

[Authorize]
public ActionResult SomeAction()
{
    string currentlyLoggedInUsername = User.Identity.Name;
    ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0
  this.HttpContext.User.Identity

Note that you should also check if

  this.HttpContext.User.Identity.IsAuthenticated

to distinguish between anonymous and authenticated users.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106