javascript 如何在我的 Windows 网络中的 Intranet Web 应用程序中获取用户的用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14359648/
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
How to get the user's username in my intranet web application in a windows network
提问by Amarundo
I have a simple HTML page internally which just displays a form and asks the user to fill it out. I want to automatically capture the windows domain username and machine name to submit it together with the data collected in the form.
我在内部有一个简单的 HTML 页面,它只显示一个表单并要求用户填写它。我想自动捕获windows域用户名和机器名,连同表单中收集的数据一起提交。
Can I do that on the client side? HTML? JavaScript? Or am I forced to do it on the server side (which I don't know how to do... yet)
我可以在客户端这样做吗?HTML?JavaScript?还是我被迫在服务器端执行此操作(我不知道该怎么做……但)
Users have XP and 7 machines and IE7,8.
用户有XP和7机器和IE7、8。
BTW, I found this articlebut I have to confess I don't know where in the page you would enter that "code".
顺便说一句,我找到了这篇文章,但我必须承认我不知道您会在页面中的哪个位置输入该“代码”。
Here's the code I'm using that is not working. It calls the JavaScript function, and displays "Username="
and in the next line "Done."
, but doesn't display the username.
这是我正在使用的代码不起作用。它调用 JavaScript 函数,并"Username="
在下一行显示和"Done."
,但不显示用户名。
And if using Chrome, it displays <%HttpContext.Current.User.Identity.Name%>
.
如果使用 Chrome,它会显示<%HttpContext.Current.User.Identity.Name%>
.
<html lang="en">
<head>
<script>
function get_user_Name() {
var username = '<%HttpContext.Current.User.Identity.Name%>';
document.write("Username=");
document.write(username);
document.write(" <br> Done.");
}
</script>
<meta charset="utf-8" />
<title>Training and Development Site</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
</head>
<body>
<a href="add-number-to-dnc.html">Add Phone Number to Dialer Do Not Call list</a>
<br><br>
<button type="button" onclick="get_user_Name()">Display User Name</button>
</body>
</html>
Also - I'm using WebMatrix / IIS7 Express
另外 -我正在使用 WebMatrix / IIS7 Express
回答by Ryan McDonough
Using only javascript on a none IIS server then it isn't possible.
在非 IIS 服务器上仅使用 javascript 那么这是不可能的。
However:
然而:
If you are using IIS then you can use the following JS:
如果您使用的是 IIS,那么您可以使用以下 JS:
<script language="javascript">
var username = '<%HttpContext.Current.User.Identity.Name %>';
</script>
Assuming you have turned Windows Authentication on in IIS for your site, then C# side:
假设您已在 IIS 中为您的站点打开 Windows 身份验证,那么 C# 端:
C#
C#
public string user_Name
{
get
{
string x = Page.User.Identity.Name;
x = x.Replace("YOURDOMAIN\", "");
return x;
}
}
The x = x.Replace("DOMAIN\\", "");
strips out the DOMAIN sectionof the user acccount e.g NISSAN\rmcdonough
该 x = x.Replace("DOMAIN\\", "");
sectionof用户acccount如NISSAN \ rmcdonough出DOMAIN条
回答by dangmh
capturing the windows domain username and machine name, I think we can't
捕获windows域用户名和机器名,我想我们不能