gpt4 book ai didi

c# - 支持aspx页面中的OPTIONS请求头

转载 作者:太空狗 更新时间:2023-10-29 22:12:48 25 4
gpt4 key购买 nike

我正在维护接受表单发布的服务,并在添加对 CORS 请求的支持时遇到了 Firefox 3.6 中的问题,它发送带有 OPTIONS 请求 header 的预检请求。

我在使用通用 http 处理程序页面添加必要的 Access-Control-Allow-Origin 响应 header 时没有遇到任何问题,但我在使用完整的 aspx 页面时遇到了困难。它绝对没有达到 Page_Load,我不知道它卡在了页面生命周期的哪个位置。

有人有什么想法吗?

谢谢!

最佳答案

您可以使用 HttpModuleHttpHandler

我认为其中一些来自某个地方的文章,而其他部分是内部开发的...所以如果其中一些来自其他地方,我提前为没有给予应有的信任而道歉:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YourNamespaceHere
{
using System;
using System.Web;
using System.Collections;

public class CrossOriginModule : IHttpModule {
public String ModuleName {
get { return "CrossOriginModule"; }
}

public void Init(HttpApplication application) {
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
}

private void Application_BeginRequest(Object source, EventArgs e) {
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
CrossOriginHandler.SetAllowCrossSiteRequestOrigin(context);
}

public void Dispose()
{
}
}

public class CrossOriginHandler : IHttpHandler
{
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}

public void ProcessRequest(HttpContext context)
{
//Clear the response (just in case)
ClearResponse(context);

//Checking the method
switch (context.Request.HttpMethod.ToUpper())
{
//Cross-Origin preflight request
case "OPTIONS":
//Set allowed method and headers
SetAllowCrossSiteRequestHeaders(context);
//Set allowed origin
//This happens for us with our module:
SetAllowCrossSiteRequestOrigin(context);
//End
context.Response.End();
break;

default:
context.Response.Headers.Add("Allow", "OPTIONS");
context.Response.StatusCode = 405;
break;
}

context.ApplicationInstance.CompleteRequest();
}
#endregion

#region Methods
protected void ClearResponse(HttpContext context)
{
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.Clear();
}

protected void SetNoCacheHeaders(HttpContext context)
{
context.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
context.Response.Cache.SetValidUntilExpires(false);
context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetNoStore();
}
#endregion

public static void SetAllowCrossSiteRequestHeaders(HttpContext context)
{
string requestMethod = context.Request.Headers["Access-Control-Request-Method"];

context.Response.AppendHeader("Access-Control-Allow-Methods", "GET,POST");

//We allow any custom headers
string requestHeaders = context.Request.Headers["Access-Control-Request-Headers"];
if (!String.IsNullOrEmpty(requestHeaders))
context.Response.AppendHeader("Access-Control-Allow-Headers", requestHeaders);
}

public static void SetAllowCrossSiteRequestOrigin(HttpContext context)
{
string origin = context.Request.Headers["Origin"];
if (!String.IsNullOrEmpty(origin))
context.Response.AppendHeader("Access-Control-Allow-Origin", origin);
else
//This is necessary for Chrome/Safari actual request
context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
}
}
}

在 Web.config 中:

  ...
<system.webServer>
...
<modules runAllManagedModulesForAllRequests="true">
...
<add name="CrossOriginModule" preCondition="managedHandler" type="YOURNANMESPACEHERE.CrossOriginModule, ASSEMBLYNAME" />
</modules>
<handlers>
<add name="CrossOrigin" verb="OPTIONS" path="*" type="YOURNAMESPACEHERE.CrossOriginHandler, ASSEMBLYNAME" />
</handlers>
</system.webServer>

关于c# - 支持aspx页面中的OPTIONS请求头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6588447/

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