2016-06-28 71 views
0

我嘗試使用swagger ui作爲rest API文檔。 Rest API使用WCF服務創建。我解決了Cors問題,服務接受OPTIONS方法。但請求有一個名爲SessionUUID的頭參數,該參數不發送給服務。當我調試服務端,這個參數不會來。這個問題只在Chrome中。 Chrome在Cors請求中將塊阻塞到頭部參數。CORS請求標題參數不在Chrome中發送

Cors問題的解決WCF服務是這樣的;

  1. 我增加新的行爲和頭PARAMS定義

    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)); 
        }  
        ... 
        } 
    
  2. 我在檢查員加入響應頭。在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); 
        } 
    } 
    

我該如何解決這個問題的鍍鉻?

回答

0

您將不得不添加Chrome擴展程序以允許自定義標頭。

這裏有一些,你可以使用:

  1. WebRequestAPI
  2. Mod Header

如果你想嘗試Mozilla Firefox瀏覽器,使用它的擴展modify-headers

+0

Mod Header的作品非常感謝你。但我需要在服務器端解決這個問題。 – ebruszl

+0

嘗試添加:requiredHeaders.Add(「Access-Control-Expose-Headers」,「SessionUUID」); – Sampada