我试图建立在通过表单验证保护,并通过在IE中一个.net用户控件访问ASP.Net兼容模式在IIS托管的WCF服务设置的HTTP cookies(头)。 (见Secure IIS hosted WCF service for access via IE hosted user control)。如何在HTTP请求
需要在IE浏览器的用户控制,因为它使用了其中不存在任何在Silverlight或AJAX可比一个特定的第三方控制。
所以我需要的用户控件来设置HTTP请求头认证和会话ID的Cookie它访问WCF服务之前。我的方法是设置一个消息检查器来执行此操作。
所以我定义消息检查:
public class CookieInspector : IClientMessageInspector {
public void AfterReceiveReply(ref Message reply, object correlationState) {
}
public object BeforeSendRequest(
ref Message request,
IClientChannel channel) {
HttpRequestMessageProperty messageProperty;
if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name)) {
messageProperty = (HttpRequestMessageProperty) request.Properties[
HttpRequestMessageProperty.Name
];
}
else {
messageProperty = new HttpRequestMessageProperty();
request.Properties.Add(
HttpRequestMessageProperty.Name,
messageProperty
);
}
// Set test headers for now...
messageProperty.Headers.Add(HttpRequestHeader.Cookie, "Bob=Great");
messageProperty.Headers.Add("x-chris", "Beard");
return null;
}
}
和端点行为:
public class CookieBehavior : IEndpointBehavior {
public void AddBindingParameters(
ServiceEndpoint endpoint,
BindingParameterCollection bindingParameters) {
}
public void ApplyClientBehavior(
ServiceEndpoint endpoint,
ClientRuntime clientRuntime) {
clientRuntime.MessageInspectors.Add(new CookieInspector());
}
public void ApplyDispatchBehavior(
ServiceEndpoint endpoint,
EndpointDispatcher endpointDispatcher) {
}
public void Validate(ServiceEndpoint endpoint) {
}
}
和我配置和代码中创建我的频道和WCF客户端:
var ea = new EndpointAddress("http://.../MyService.svc");
// EDIT: Http cookies can't be set with WSHttpBinding :-(
// var binding = WSHttpBinding();
var binding = new BasicHttpBinding();
// Disable automatically managed cookies (which enables user cookies)
binding.AllowCookies = false;
binding.MaxReceivedMessageSize = 5000000;
binding.ReaderQuotas.MaxStringContentLength = 5000000;
var cf = new ChannelFactory<ITranslationServices>(binding, ea);
cf.Endpoint.Behaviors.Add(new CookieBehavior());
ITranslationServices service = cf.CreateChannel();
但是,当我看着我的请求与提琴手,http头和cookie没有设置,我不知道为什么。我读过网上的各种文章,Stackoverflow等基本上说它应该工作,但它不。要么我错过了明显的东西,或者WCF或其他东西中存在错误?
与论坛网站不同,我们不使用“谢谢”或“任何帮助表示”,或在[so]上签名。请参见“[应‘你好’,‘谢谢’标语,并称呼从撤职?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be - 从帖子中删除) – 2012-10-26 05:26:02