Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows authentication in MVC

I want to check the login name of a user using windows authentication. I have this as a constructor of my controller:

public class HomeController : BaseController
{
    public string UserIdentityName;

    public HomeController()
    {
        UserIdentityName = System.Web.HttpContext.Current.User.Identity.Name;// HttpContext.Current.User.Identity.Name;
    }
}

But UserIdentityName returns empty string...

I also have this at my web.config:

<authentication mode="Windows" />   

any idea?

like image 790
Guy Z Avatar asked Nov 30 '22 05:11

Guy Z


1 Answers

In solution explorer pane select the Web Project and hit F4. (not right click+properties, thats different)

In properties Pane set:
Windows Authentication: Enable
Anonymous Authentication: Disabled

In Web.config: <authentication mode="Windows"></authentication>

To get Username:

HttpContext.Current.User.Identity.Name OR User.Identity.Name

Run your project, Happy Days!

like image 74
Moji Avatar answered Dec 06 '22 05:12

Moji