How to get the current Windows user with ASP.NET Core RC2 MVC6 and IIS7

前端 未结 4 1350
难免孤独
难免孤独 2021-01-17 10:48

I have an intranet site built in MVC6 using ASP.NET Core RC2. I want to get the Windows username of the person accessing the intranet site.

So if Jim goes to the in

4条回答
  •  猫巷女王i
    2021-01-17 11:27

    For anyone else using Windows Authentication on an Intranet app, who just wants to be able to retrieve info about a user, it's simpler that you might think.

    In your controller, this line will grab the user name of the current user in DOMAIN\username form.

    var userId = this.User.Identity.Name;
    

    I spent some time looking through the other answers before realizing that most of them assumed I was implementing Individual accounts to be authenticated against AD, not out of the box Windows Authentication.

提交回复
热议问题