How to get the current login user name in my Script file inside my asp.net mvc

后端 未结 6 1123
谎友^
谎友^ 2020-12-10 14:02

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 :-

    $(documen         


        
相关标签:
6条回答
  • 2020-12-10 14:29

    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&loginAs=@HttpContext.Current.User.Identity.Name&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

    0 讨论(0)
  • 2020-12-10 14:32
    $(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;
    
    0 讨论(0)
  • 2020-12-10 14:45

    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&loginAs=@User.Identity.Name&packageId=' + name;
            $.ajax({
                type: "GET",
                url: fullurl,
                dataType: "JSONP",
                success: function (result) {
    //code goes here
    
    0 讨论(0)
  • 2020-12-10 14:46
        @User.Identity.Name
    

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

        @User.FindFirst(ClaimTypes.NameIdentifier).Value
    

    also a good option.

    0 讨论(0)
  • 2020-12-10 14:52

    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;
    
    0 讨论(0)
  • 2020-12-10 14:54

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

    0 讨论(0)
提交回复
热议问题