管理 ASP.NET 中第三方 API 密钥的多种变体
我有一个同时利用 Google Analytics 和 Google 地图的网站。这两项服务都有 API 密钥,需要在我们网站的代码中进行管理。对于 Google Analytics,我们有两个帐户,一个是真实帐户,另一个是临时帐户,用于在午餐前测试跟踪。对于 Google 地图,我们实际上需要为每个主机名提供一个唯一的 API 密钥。我们有几个临时域和本地主机名,因此我们的地图代码对 API 密钥有相当多的切换。
现在,我正在使用 Request.Url.Host
上的开关在各个页面的 C# 代码隐藏中管理我的密钥:
// for analytics in file1
switch (Request.Url.Host) {
case "staging": ltlUACode.Text = "stageKey"; break;
case "client.stage.com": ltlUACode.Text = "stageKey"; break;
case "www.livesite.com": ltlUACode.Text = "liveKey"; break;
case "livesite.com": ltlUACode.Text = "liveKey"; break;
}
// for maps in file2
switch(Request.Url.Host) {
case "staging": GoogleMapsKey = "uniqueKey1"; break;
case "client.stage.com": GoogleMapsKey = "uniqueKey2"; break;
case "www.livesite.com": GoogleMapsKey = "uniqueKey3"; break;
// etc
}
我想知道更好的跟踪方法是什么所有这些键。我考虑了两种可能可行的方法:
- 在我的
web.config
中使用应用程序设置:但这里的一个问题是地图键经常根据主机名而变化(即地图键的许多变体将管理起来是一场噩梦) - 有一个 C# 类来根据主机名处理所有这些键:我可以编写一个静态类,该类具有分析键和映射键的属性并基于
Request.Url.Host
我可以将属性设置为适当的值。这个方法本质上是我的 switch 方法,包装在一个帮助器类中。
I have a site that leverages both Google Analytics and Google Maps. Both of these services have API keys that need to be managed in our site's code. For Google Analytics, we have two accounts, a live account and staging account to test tracking prior to lunch. For Google Maps we actually need a unique API key for every single host name. We have several staging domains and local hostnames so our maps code has quite a bit of toggling for API keys.
Right now I am managing my keys in a C# code-behind for the respective pages using a switch on the Request.Url.Host
:
// for analytics in file1
switch (Request.Url.Host) {
case "staging": ltlUACode.Text = "stageKey"; break;
case "client.stage.com": ltlUACode.Text = "stageKey"; break;
case "www.livesite.com": ltlUACode.Text = "liveKey"; break;
case "livesite.com": ltlUACode.Text = "liveKey"; break;
}
// for maps in file2
switch(Request.Url.Host) {
case "staging": GoogleMapsKey = "uniqueKey1"; break;
case "client.stage.com": GoogleMapsKey = "uniqueKey2"; break;
case "www.livesite.com": GoogleMapsKey = "uniqueKey3"; break;
// etc
}
I'd like to know what would a better method be to keep track of all of these keys. I considered two possible things that could work:
- Using app settings in my
web.config
: one issue here though is that the maps keys change often based on the hostnames (i.e. the many variants for the maps key would be a nightmare to manage) - Having a C# class to handle all of these keys based on the hostname: I could write a static class that has properties for the analytics key and the maps key and based on the
Request.Url.Host
I could set the properties to their appropriate values. This method is essentially my switch method wrapped up into a helper class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两者都做:
因为它们不是真正的代码,所以存储域 => Web 配置中的 ApiKey 对。对于大量类似的 web.config 设置,创建自定义部分和处理程序会很有帮助。这里额外工作的好处是,您还可以扩展自定义部分处理程序的功能,以根据环境返回正确的值。
创建一种自定义部分类型,然后为每个 api 键(地图、分析等)添加一个(该类型的)部分。
Do Both:
Since they're not really code, store the Domain => ApiKey pairs in the web config. For large blocks of similar web.config settings, it's helpful to create custom sections and handlers. The bonus of the extra work here is that you can extend the functionality of the custom section handlers to return the correct values based on the environment, too.
Create one custom section type, then add one section (of that type) for each api key (maps, analytics, etc).
我通常将它们分成自己的文件,然后根据它们的位置部署不同的配置文件。
web.config
c# 的
I usually split them up in to their own files and then deploy different config files based on their location.
web.config
c#