简体   繁体   English

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

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

I have an ASP.NET 3.5 application that uses ASP.NET forms authentication. 我有一个使用ASP.NET表单身份验证的ASP.NET 3.5应用程序。 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. 我希望能够在页面中编辑数据时获取当前登录到计算机的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

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

To get the currently logged in user to a Windows account you have to use Windows authentication instead 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. 在Visual Studio Web服务器上托管应用程序时,它使用您的本地帐户。 However, when you will log in to the web application with different credentials it will always show your current Windows login. 但是,当您使用不同凭据登录Web应用程序时,它将始终显示您当前的Windows登录名。

An application deployed to IIS uses the NT AUTHORITY\\SYSTEM account in your case. 部署到IIS的应用程序在您的情况下使用NT AUTHORITY \\ SYSTEM帐户。

要使用C#将当前登录的用户导入Windows,请使用:

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

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. 其中一件事是我无法访问被锁定的IIS,因此我无法更改任何服务器设置。 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". 当我研究它时,许多回复说,“设置这样的IIS”。 . .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. .well,当你有权访问IIS时,这很棒,但我没有 - 我必须使用我在代码中可以做的事情。 So, I ended up handling it like this: 所以,我最终像这样处理它:

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. 一旦我将上面的代码放入webconfig文件中,它就在Intranet上工作,它只是在我的本地返回错误,但是一旦我将上面的内容添加到我的本地applicationhost.config文件中,它就开始在我的本地工作了同样。 Then, I called the following variable to return the name of the logged in user on windows: 然后,我调用以下变量来返回Windows上登录用户的名称:

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

Cheers! 干杯!

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);
string strName = HttpContext.Current.User.Identity.Name.ToString();

就像你想要它做的那样是正确的,但是你需要首先设置web服务器,参考如何使用ASP.NET获取Window NT Logged用户名 (设置Web服务器的第一步)。

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";
    }
}

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. 我设法通过以下链接中的方法1中的说明解决此问题 - https://support.microsoft.com/en-us/help/896861/you-receive-error-401-1-when- you-browse-a-web-site-that-uses-integrate简而言之,禁用除Windows身份验证之外的所有身份验证方法。 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. 在管理员帐户下打开注册表,找到HKEY_LOCAL_MACHINE \\ SYSTEM \\ CurrentControlSet \\ Control \\ Lsa \\ MSV1_0,右键单击该节点并转到“新建”,然后选择“多字符串值”。 Enter "BackConnectionHostNames" and click Enter. 输入“BackConnectionHostNames”并单击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. 重启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是一个醒来的噩梦!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何禁用ASP.NET成员资格提供程序的帐户? - How do I disable an account with the ASP.NET Membership Provider? 如何获取i.asp.net的Facebook帐户详细信息? - how to get facebook account details i, asp.net? 如何从 .NET 中的 Windows 服务获取当前登录的用户名? - How do I get the currently-logged username from a Windows service in .NET? 如何从 ASP.NET Core 中的私有 Azure Blob Stroage 帐户将图像返回到视图? - How do I return an image to a view from a private Azure Blob Stroage account in ASP.NET Core? 如何防止asp.net登录控件上的LoggedIn-event自动重定向? - How can I prevent automatic redirect in LoggedIn-event on the asp.net login control? 从ASP.NET页面获取Windows登录 - Get Windows Logon From Within ASP.NET Page ASP.NET中的特殊Windows帐户的页面权限 - Permission of page for Special Windows Account in ASP.NET 如何将数据从asp.net MVC控制器发布到非MVC asp.net页面? - How do I POST data from an asp.net MVC controller to a non-MVC asp.net page? 如何制作当前隐藏的图像,通过asp.net验证后可见 - How do I make an image that is currently hidden, become visible upon validation passing in asp.net 如何在ASP.NET MVC中将所选项目从一页传递到另一页 - How do I Pass Selected items from one page to another page in asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM