CORS请求标头参数未在Chrome中发送

时间:2016-06-28 14:37:27

标签: google-chrome http-headers cors cross-domain swagger

我尝试使用swagger ui进行rest api文档。使用WCF服务创建的Rest API。我解决了cors问题,服务接受了OPTIONS方法。但是请求有一个头参数,其名称是SessionUUID,此参数不发送给服务。当我调试服务端时,此参数不会出现。此问题仅适用于Chrome。 Chrome会在cors请求中阻止标题参数。

像这样在WCF服务上解决Cors问题;

  1. 我添加了新行为,标题参数已定义

    public class EnableCorsEndpointBehavior : BehaviorExtensionElement, IEndpointBehavior
    {
       ...
    
        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
            var requiredHeaders = new Dictionary<string, string>();
    
            requiredHeaders.Add("Access-Control-Allow-Origin", "*");
            requiredHeaders.Add("Access-Control-Request-Method", "POST,GET,PUT,DELETE,OPTIONS");
            requiredHeaders.Add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept, Accept-Language, SessionUUID, Origin, X-Custom-Header, sessionuuid");
            requiredHeaders.Add("Access-Control-Allow-Credentials", "true");
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new ServiceInspector(requiredHeaders));
        }    
       ...
      }
    
    1. 我在检查器中添加了响应标头。由于此问题,在AfterReceiveRequest中检查了名为SessionUUID的标头参数,但它是 null

      public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
      {
          try
          {
      
      
              WebHeaderCollection headers = WebOperationContext.Current.IncomingRequest.Headers;
              string requestTo = request.Headers.To.ToString();
      
      
              if (!requestTo.Contains("login") && request.GetType().Name != "InternalByteStreamMessage")
              {
                  if (headers["SessionUUID"] == null)
                  {
                      throw new RestAppException("401", "Oturum bilgisi bulunamadı.");
                  }
                  else
                  {
                      DateTime requestDate = DateTime.Now;
                      string sessionUUID = headers["SessionUUID"] != null ? headers["SessionUUID"].AsString() : headers["sessionuuid"].AsString();
                      sessionManager.CheckSessionByUUID(sessionUUID, requestDate);
      
                      sessionManager.UpdateExpireDate(sessionUUID, requestDate);
                      sessionManager.SetSessionByUUID(sessionUUID);
                  }
              }
          }
          catch (RestAppException ex)
          {
              ex.Message = "İşlem Başarısız";
              throw ex;
          }
          catch (Exception ex)
          {
              throw ex;
          }
      
          return null;
      }
      
      public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
      {
          var httpHeader = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
          foreach (var item in requiredHeaders)
          {
              httpHeader.Headers.Add(item.Key, item.Value);
          }
      }
      
  2. 我如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您必须添加Chrome扩展程序以允许自定义标题。

以下是您可以使用的一些内容:

  1. WebRequestAPI
  2. Mod Header
  3. 如果您想试用Mozilla Firefox,请使用其扩展程序modify-headers

答案 1 :(得分:0)

我遇到了类似的问题。可能和url有关。

当我输入“www”时,我的问题解决了。

如果您已经输入了“www”,请删除它并重试。