6

In my MVC application I want to render a table in a cshtml file, if the current log in user is some x person. I am using windows authentication and I have made the following changes in web.config file.

<authentication mode="Windows">
      </authentication>

And in my controller when I am trying to access the current user name I am not getting any user name. I am trying the following:

ViewBag.LogInUserName = Request.RequestContext.HttpContext.User.Identity.Name;

This above line was working before. But I don't know whats wrong now. Also I have hosted my application on IIS now.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Jash
  • 942
  • 4
  • 17
  • 40

3 Answers3

13

Take a look at the web project's properties, in particular:

  1. Anonymous Authentication - Set to "Disabled"
  2. Windows Authentication - Set to "Enabled"

By default these are set to the opposite of what you're probably looking for.

Web project properties

(Image sourced from MSDN)

Bern
  • 7,808
  • 5
  • 37
  • 47
12

You need to put the [Authorize] attribute on your controller.

You can use User.Identity.Name in your controllers.

[Authorize]
public class YourController : Controller
{

    public ActionResult SomeAction()
    {
        var userName = User.Identity.Name;
    }
}
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • Thanks for the help. Can you just show how I can achieve that. – Jash Jan 23 '12 at 07:58
  • I have just change my web project properties to use Asp.Net development server. And now I am able to retrieve the current log in user name. So problem is when I am hosting my web project on IIS. Any idea? – Jash Jan 23 '12 at 08:08
  • @Jash: The above code should work fine with windows authentication (even if anonymous access is turned on). Did you try it in IIS? – jgauffin Jan 23 '12 at 08:11
  • Yup it is working iwth windows authentication. Nut it is not working with IIS – Jash Jan 23 '12 at 08:13
  • User.Identity.Name returns string.Empty – Jash Jan 23 '12 at 10:27
  • Have you any solution for this. – Jash Jan 23 '12 at 10:44
  • I've never got the same problem as you and I use windows auth all the time doing like I described above. – jgauffin Jan 23 '12 at 10:54
  • Is your site hosted on IIS. does I need to add anything in web.config – Jash Jan 23 '12 at 11:16
  • 1
    HI i jsut added the Authorize attribute but after that it is throwin 4o1 unathorized error. – Jash Jan 24 '12 at 06:58
  • You have probably not installed the Windows Authentication feature in IIS. – jgauffin Aug 23 '16 at 06:29
  • 1
    if you remove [Authorize] then the 401.0 - Unauthorized goes away and you can get the user name like System.Security.Principal.WindowsIdentity.GetCurrent().Name – Saad A Oct 06 '17 at 16:22
1

A little bit late, but this may serve others in the future.

I had the same problem once after deploying my site to a new IIS server, and the anonymous authentication was enabled, so make sure that anonymous authentication is disabled and it should work.

Mhd. Yasseen
  • 1,027
  • 3
  • 16
  • 27