如何在 C# 中读取安全的 XML URI?

发布于 2024-12-29 19:01:12 字数 157 浏览 0 评论 0原文

我正在尝试从 C# 中的 URI 读取安全的 XML 文档。我了解 XmlReader 类的基础知识。但是我不知道如何在代码中为 URI 提供用户名和密码。我感觉它与 XmlSecureResolver 对象有关。但我不知道如何设置用户名和密码。谁能帮助我如何设置凭据?

谢谢, 科里

I am trying to read a secure XML document from a URI in C#. I understand the basics of the XmlReader class. However I cannot figure out how to supply a username and password for the URI in code. I get the feeling it has something to do with an XmlSecureResolver object. But I can't figure out how to set the username and password. Can anyone help me with how to set the credentials?

Thanks,
Corey

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的痛♀有谁懂 2025-01-05 19:01:12

我认为这应该可以解决问题:

WebRequest request = WebRequest.Create(url);
request.Credentials = new NetworkCredential("usernamne", "password");

using (WebResponse response = request.GetResponse()) 
{
    using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
    {
        // Blah blah...
    }
}

I think this should do the trick:

WebRequest request = WebRequest.Create(url);
request.Credentials = new NetworkCredential("usernamne", "password");

using (WebResponse response = request.GetResponse()) 
{
    using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
    {
        // Blah blah...
    }
}
白昼 2025-01-05 19:01:12

快速搜索术语“XmlReader Authenticate”会得到第一个结果:

http://msdn.microsoft.com/en-us/library/47as68k4%28v=vs.71%29.aspx

将 URL 解析为包含要读取的 XML 数据的文件时,该文件可能具有受限的访问策略。如果需要进行身份验证才能访问网络资源,请使用 XmlResolver.Credentials 属性指定必要的凭据。如果未设置 XmlResolver.Credentials 属性,则凭据将设置为 null。

XmlTextReader rdr = new XmlTextReader("http://localhost/bookstore/books.xml");
rdr.XmlResolver.Credentials = CredentialCache.DefaultCredentials;
XmlDocument doc = new XmlDocument();
doc.Load(rdr)

并使用不同的凭据:

NetworkCredential myCred = new NetworkCredential(UserName, SecurelyStoredPassword, domain); 
CredentialCache myCache = new CredentialCache(); 
myCache.Add(new Uri("www.contoso.com"), "Basic", myCred); 
myCache.Add(new Uri("app.contoso.com"), "Basic", myCred);
reader.XmlResolver.Credentials = myCache;

A quick Google of the term "XmlReader Authenticate" yields this as the first result:

http://msdn.microsoft.com/en-us/library/47as68k4%28v=vs.71%29.aspx

When resolving a URL to a file that contains the XML data to read, the file may have a restricted access policy. If authentication is required to access a network resource, use the XmlResolver.Credentials property to specify the necessary credentials. If the XmlResolver.Credentials property is not set, then credentials are set to null.

XmlTextReader rdr = new XmlTextReader("http://localhost/bookstore/books.xml");
rdr.XmlResolver.Credentials = CredentialCache.DefaultCredentials;
XmlDocument doc = new XmlDocument();
doc.Load(rdr)

and using different credentials:

NetworkCredential myCred = new NetworkCredential(UserName, SecurelyStoredPassword, domain); 
CredentialCache myCache = new CredentialCache(); 
myCache.Add(new Uri("www.contoso.com"), "Basic", myCred); 
myCache.Add(new Uri("app.contoso.com"), "Basic", myCred);
reader.XmlResolver.Credentials = myCache;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文