简体   繁体   English

在服务器端ASP.NET Core上使用Google Maps受限API密钥

[英]Use Google Maps restricted API key on server side ASP.NET Core

I have an application in production that uses lots of Google Maps services and therefore I've created a restricted API key with HTTP referrers restriction. 我在生产中有一个使用大量Google Maps服务的应用程序,因此我创建了一个具有HTTP referrers限制的受限API密钥。 The restricted key works fine sending requests from client (loading maps, etc.), but I have trouble using it server side as I send some of the requests from ASP.NET Core controllers. 受限键可以很好地发送来自客户端的请求(加载地图等),但是由于我从ASP.NET Core控制器发送了一些请求,因此在服务器端使用它时遇到了麻烦。 Before I send the request I set the Referer header, but still the request gets denied: 在发送请求之前,我设置了Referer标头,但仍然拒绝了该请求:

var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Referrer = new Uri(_configuration.GetValue<string>("Google:RequestReferrer")); //referrer string is set in the appsettings.json file
var client = _httpClientFactory.CreateClient();
var response = await client.SendAsync(request);

Is it possible to use the same restricted key on client side and server side by somehow setting the correct request headers or should I create a new API key restricted with IP for the server side usage. 是否可以通过某种方式设置正确的请求标头在客户端和服务器端使用相同的受限密钥,还是应该为服务器端使用创建一个受IP限制的新API密钥。

Thank you. 谢谢。

No. If you're going to make 2 different types of requests (client side and server side) you should have a separate API key restricted with HTTP referrers for the client side, and an API key restricted with IP addresses for the server side. 否。如果要进行2种不同类型的请求(客户端和服务器端),则应为客户端使用一个单独的API密钥(受HTTP Referrers限制),并为API密钥一个受IP地址限制的API密钥。

You may also visit the API key Best Practices here for more details. 您还可以在此处访问API密钥最佳做法以了解更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM