gpt4 book ai didi

asp.net-mvc - 仅允许来自同一网络服务器的请求的操作

转载 作者:行者123 更新时间:2023-12-04 18:08:28 25 4
gpt4 key购买 nike

我有一个 MVC Controller ,它公开了 Initialise Action .托管在同一 IIS 上的其他虚拟 Web 应用程序将需要访问此 Action.
出于安全原因,只有来自同一 Web 服务器(托管 MVC 应用程序)的请求才需要被授予访问此 Iniliase 方法的权限。

有人可以帮助如何实现这一目标吗?我们无法使用 localhost 进行验证,因为此应用程序将托管在不支持 locahost 请求的 Azure 中。

最佳答案

我的回答是关于限制服务器端请求。

调用Initialise的网站需要向 http://www.example.com/controller/Initialise 提出请求而不是 http://localhost/controller/Initialise (当然,用您的域和 Controller 名称替换 www.example.comcontroller)。

HttpRequest.IsLocal应该在您的 Controller 操作中检查:

if (!Request.IsLocal)
{
throw new SecurityException();
}

这将拒绝任何不是来自本地主机的请求。这种方法假设调用站点和请求站点共享相同的 IP 地址 - 文档说明这应该有效:

The IsLocal property returns true if the IP address of the request originator is 127.0.0.1 or if the IP address of the request is the same as the server's IP address.



用于限制客户端请求谷歌“csrf 缓解”。

关于asp.net-mvc - 仅允许来自同一网络服务器的请求的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20593972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com