管理 ASP.NET 中第三方 API 密钥的多种变体

发布于 2024-08-29 09:58:02 字数 1136 浏览 4 评论 0原文

我有一个同时利用 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
}

我想知道更好的跟踪方法是什么所有这些键。我考虑了两种可能可行的方法:

  1. 在我的 web.config 中使用应用程序设置:但这里的一个问题是地图键经常根据主机名而变化(即地图键的许多变体将管理起来是一场噩梦)
  2. 有一个 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:

  1. 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)
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红墙和绿瓦 2024-09-05 09:58:02

两者都做:

因为它们不是真正的代码,所以存储域 => 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).

风蛊 2024-09-05 09:58:02

我通常将它们分成自己的文件,然后根据它们的位置部署不同的配置文件。

web.config

   <appSettings configSource="Host1.config">
        </appSettings>

c# 的

System.Configuration.ConfigurationManager.AppSettings(fieldname)

I usually split them up in to their own files and then deploy different config files based on their location.

web.config

   <appSettings configSource="Host1.config">
        </appSettings>

c#

System.Configuration.ConfigurationManager.AppSettings(fieldname)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文