Access external xml using xslt document function return 401
- by Ciprian Grosu
On MOSS2007, I have a webpart that display the content of a xml feed. I use a xslt with parameters for transforamtions. There is a situation when I receive a 401 Authorisation exception. I realize that this happen when a document() function from my xslt try to open an external xml. If I try to open this xml in browser all work ok. I provided my admin credentials to the web part and to the XmlSecureResolver. Same problem.
The webpart is on server1 and the xml feed and external xml required by xslt is on server2.
What can be ?
protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents(writer);
if (string.IsNullOrEmpty(this.xmlUrl) || this.xmlResponseStream == null)
return;
try
{
XslCompiledTransform transform = new XslCompiledTransform();
if (UseXslt)
{
XmlTextReader stylesheet = null;
try
{
SPSite site = new SPSite(xsltlUrl);
SPWeb web = site.OpenWeb();
SPFile file = web.GetFile(xsltlUrl);
if (file != null)
{
stylesheet = new XmlTextReader(file.OpenBinaryStream());
}
}
catch(Exception ex)
{
stylesheet = new XmlTextReader(xsltlUrl);
}
if (stylesheet != null)
{
transform.Load(stylesheet, new XsltSettings(true, true), GetAResolver());
}
using (XmlReader reader = new XmlTextReader(this.xmlResponseStream))
{
string theParams = xsltProperties;
XsltArgumentList xslAgrs = GetXsltArgumentList(xsltProperties);
XmlTextWriter results = new XmlTextWriter(writer.InnerWriter);
if (UseProperties)
{
transform.Transform(reader, xslAgrs, results, GetASecureResolver());
}
else
{
transform.Transform(reader, results);
}
reader.Close();
}
}
else
{
string feedAsString = null;
using (StreamReader rssReader = new StreamReader(this.xmlResponseStream))
{
feedAsString = rssReader.ReadToEnd();
writer.InnerWriter.Write(SPHttpUtility.HtmlEncode(feedAsString));
}
}
}
catch (Exception ex)
{
writer.Write(ex.Message);
if (this.xmlResponseStream != null)
{
this.xmlResponseStream.Close();
this.xmlResponseStream.Dispose();
}
}
}
private static XmlSecureResolver GetASecureResolver()
{
// Create a secure resolver
XmlSecureResolver resolver = new XmlSecureResolver(new XmlUrlResolver(), "http://externalservername.com/thesite/");
string proxyUserName = RssFeedUtility.GetConfigFileReader().ProxyUserName;
string proxyUserPwd = RssFeedUtility.GetConfigFileReader().ProxyUserPassword;
string proxyUserDomain = RssFeedUtility.GetConfigFileReader().ProxyUserDomain;
resolver.Credentials = new NetworkCredential(proxyUserName, proxyUserPwd, proxyUserDomain);
return resolver;
}