How to get current login name of my Script file inside my asp.net mvc

I have a script file that calls a JSON API call, and I need to send the current login username as part of the call. I tried the following: -

$(document).ready(function () { var name = prompt("Please enter your packageid", "test"); var fullurl = 'http://localhost:8080/jw/web/json/workflow/process/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=' + HttpContext.Current.User.Identity.Name + 'packageId=' + name; $.ajax({ type: "GET", url: fullurl, dataType: "JSONP", success: function (result) { //code goes here 

But this will result in the following error: - "HttpContext" - undefined

+8
javascript asp.net-mvc asp.net-mvc-4
source share
5 answers

Your script is looking for a Javascript variable called HttpContext . Your code should be

 @HttpContext.Current.User.Identity.Name 

in razor

so javascript becomes

  var fullurl = 'http://localhost:8080/jw/web/json/workflow/process/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=@HttpContext.Current.User.Identity.Name&packageId=' + name; 

You also lack the & between the username and packageId if you intended them as separate variables

Edit : based on your comment, and this is inside the js file (which I think I skipped in OP)

Two options:

  • Maintaining the username inside the variable on the page that invokes the script file. Like this:

Page

 <script> var usrName = "@HttpContext.Current.User.Identity.Name"; </script> 

Js file

 ....&loginAs='+ usrName + '&packageId=' + name; 

The second option is to not include the username at all and just get it from Action. This is only an option if the page you submit is in the same application.

+14
source share

You can only use User.Identity.Name obtained from System.Web.WebPages.WebPageRenderingBase (this means that the code must be in the View file). You cannot access server code directly in script -files.

  $(document).ready(function () { var name = prompt("Please enter your packageid", "test"); var fullurl = 'http://localhost:8080/jw/web/json/workflow/process/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=@User.Identity.Name&packageId=' + name; $.ajax({ type: "GET", url: fullurl, dataType: "JSONP", success: function (result) { //code goes here 
+1
source share

You will need to set the javascript variable in your view (.cshtml) using a razor and then use it in the script file (.js)

So, in your opinion:

 <script type="text/javascript"> var userName = '@HttpContext.Current.User.Identity.Name'; </script> 

and then your script file

  var fullurl = '....loginAs=' + userName + 'packageId=' + name; 
+1
source share
 $(document).ready(function () { var name = prompt("Please enter your packageid", "test"); var fullurl = 'http://localhost:8080/jw/web/json/workflow/process/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=' + <%= HttpContext.Current.User.Identity.Name %> + 'packageId=' + name; $.ajax({ type: "GET", url: fullurl, dataType: "JSONP", success: function (result) { 

the above code should work, on the javascript front, you will not have access to the HTTPContent object, which is available only on the server, but by placing it in the server code, you must have access to it.

or you can have a hidden control with a value set from the server

 <input type='hidden' id='uid' runat='server'> 

server code will look like

 uid.value = HTTPContext.Current.User.Identity.Name; 
+1
source share

var usrName = '<%=@HttpContext.Current.User.Identity.Name%>'

+1
source share

All Articles