admin管理员组

文章数量:1384804

I would like to get the current logged in username and display it my frontend. Currently I have a function called GetCurrentUser() that gets called when a button is clicked.

<button type="submit" onclick="GetCurrentUser()" style="margin-left: 15px;margin-top:10px; margin-bottom: 5px;background-color: black; "value="Submit">Save Selections</button><br>

function GetCurrentUser() {
var usrName ="@HttpContext.Current.User.Identity.Name.ToString()";
//var usrName = '<%HttpContext.Current.User.Identity.Name %>';
//document.getElementById("UserName").innerHTML = usrName;
console.log(usrName);}

I get the follwoingoutput in my console log--> @HttpContext.Current.User.Identity.Name

I would like to get the current logged in username and display it my frontend. Currently I have a function called GetCurrentUser() that gets called when a button is clicked.

<button type="submit" onclick="GetCurrentUser()" style="margin-left: 15px;margin-top:10px; margin-bottom: 5px;background-color: black; "value="Submit">Save Selections</button><br>

function GetCurrentUser() {
var usrName ="@HttpContext.Current.User.Identity.Name.ToString()";
//var usrName = '<%HttpContext.Current.User.Identity.Name %>';
//document.getElementById("UserName").innerHTML = usrName;
console.log(usrName);}

I get the follwoingoutput in my console log--> @HttpContext.Current.User.Identity.Name

Share Improve this question edited Apr 12, 2018 at 14:30 Taha Paksu 15.6k2 gold badges49 silver badges82 bronze badges asked Apr 12, 2018 at 14:28 rabyusbeefrabyusbeef 111 gold badge1 silver badge6 bronze badges 7
  • Did you try <%=? – Taha Paksu Commented Apr 12, 2018 at 14:30
  • like this var usrName = <%HttpContext.Current.User.Identity.Name %>; ? @taha Paksu – rabyusbeef Commented Apr 12, 2018 at 14:33
  • var usrName = "<%=HttpContext.Current.User.Identity.Name %>"; – Taha Paksu Commented Apr 12, 2018 at 14:33
  • This is what I see when I tried it in my console log GetCurrentUserName.js:5 <%HttpContext.Current.User.Identity.Name %> – rabyusbeef Commented Apr 12, 2018 at 14:34
  • Add an = after the <%. – Taha Paksu Commented Apr 12, 2018 at 14:35
 |  Show 2 more ments

2 Answers 2

Reset to default 2

If you are seeing the literal output of "HttpContext.Current.User.Identity.Name " then your JS function is generated client side after you have lost server context.

Couple options for you:

  1. Call back to your controller via ajax to get the username

  2. Store the username in a read only field on page load (kinda like setting a form value) and retrieve the value via jquery or js on function call

  3. Assign the username on page load to a global js element and just use that element in your function.

Here is an example of 2 and 3. I don't think you should worry about #1 until you fully understand why your issue is happening in the first place:

    <div class="btn btn-info" onclick="GetCurrentUser()" style="margin-left: 15px;margin-top:10px; margin-bottom: 5px;background-color: black; " value="Submit">Save Selections</div><br>
    <input type="hidden" name="method2" id="method2" value="@System.Security.Principal.WindowsIdentity.GetCurrent().Name">
    @section scripts {
        <script>
            var globalSettingMethod = '@System.Security.Principal.WindowsIdentity.GetCurrent().Name';
            function GetCurrentUser() {
                alert(globalSettingMethod);
                alert($('#method2').val());
            }
        </script>
    }

I could get the user logged in by passing the script at the end of the document.

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Example.SiteMaster" %>

<!DOCTYPE html>
<html xmlns="http://www.w3/1999/xhtml">
<head runat="server">
</head>
<body>
    <form runat="server">
    </form>
    <script>
        var loggedUser = "<%: HttpContext.Current.User.Identity.Name %>";
    </script>
</body>
</html>

I hope you find it useful.

本文标签: javascriptGet Current Logged Username in my appStack Overflow