Cesar,
Thank you so much for your response. I have pasted the code below. I
have also changed it so that I am creating a new guid for the
messageId, timestamp, and securityToken id. However, I got the same
results.
I did notice something very interesting though. I actually saw this
happen before but when I posted yesterday, this wasn't happening so I
thought I may have fixed it. This morning when I tested, the input
file to my webservice had this for a timestamp:
<wsu:Timestamp wsu:Id="Timestamp-097cfcf9-7d52-494e-9917-a5fe157ef8a6">
<wsu:Created>2005-10-19T09:11:57Z</wsu:Created>
<wsu:Expires>2005-10-19T09:21:57Z</wsu:Expires>
</wsu:Timestamp>
However, the output file which generated the exception had this:
<wsu:Timestamp wsu:Id="Timestamp-09a51f64-67ba-4917-8729-4a4f36f8347f">
<wsu:Created>2005-10-19T13:16:57Z</wsu:Created>
<wsu:Expires>2005-10-19T13:21:57Z</wsu:Expires>
</wsu:Timestamp>
Note the time on the output has had 6 hours added to it, which
definitly would seem invalid. I reset IIS and deleted the old tracing
files so I'm positive the input and output are lining up with the same
request. Could it be that something under the covers is changing that
timestamp and causing this error?
Here is the code:
public class tester
{
public string CallWebMethodWithParams(string id, string password,
string webServiceURI, string webMethodName, params object[] parameters)
{
//Note: For the next 3 lines of code, the inputParams will be
//an array of parameters for the web service method being invoked.
string wsdl = WSDLReader.getWSDL(webServiceURI,webMethodName);
char[] delims = {'|'};
string[] inputParams = wsdl.Split(delims);
StringBuilder soapStarter = new StringBuilder();
soapStarter.Append("<?xml version='1.0' encoding='utf-8'?>");
//Beginning of envelope node
soapStarter.Append("<soap:Envelope
xmlns:soap='
http://schemas.xmlsoap.org/soap/envelope/'");
soapStarter.Append("
xmlns:xsi='
http://www.w3.org/2001/XMLSchema-instance'");
soapStarter.Append(" xmlns:xsd='
http://www.w3.org/2001/XMLSchema'");
soapStarter.Append("
xmlns:wsa='
http://schemas.xmlsoap.org/ws/2004/03/addressing'");
soapStarter.Append("
xmlns:wsse='
http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'");
soapStarter.Append("
xmlns:wsu='
http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'>");
//End of envelope node
UserNameTokenMaker tm = new UserNameTokenMaker(id,password);
soapStarter.Append(tm.getSoapHeader(webServiceURI,webMethodName));
soapStarter.Append("<soap:Body>");
soapStarter.Append("<");
soapStarter.Append(webMethodName);
soapStarter.Append(" xmlns='
http://tempuri.org/'>");
for(int i=0;i<inputParams.Length;i++)
{
string paramId = inputParams
;
soapStarter.Append("<").Append(paramId).Append(">");
soapStarter.Append(Convert.ToString(parameters));
soapStarter.Append("</").Append(paramId).Append(">");
}
soapStarter.Append("</").Append(webMethodName).Append(">");
soapStarter.Append("</soap:Body>");
soapStarter.Append("</soap:Envelope> ");
XmlDocument doc = new XmlDocument();
doc.LoadXml(soapStarter.ToString());
string myResponse = string.Empty;
try
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(webServiceURI);
StringBuilder soapAction = new StringBuilder();
soapAction.Append("http://tempuri.org/").Append(webMethodName);
req.Headers.Add("SOAPAction",soapAction.ToString());
req.ContentType = "text/xml;charset='utf-8'";
req.Accept = "text/xml";
req.Method = "POST";
Stream stm = req.GetRequestStream();
doc.Save(stm);
stm.Close();
WebResponse resp = req.GetResponse();
stm = resp.GetResponseStream();
StreamReader r = new StreamReader(stm);
myResponse = r.ReadToEnd();
}
catch(SecurityFault eSec)
{
myResponse = string.Format(CONST_ERROR_FORMAT, "", eSec.ToString(),
(eSec.InnerException != null ? eSec.InnerException.ToString() :
string.Empty));
}
catch(Exception e)
{
myResponse = string.Format(CONST_ERROR_FORMAT, "", e.ToString(),
(e.InnerException != null ? e.InnerException.ToString() :
string.Empty));
}
return myResponse;
}
}
public class UserNameTokenMaker
{
private string _id;
private string _password;
public UserNameTokenMaker(string id,string password)
{
_id = id;
_password = password;
}
public string getSoapHeader(string URI,string methodName)
{
StringBuilder soapHeader = new StringBuilder();
soapHeader.Append("<soap:Header>");
soapHeader.Append("<wsa:Action>http://tempuri.org/").Append(methodName).Append("</wsa:Action>");
string myGuid = Guid.NewGuid().ToString();
soapHeader.Append("<wsa:MessageID>uuid:").Append(myGuid).Append("</wsa:MessageID>");
soapHeader.Append("<wsa:ReplyTo>");
soapHeader.Append("<wsa:Address>http://schemas.xmlsoap.org/ws/2004/03/addressing/role/anonymous</wsa:Address>");
soapHeader.Append("</wsa:ReplyTo>");
soapHeader.Append("<wsa:To>").Append(URI).Append("</wsa:To>");
soapHeader.Append(getUserNameTokenHeader());
soapHeader.Append("</soap:Header>");
return soapHeader.ToString();
}
private string getUserNameTokenHeader()
{
StringBuilder userNameHeader = new StringBuilder();
userNameHeader.Append("<wsse:Security soap:mustUnderstand='1'>");
userNameHeader.Append("<wsu:Timestamp
wsu:Id='Timestamp-").Append(Guid.NewGuid().ToString()).Append("'>");
DateTime rightNow = DateTime.Now.AddMinutes(-5);
string fromDay = rightNow.ToString("yyyy-MM-dd");
string fromTime = rightNow.ToString("HH:mm:ss");
DateTime notNow = rightNow.AddMinutes(10);
string toDay = notNow.ToString("yyyy-MM-dd");
string toTime = notNow.ToString("HH:mm:ss");
userNameHeader.Append("<wsu:Created>").Append(fromDay).Append("T").Append(fromTime).Append("Z").Append("</wsu:Created>");
userNameHeader.Append("<wsu:Expires>").Append(toDay).Append("T").Append(toTime).Append("Z").Append("</wsu:Expires>");
userNameHeader.Append("</wsu:Timestamp>");
userNameHeader.Append("<wsse:UsernameToken
xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'
wsu:Id='SecurityToken-").Append(Guid.NewGuid().ToString()).Append("'>");
userNameHeader.Append("<wsse:Username>").Append(_id).Append("</wsse:Username>");
userNameHeader.Append("<wsse
assword
Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>").Append(_password).Append("</wsse
assword>");
userNameHeader.Append("<wsse:Nonce>KBlpA4ql1SWPoH8qT/hHbg==</wsse:Nonce>");
userNameHeader.Append("<wsu:Created>").Append(fromDay).Append("T").Append(fromTime).Append("Z").Append("</wsu:Created>");
userNameHeader.Append("</wsse:UsernameToken>");
userNameHeader.Append("</wsse:Security>");
return userNameHeader.ToString();
}
}
public class tester
{
public string CallWebMethodWithParams(string id, string password,
string webServiceURI, string webMethodName, params object[] parameters)
{
//Note: For the next 3 lines of code, the inputParams will be
//an array of parameters for the web service method being invoked.
string wsdl = WSDLReader.getWSDL(webServiceURI,webMethodName);
char[] delims = {'|'};
string[] inputParams = wsdl.Split(delims);
StringBuilder soapStarter = new StringBuilder();
soapStarter.Append("<?xml version='1.0' encoding='utf-8'?>");
//Beginning of envelope node
soapStarter.Append("<soap:Envelope
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'");
soapStarter.Append("
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'");
soapStarter.Append(" xmlns:xsd='http://www.w3.org/2001/XMLSchema'");
soapStarter.Append("
xmlns:wsa='http://schemas.xmlsoap.org/ws/2004/03/addressing'");
soapStarter.Append("
xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'");
soapStarter.Append("
xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'>");
//End of envelope node
UserNameTokenMaker tm = new UserNameTokenMaker(id,password);
soapStarter.Append(tm.getSoapHeader(webServiceURI,webMethodName));
soapStarter.Append("<soap:Body>");
soapStarter.Append("<");
soapStarter.Append(webMethodName);
soapStarter.Append(" xmlns='http://tempuri.org/'>");
for(int i=0;i<inputParams.Length;i++)
{
string paramId = inputParams;
soapStarter.Append("<").Append(paramId).Append(">");
soapStarter.Append(Convert.ToString(parameters));
soapStarter.Append("</").Append(paramId).Append(">");
}
soapStarter.Append("</").Append(webMethodName).Append(">");
soapStarter.Append("</soap:Body>");
soapStarter.Append("</soap:Envelope> ");
XmlDocument doc = new XmlDocument();
doc.LoadXml(soapStarter.ToString());
string myResponse = string.Empty;
try
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(webServiceURI);
StringBuilder soapAction = new StringBuilder();
soapAction.Append("http://tempuri.org/").Append(webMethodName);
req.Headers.Add("SOAPAction",soapAction.ToString());
req.ContentType = "text/xml;charset='utf-8'";
req.Accept = "text/xml";
req.Method = "POST";
Stream stm = req.GetRequestStream();
doc.Save(stm);
stm.Close();
WebResponse resp = req.GetResponse();
stm = resp.GetResponseStream();
StreamReader r = new StreamReader(stm);
myResponse = r.ReadToEnd();
}
catch(SecurityFault eSec)
{
myResponse = string.Format(CONST_ERROR_FORMAT, "", eSec.ToString(),
(eSec.InnerException != null ? eSec.InnerException.ToString() :
string.Empty));
}
catch(Exception e)
{
myResponse = string.Format(CONST_ERROR_FORMAT, "", e.ToString(),
(e.InnerException != null ? e.InnerException.ToString() :
string.Empty));
}
return myResponse;
}
}
public class UserNameTokenMaker
{
private string _id;
private string _password;
public UserNameTokenMaker(string id,string password)
{
_id = id;
_password = password;
}
public string getSoapHeader(string URI,string methodName)
{
StringBuilder soapHeader = new StringBuilder();
soapHeader.Append("<soap:Header>");
soapHeader.Append("<wsa:Action>http://tempuri.org/").Append(methodName).Append("</wsa:Action>");
string myGuid = Guid.NewGuid().ToString();
soapHeader.Append("<wsa:MessageID>uuid:").Append(myGuid).Append("</wsa:MessageID>");
soapHeader.Append("<wsa:ReplyTo>");
soapHeader.Append("<wsa:Address>http://schemas.xmlsoap.org/ws/2004/03/addressing/role/anonymous</wsa:Address>");
soapHeader.Append("</wsa:ReplyTo>");
soapHeader.Append("<wsa:To>").Append(URI).Append("</wsa:To>");
soapHeader.Append(getUserNameTokenHeader());
soapHeader.Append("</soap:Header>");
return soapHeader.ToString();
}
private string getUserNameTokenHeader()
{
StringBuilder userNameHeader = new StringBuilder();
userNameHeader.Append("<wsse:Security soap:mustUnderstand='1'>");
userNameHeader.Append("<wsu:Timestamp
wsu:Id='Timestamp-").Append(Guid.NewGuid().ToString()).Append("'>");
DateTime rightNow = DateTime.Now.AddMinutes(-5);
string fromDay = rightNow.ToString("yyyy-MM-dd");
string fromTime = rightNow.ToString("HH:mm:ss");
DateTime notNow = rightNow.AddMinutes(10);
string toDay = notNow.ToString("yyyy-MM-dd");
string toTime = notNow.ToString("HH:mm:ss");
userNameHeader.Append("<wsu:Created>").Append(fromDay).Append("T").Append(fromTime).Append("Z").Append("</wsu:Created>");
userNameHeader.Append("<wsu:Expires>").Append(toDay).Append("T").Append(toTime).Append("Z").Append("</wsu:Expires>");
userNameHeader.Append("</wsu:Timestamp>");
userNameHeader.Append("<wsse:UsernameToken
xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'
wsu:Id='SecurityToken-").Append(Guid.NewGuid().ToString()).Append("'>");
userNameHeader.Append("<wsse:Username>").Append(_id).Append("</wsse:Username>");
userNameHeader.Append("<wsse
assword
Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>").Append(_password).Append("</wsse
assword>");
userNameHeader.Append("<wsse:Nonce>KBlpA4ql1SWPoH8qT/hHbg==</wsse:Nonce>");
userNameHeader.Append("<wsu:Created>").Append(fromDay).Append("T").Append(fromTime).Append("Z").Append("</wsu:Created>");
userNameHeader.Append("</wsse:UsernameToken>");
userNameHeader.Append("</wsse:Security>");
return userNameHeader.ToString();
}
}