12

I have a script file that makes a call to JSON API, 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 it will raise the following error:- 'HttpContext' is undefined

6 Answers 6

20

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

@HttpContext.Current.User.Identity.Name

in Razor

so the javascript becomes

 var fullurl = 'http://localhost:8080/jw/web/json/workflow/process/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&[email protected]&packageId=' + name;

You're also missing and & between the user name and packageId if you intended for them to be separate variables

Edit: based on your comment and this being inside of a js file (which I guess I missed in the OP)

Two options:

  1. Is to hold the username inside a variable on the page calling the script file. Like this:

Page

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

JS file

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

Option Two is to not include the username at all and just get it from the Action. This is only an option if the page you're posting to is on the same app

Sign up to request clarification or add additional context in comments.

5 Comments

you mean i should write the javascript inside the razor and not inside the Scripts folder.?.
@johnG, yes, that's what he means.
then there is no way to get the username from inside my .js file?
@Eonasdan but how i can pass the variable to the script,, i do not think it will work if i write <script> var usrName = "@HttpContext.Current.User.Identity.Name"; </script> in my razor view. can you post a complete code -if possible-
did you try it? because I do that all the time. As long as var usrName comes first - before the .js file it will work
3
$(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 HTTPContent object which is available only on the server, but putting them in a server code you should be able to access the same.

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

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

server code would look like

uid.value = HTTPContext.Current.User.Identity.Name;

12 Comments

i strongly suggest you relook at passing user id as a parameter, its not a suggested practice.
why but how i can integrate with other system instead ?, as i am sending a API request to return JSON from another application. so i have to send the username in my request.
at first please let me know the answer i gave works for you :) , second you should look at passing a token instead of exposing your userid, where the token should be generated on your server side.
thanks for your alert, but the web service i am integrating with expect a loginAs parameter to make the call, and it does not have a parameter for the token. baring in mind that i do not own the system i am integrating with.. any suggestions.?
is there a possibility for you to do an AJAX call to your server and your server gets the required info for you ? by that you would not be exposing your user id to the external world. using a WebRequest Class get WebResponse ?
|
1

You can use just User.Identity.Name that's derived from System.Web.WebPages.WebPageRenderingBase (that means the code should be in a View-file). You can't access server-side 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&[email protected]&packageId=' + name;
        $.ajax({
            type: "GET",
            url: fullurl,
            dataType: "JSONP",
            success: function (result) {
//code goes here

7 Comments

this will raise "Microsoft JScript runtime error: 'User' is undefined" error.
as I suggest you need to make Razor render this into your javascript instead of asking javascript for it
Is the script in a view or a js-file?
in a separate script file under the Scripts folder, and i am just calling this script file in my razor.
@johnG Then you should put this code in a script-tag in say your _layout.cshtml file or other view-file.
|
1

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

So in your view:

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

and then your script file

   var fullurl = '....loginAs=' + userName  + 'packageId=' + name;

2 Comments

thanks for the reply, but what you mean by "to set a javascript variable in your view". i did not perform such thing before?
updated answer, Razor will not be interpreted by compiler in your .js files but it will in your .cshtml files. Thus have the above script in your view and then use userName in your script
1
    @User.Identity.Name

You can directly access user identity in script section by prefixing @

    @User.FindFirst(ClaimTypes.NameIdentifier).Value

also a good option.

Comments

0

var usrName = '<%[email protected]%>'

3 Comments

Please, also provide a minimum explanation.
I think that is self explanatory:)
This answer is already provided by Eonasdan. He explains that this makes sense only in Razor (cshtml) only and how to pass it in a script outside of the view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.