gpt4 book ai didi

c# - 如何在休息时检索请求域 URL?

转载 作者:可可西里 更新时间:2023-11-01 16:38:57 24 4
gpt4 key购买 nike

在 Global.asax.cs 文件中,我有允许跨域访问的代码。

    protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();

if( // The requesting URL == "http://theproperdomain.com"){
EnableCrossDmainAjaxCall();
}
}

private void EnableCrossDmainAjaxCall()
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}

但在我的其余项目允许跨域访问之前,它必须首先检查请求域是否等于正确的域。即:“http://theproperdomain.com”

我有这个代码:

string properdomain = HttpContext.Current.Request.Url.AbsoluteUri;  

但我不确定它是否是最好用的。
请提出你更好的想法。

编辑

我的域服务器是 http://theproperdomain.com 我的其余服务所在的地方。
我将从我的 webconfig 中获取这个域名并与当前访问的客户端进行比较。我需要这样做,因为我只想允许一个域进行“跨域访问”。

最佳答案

您可以使用 Host header of HTTP/1.1标识当前请求发送到的域。根据规范,如果请求缺少此信息,您可以返回 400 Bad Request

要访问 header ,您可以使用 HttpContext.Current.Request.Headers["HOST"]

但是,如果您想知道请求是从哪个应用程序发出的,那么您需要维护一个应用程序 secret ,该 secret 对于每个应用程序都是唯一的并且需要获得授权。只要应用程序 secret 保持安全,这就是安全的。避免使用引荐来源 header ,因为它是一个可选字段。

关于c# - 如何在休息时检索请求域 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10135784/

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