如何在我的脚本文件获取当前登录的用户名我的asp.net mvc的内 [英] How to get the current login user name in my Script file inside my asp.net mvc
问题描述
我有一个脚本文件,使以JSON API的调用,我需要发送当前登录用户名作为调用的一部分。我试过如下: -
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
但它会引发以下错误: - 的HttpContext'未定义
推荐答案
您的脚本正在寻找的的JavaScript 变量名为的HttpContext
您code应
Your script is looking for a Javascript variable called HttpContext
Your code should be
@HttpContext.Current.User.Identity.Name
在剃刀
所以JavaScript是
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;
你还缺少和&放大器;用户名和包标识,如果你打算为他们是独立变量之间
修改:根据您的意见,这是一个js文件里(我想我错过了在OP)
Edit: based on your comment and this being inside of a js file (which I guess I missed in the OP)
两个选项:
- 是持有调用脚本文件页面上的变量中的用户名。像这样的:
页
<script>
var usrName = "@HttpContext.Current.User.Identity.Name";
</script>
JS文件
....&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
这篇关于如何在我的脚本文件获取当前登录的用户名我的asp.net mvc的内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!