我试图通过调用Web服务上的一些方法来对Web服务进行单元测试,但是我一直在点击登录页面重定向,而不是获得实际结果。
我首先调用返回了cookie的安全服务,然后可以重用该cookie调用我的其他Web服务。我认为这是避免重定向的方法。
string sharedCookie;
var securityWebService = new SecurityWebServiceClient(CreateCustomBinding(), new EndpointAddress("http://localhost/SecurityWebService.svc"));
using (new OperationContextScope(securityWebService.InnerChannel))
{
securityWebService.Login("UserName", "Password");
var response = (HttpResponseMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpResponseMessageProperty.Name];
sharedCookie = response.Headers["Set-Cookie"];
}
var webService = new WebServiceClient(CreateCustomBinding(), new EndpointAddress("http://localhost/WebServiceWithFormsAuthentication.svc"));
using (new OperationContextScope(webService.InnerChannel))
{
var request = new HttpRequestMessageProperty();
request.Headers["Cookie"] = sharedCookie;
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = request;
var data = webService.GetSomething();
}
错误如下:
There was no endpoint listening at http://localhost/WebServiceWithFormsAuthentication.svc that could accept the message.
内部异常是404
找不到与ResponseUri
登录重定向URL相对应的内容。如果我禁用了表单身份验证,则呼叫将正确进行。
我的问题是:
编辑:出于最奇怪的原因,如果我在两次调用Web服务之前立即在IIS中回收我的应用程序池,它们将正确通过而不被重定向。
编辑2:似乎在我回收了应用程序池后它可以工作,因为尚未创建会话ID cookie。在响应中,它显示在我的身份验证Cookie后面。在任何后续调用中,会话ID cookie首先出现,并且看起来我的第二个Web服务不希望看到该会话ID cookie首先出现...?
编辑3:我的身份验证服务已启用会话,而我的普通Web服务则没有。当会话cookie首先出现在标题中时,似乎第二个不喜欢它。也许不希望首先看到另一个Cookie?
这是我所做的使它工作的方法。我必须这样做似乎很奇怪,所以如果有人有更好的解决方案,我想听听。
安全Web服务在响应标头中返回两个cookie。一个是会话cookie,另一个是身份验证cookie。我所做的是解析Cookie容器中的标头,以仅提取身份验证Cookie,以仅将身份验证Cookie发送给另一个Web服务。
string sharedCookie;
var securityWebService = new SecurityWebServiceClient(CreateCustomBinding(), new EndpointAddress("http://localhost/SecurityWebService.svc"));
using (new OperationContextScope(securityWebService.InnerChannel))
{
securityWebService.Login("UserName", "Password");
var response = (HttpResponseMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpResponseMessageProperty.Name];
var cookieHeader = response.Headers["Set-Cookie"];
var container = new CookieContainer();
container.SetCookies(new Uri("http://localhost"), cookiesString);
var cookies = container.GetCookies(new Uri("http://localhost"));
var authCookie = cookies["nameOfAuthCookie"];
if (authCookie != null)
sharedCookie = authCookie.ToString();
}
这工作得很好。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句