How do I get the currently loggedin Windows accoun

2019-01-11 13:15发布

I have an ASP.NET 3.5 application that uses ASP.NET forms authentication. I want to be able to get the Windows user name currently logged into the computer (NOT logged into the ASP.NET application, but into Windows) when data is edited in a page.

If I use Context.User.Identity.Name.Tostring(), I get the user name logged into the ASP.NET application, but I need the Windows account name.

System.Security.Principal.WindowsIdentity.GetCurrent().Name.Tostring()

Also, it only works when I run the website from Visual Studio, but after deploying to IIS it returns NT AUTHORITY\SYSTEM.

8条回答
Evening l夕情丶
2楼-- · 2019-01-11 13:42

To get the currently logged-in user to Windows in C#, use:

string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
查看更多
叼着烟拽天下
3楼-- · 2019-01-11 13:44

To get the currently logged in user to a Windows account you have to use Windows authentication instead of Forms authentication:

System.Security.Principal.WindowsIdentity.GetCurrent().Name.Tostring() also only works when i run the website from visual studio but after deploying to IIS it returns NT AUTHORITY\SYSTEM

It shows the application current user. When you host your application on the Visual Studio web server it uses your local account. However, when you will log in to the web application with different credentials it will always show your current Windows login.

An application deployed to IIS uses the NT AUTHORITY\SYSTEM account in your case.

查看更多
Summer. ? 凉城
4楼-- · 2019-01-11 13:49

I managed to resolve this issue by following the instructions on here in Method 1 at the following link - https://support.microsoft.com/en-us/help/896861/you-receive-error-401-1-when-you-browse-a-web-site-that-uses-integrate In brief, Disable all Authentication methods except Windows Authentication. Open regedit under an admin account, locate HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0, right click the node and go New, and select Multi-String Value. Enter "BackConnectionHostNames" and click Enter. For Value enter the website you're trying to set access on and click OK. Restart IIS Once I'd done that I was able to get the current windows user using HttpContext.Current.User.Identity.Name, WindowsPrincipal(this.Request.LogonUserIdentity) also got me the Windows username logged in. For reference System.Environment.UserName and System.Security.Principal.WindowsIdentity.GetCurrent().Name, both of these still gave the IIS user.

This has taken me ages to get to the bottom of. Good luck with it. IIS is a waking nightmare!

查看更多
Bombasti
5楼-- · 2019-01-11 13:55
string strName = HttpContext.Current.User.Identity.Name.ToString();

like you wanted it to do was correct, but you need to set up the webserver first, referring to How to Get Window NT Logged User Name Using ASP.NET (first steps setting up a web server).

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-11 13:57

Try with the below line of code:

string loggedOnUser = string.Empty;
 loggedOnUser = Request.ServerVariables.Get("AUTH_USER");

You may not be getting the values when you run the application from Visual Studio... Check it after deployed in IIS.

For getting the User name, use:

string userName = string.Empty;
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "Your Domain Name"))
{
    UserPrincipal user = new UserPrincipal(pc);
    user = UserPrincipal.FindByIdentity(pc, "User ID Will Come here");
    if (user != null)
    {
        userName = user.GivenName + " " + user.Surname;

    }
    else
    {
        //return string.Empty;
        userName = "User Not Found";
    }
}
查看更多
\"骚年 ilove
7楼-- · 2019-01-11 13:58

I use this:

System.Security.Principal.WindowsPrincipal user;
user = new WindowsPrincipal(this.Request.LogonUserIdentity);
this.Request.LogonUserIdentity.Impersonate();
user_name = user_name.Substring(user_name.LastIndexOf("\\") + 1);
查看更多
登录 后发表回答