最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the user's username in my intranet web application in a windows network - Stack Overflow

programmeradmin2浏览0评论

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.

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)

Users have XP and 7 machines and IE7,8.

BTW, I found this article but 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.

And if using Chrome, it displays <%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

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.

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)

Users have XP and 7 machines and IE7,8.

BTW, I found this article but 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.

And if using Chrome, it displays <%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

Share Improve this question edited May 23, 2017 at 12:02 CommunityBot 11 silver badge asked Jan 16, 2013 at 13:32 AmarundoAmarundo 2,39716 gold badges52 silver badges70 bronze badges 1
  • 1 This is old but in case someone es here: <%HttpContext.Current.User.Identity.Name%> is syntax that is evaluated on the server before the page is rendered. Obviously, this will only work if you are running in . – Charles Driver Jr. Commented Oct 17, 2017 at 12:53
Add a ment  | 

2 Answers 2

Reset to default 1

Using only javascript on a none IIS server then it isn't possible.

However:

If you are using IIS then you can use the following 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:

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

capturing the windows domain username and machine name, I think we can't

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论