C# 如何从 ASP.NET 页面获取当前登录的 Windows 帐户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16184685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:17:14  来源:igfitidea点击:

How do I get the currently loggedin Windows account from an ASP.NET page?

c#asp.netvb.netsecuritywindows-identity

提问by StackTrace

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.

我有一个使用 ASP.NET 表单身份验证的 ASP.NET 3.5 应用程序。当在页面中编辑数据时,我希望能够获取当前登录到计算机的 Windows 用户名(未登录到 ASP.NET 应用程序,而是登录到 Windows)。

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.

如果我使用Context.User.Identity.Name.Tostring(),我将获得登录到 ASP.NET 应用程序的用户名,但我需要 Windows 帐户名。

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.

此外,它仅在我从 Visual Studio 运行网站时才有效,但在部署到 IIS 后它返回NT AUTHORITY\SYSTEM

采纳答案by Zo Has

You have to set authentication mode to Windowsin your configuration & also disable anonymous users in authorization tag.

您必须Windows在您的配置中设置身份验证模式,并在授权标签中禁用匿名用户。

回答by Postback

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).

就像您希望它做的那样是正确的,但您需要先设置网络服务器,请参阅如何使用 ASP.NET 获取 Window NT 记录的用户名(设置网络服务器的第一步)。

回答by Rohit Vyas

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.

当您从 Visual Studio 运行应用程序时,您可能无法获取这些值...在 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";
    }
}

回答by Dawid Dziadkiewicz

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

要将当前登录的用户获取到 Windows 帐户,您必须使用Windows authentication而不是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

System.Security.Principal.WindowsIdentity.GetCurrent().Name.Tostring() 也仅在我从 Visual Studio 运行网站时才有效,但在部署到 IIS 后它返回 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.

它显示应用程序当前用户。当您在 Visual Studio Web 服务器上托管您的应用程序时,它会使用您的本地帐户。但是,当您使用不同的凭据登录 Web 应用程序时,它将始终显示您当前的 Windows 登录信息。

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

在您的情况下,部署到 IIS 的应用程序使用 NT AUTHORITY\SYSTEM 帐户。

回答by YaakovHatam

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);

回答by Abhishek Jaiswal

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

要在 C# 中将当前登录的用户获取到 Windows,请使用:

string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

回答by dotnetterdev

I struggled and struggled and struggled with this. One of the things is that I don't have access to IIS, that is locked down, so I couldn't change any of the server settings. I had to go with what I was capable of doing in code. When I researched it, many of the replies said, "set up IIS like this". . .well, that's great when you have access to IIS, but I didn't -- I had to work with what I could do in code. So, I ended up handling it like this:

我为此挣扎、挣扎、挣扎。一件事是我无权访问 IIS,它已被锁定,因此我无法更改任何服务器设置。我不得不使用我在代码中能够做的事情。研究了一下,很多回复说“IIS这样设置”。. .好吧,当您可以访问 IIS 时,这很好,但我没有 - 我必须使用我可以在代码中执行的操作。所以,我最终是这样处理的:

In my web config file, I added the following lines of code within the section:

在我的 Web 配置文件中,我在该部分中添加了以下代码行:

<system.webServer>
<security>
  <authentication>
    <anonymousAuthentication enabled="false" />
    <windowsAuthentication enabled="true" />
  </authentication>
</security>
</system.webServer>

Then, it returned an error on my local, which I had to go in and fix. I went to the applicationhost.config file located in the following path on my machine (yours might be different):

然后,它在我的本地返回一个错误,我必须进入并修复。我转到位于我机器上以下路径中的 applicationhost.config 文件(您的可能不同):

C:\users\"your user name"\My Documents\"yourIISInstallation"\config\applicationhost.config

C:\users\"你的用户名"\My Documents\"yourIISInstallation"\config\applicationhost.config

and I changed the following settings to "allow", which had been set to "deny":

我将以下设置更改为“允许”,该设置已设置为“拒绝”:

<section name="anonymousAuthentication" overrideModeDefault="Deny" />

changed to

变成

<section name="anonymousAuthentication" overrideModeDefault="Allow" />

and

<section name="windowsAuthentication" overrideModeDefault="Deny" />

to

<section name="windowsAuthentication" overrideModeDefault="Allow" />

in the

在里面

<sectionGroup name="authentication">

section. Before I found out this fix, I was pulling my hair out over this. I hope this helps someone. As soon as I put in the above code into the webconfig file, it worked on the intranet, it just returned errors in my local, but as soon as I added the above to my local applicationhost.config file, it started working on my local as well. Then, I called the following variable to return the name of the logged in user on windows:

部分。在我发现这个修复之前,我正在把我的头发拉出来。我希望这可以帮助别人。一旦我将上述代码放入 webconfig 文件中,它就可以在 Intranet 上运行,它只是在我的本地返回错误,但是一旦我将上述代码添加到我的本地 applicationhost.config 文件中,它就开始在我的本地运行以及。然后,我调用以下变量以返回 Windows 上登录用户的名称:

    HttpContext.Current.User.Identity.Name.ToString().Substring((HttpContext.Current.User.Identity.Name.ToString().IndexOf("\")) + 1);

Cheers!

干杯!

回答by Glen Quarless

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-integrateIn 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.

我按照以下链接中方法 1 中的说明设法解决了此问题 - https://support.microsoft.com/en-us/help/896861/you-receive-error-401-1-when-你浏览一个使用集成的网站简而言之,禁用除 Windows 身份验证之外的所有身份验证方法。在管理员账户下打开regedit,找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0,右键单击节点并转到New,然后选择Multi-String Value。输入“BackConnectionHostNames”并单击 Enter。对于值,输入您尝试设置访问权限的网站,然后单击确定。重新启动 IIS 完成后,我能够使用 HttpContext.Current.User.Identity.Name 获取当前 Windows 用户,WindowsPrincipal(this.Request.LogonUserIdentity) 也让我登录了 Windows 用户名。供参考 System.Environment .UserName 和 System.Security.Principal.WindowsIdentity.GetCurrent().Name,这两个仍然给了 IIS 用户。

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

这让我花了很长时间才弄清楚。祝你好运。IIS 是一场醒着的噩梦!