1

I am working on a MVC 3 intranet application ( windows authentication ). I want the username of current logged in user in my controller action. I have tried lot of variation but cannot get the username . I tried using Environment.Username it worked well in debug mode but when deployed it provided the pool name rather than the username.

Controller action.

  public ActionResult Index()
        {
            LoggedUser usr = new LoggedUser();
            var aa = usr.User;
        }

LoggedUser

public class LoggedUser : Controller
    {
        public LoggedUser()
        {

        }
    }

I was following this post but was not able to make it work Getting the logged in username in ASP.NET MVC3 intranet application

Any help??

Community
  • 1
  • 1
ankur
  • 4,565
  • 14
  • 64
  • 100

3 Answers3

2

To retrieve the currently logged in username you could use the User.Identity.Name property inside your action:

public ActionResult Index()
{
    string usr = User.Identity.Name;
    ...
}

The code you have shown in your question is incorrect. You have defined a LoggedUser and are instantiating this controller inside some Index action. You are not supposed to instantiate controllers manually. That's the responsibility of the framework.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • This means that you do not have an authenticated user. Did you configure Windows authentication correctly? Did you enable it on your web server? Are you running inside a domain? Do you have a valid domain user? – Darin Dimitrov Feb 25 '13 at 10:04
  • it is just a default constructor i have just made the class for the sake of getting the user property of base class – ankur Feb 25 '13 at 10:05
  • 2
    But you cannot get the currently logged in user inside the constructor of a controller because it is not yet initialized. You should access the `User.Identity.Name` property inside a controller action. – Darin Dimitrov Feb 25 '13 at 10:07
  • it is working on local but when deployed on the prod server it is not working. Yes windows auth mode is configured in my web config,I am deploying it on IIS 6.0 is it some problem in that, since all the rest MVC 3 things are working fine. Yes I am running inside the domain and it is valid domain user.... – ankur Feb 25 '13 at 10:09
  • Create a new ASP.NET MVC 3 application in Visual Studio using the `Intranet Template`. A text file will be opened containing detailed instructions on how to setup Windows Authentication. Read it. – Darin Dimitrov Feb 25 '13 at 10:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25069/discussion-between-ankur-and-darin-dimitrov) – ankur Feb 25 '13 at 10:13
1

if you want to use a controller level variable that needs the username, you can override OnActionExecuting event where you can access the authenticated user id. You may have to check for null for the user.identity just to be cautious.

    Obj myObj;
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        myObj = new Obj(User.Identity.Name);
        base.OnActionExecuting(filterContext);
    }
civilator
  • 119
  • 4
0

whole discussions is about what is really making, sound like a "state" of permissions, in access of methods/functions, indeed sound like a bigger concept what can live in the controller like object, making a class of that type "state concept"... all discussion about if that live in model/view/controller, can be resolve by using ID/Object in the parameters interchange, but because it's access-data need, is not job for the controller, is job for the dbms and structuring data-access for memory struct of data model, job for a class in any form/model/struct of class modelling an "idea/connection/task". All discussion about model/controller is about efficiency, and efficiency is DBMS in all case.