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
2 Answers
Reset to default 2If 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:
Call back to your controller via ajax to get the username
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
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
版权声明:本文标题:javascript - Get Current Logged Username in my app - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744514615a2610084.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论