将可配置的appInsights键传递给javascript(反应/角度)应用程序
am将Azure AppInsights与一些ASP.NET Core 2.1应用程序结合使用。我将插装键保留在appsettings.json中(稍后由Azure应用程序服务中的应用程序配置覆盖)。显然,密钥可能因环境而异(在dev/qa/prod环境中不同)。该密钥由后端的AppInsights SDK使用 我的问题是:如何将该密钥传递给JavaScript/React应用程序(与JavaScript应用程序Insisghts sdk一起使用)?我没有使用Razor(我也不想使用Razor)生成主页面-它只是静态html页面:将可配置的appInsights键传递给javascript(反应/角度)应用程序,javascript,asp.net-core,azure-application-insights,Javascript,Asp.net Core,Azure Application Insights,am将Azure AppInsights与一些ASP.NET Core 2.1应用程序结合使用。我将插装键保留在appsettings.json中(稍后由Azure应用程序服务中的应用程序配置覆盖)。显然,密钥可能因环境而异(在dev/qa/prod环境中不同)。该密钥由后端的AppInsights SDK使用 我的问题是:如何将该密钥传递给JavaScript/React应用程序(与JavaScript应用程序Insisghts sdk一起使用)?我没有使用Razor(我也不想使用Razor)
<!DOCTYPE html>
<html>
<head>
<title>To Do List</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script type="text/javascript">
var appInsights=window.appInsights||function(a){
function b(a){c[a]=function(){var b=arguments;c.queue.push(function(){c[a].apply(c,b)})}}var c={config:a},d=document,e=window;setTimeout(function(){var b=d.createElement("script");b.src=a.url||"https://az416426.vo.msecnd.net/scripts/a/ai.0.js",d.getElementsByTagName("script")[0].parentNode.appendChild(b)});try{c.cookie=d.cookie}catch(a){}c.queue=[];for(var f=["Event","Exception","Metric","PageView","Trace","Dependency"];f.length;)b("track"+f.pop());if(b("setAuthenticatedUserContext"),b("clearAuthenticatedUserContext"),b("startTrackEvent"),b("stopTrackEvent"),b("startTrackPage"),b("stopTrackPage"),b("flush"),!a.disableExceptionTracking){f="onerror",b("_"+f);var g=e[f];e[f]=function(a,b,d,e,h){var i=g&&g(a,b,d,e,h);return!0!==i&&c["_"+f](a,b,d,e,h),i}}return c
}({
instrumentationKey: "how_to_populate_that_key?"
});
window.appInsights=appInsights,appInsights.queue&&0===appInsights.queue.length&&appInsights.trackPageView();
</script>
</head>
<body>
<div id="root"></div>
</body>
</html>
待办事项清单
var appInsights=window.appInsights | |函数(a){
函数b(a){c[a]=function(){var b=arguments;c.queue.push(function(){c[a].apply(c,b)}}var c={config:a},d=document,e=window;setTimeout(函数(){var b=d.createElement(“脚本”);b.src=a.url |”https://az416426.vo.msecnd.net/scripts/a/ai.0.js,d.getElementsByTagName(“脚本”)[0].parentNode.appendChild(b)});请尝试{c.cookie=d.cookie}catch(a){}c.queue=[];for(var f=[“事件”、“异常”、“度量”、“页面视图”、“跟踪”、“依赖项”];f.length;)b(“跟踪”+f.pop());if(b(“setAuthenticatedUserContext”)、b(“clearAuthenticatedUserContext”)、b(“startTrackEvent”)、b(“startTrackPage”)、b(“stopTrackPage”)、b(“刷新”)、!a.disableExceptionTracking){f=“onerror”、b(“Uf”);var g=e[f]、e[f]=函数(a、b、d、e、h){var i=g&&g(a,b,d,e,h);return!0!==i&&c[“”+f](a,b,d,e,h),i}}返回c
}({
instrumentationKey:“如何填充该密钥?”
});
window.appInsights=appInsights,appInsights.queue&&0==appInsights.queue.length&&appInsights.trackPageView();
将密钥从后端动态传递到前端的正确解决方案是什么?这应该由某个端点公开吗?或者我应该在后端(包含该键)动态生成某种脚本,并引用它index.html吗
我只是不想在JS代码库中硬编码密钥。在加载应用程序之前调用API并加载指令插入密钥,或者这里是从后端加载的另一种方法
<script type="text/javascript">
// Standard Application Insights web page script:
var appInsights = window.appInsights || function(config){ ...
// Modify this part:
}({instrumentationKey:
// Generate from server property:
"@Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey"
}
)
</script>
//标准Application Insights网页脚本:
var appInsights=window.appInsights | |函数(配置){。。。
//修改此部分:
}({instrumentationKey:
//从服务器属性生成:
“@Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey”
}
)
有关更多信息,请浏览此