0
我嘗試使用swagger ui作爲rest API文檔。 Rest API使用WCF服務創建。我解決了Cors問題,服務接受OPTIONS方法。但請求有一個名爲SessionUUID的頭參數,該參數不發送給服務。當我調試服務端,這個參數不會來。這個問題只在Chrome中。 Chrome在Cors請求中將塊阻塞到頭部參數。CORS請求標題參數不在Chrome中發送
Cors問題的解決WCF服務是這樣的;
我增加新的行爲和頭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)); } ... }
我在檢查員加入響應頭。在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); } }
我該如何解決這個問題的鍍鉻?
Mod Header的作品非常感謝你。但我需要在服務器端解決這個問題。 – ebruszl
嘗試添加:requiredHeaders.Add(「Access-Control-Expose-Headers」,「SessionUUID」); – Sampada