使用 HTTP Web 请求发送 HTTP 标头以进行 NTLM 身份验证

发布于 2024-08-06 05:23:59 字数 134 浏览 3 评论 0原文

我想登录 Sharepoint 门户,该门户会显示登录对话框,但使用 NTLM 身份验证。如何修改 C# 中的 HTTP 标头以发出成功的登录请求?我假设我需要向门户登录部分中的页面发出 HTTPWebRequest 并在此旁边发布 HTTP 标头集合?

I want to login to a Sharepoint portal which brings up a login dialog but is using NTLM authentication. How can I modify the HTTP headers in C# to make a successful login request? I assume I would need to make a HTTPWebRequest to a page within the logged in section of the portal and post the HTTP headers collection alongside this?

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

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

发布评论

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

评论(3

荒路情人 2024-08-13 05:23:59

您可以使用 WebRequest 类来完成此操作。

WebRequest req = WebRequest.Create(tokenUri);
req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
req.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
WebResponse resp = req.GetResponse();
StreamReader reader = new StreamReader(resp.GetResponseStream());
var token = reader.ReadToEnd().Trim();

此代码将整个响应读取到名为 token 的变量中。

You can do this using the WebRequest class.

WebRequest req = WebRequest.Create(tokenUri);
req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
req.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
WebResponse resp = req.GetResponse();
StreamReader reader = new StreamReader(resp.GetResponseStream());
var token = reader.ReadToEnd().Trim();

This code reads the whole response into a variable called token.

别靠近我心 2024-08-13 05:23:59

要使用 NTLM,请参阅 John 的 回答。如果您需要跨会话的标头,请查看 HttpWebRequest 对象上的 CookieContainer 属性。您需要保留对 CookieContainer 的引用,并将其附加到您发出的任何其他 HttpWebRequest。

To use NTLM see John's answer. If you need to have headers across sessions look at the CookieContainer property on the HttpWebRequest object. You will need to keep a reference to your CookieContainer and attach it to any other HttpWebRequests you make.

葬﹪忆之殇 2024-08-13 05:23:59

为了对 WebRequest 使用 NTLM 身份验证,信用信息应存储在 CredentialCache 中:

var request = (HttpWebRequest)WebRequest.Create(url);
// INIT REQUEST HERE ...

var credential = new NetworkCredential(username, password, domain);
var credentialCache = new CredentialCache();
credentialCache.Add(new Uri(url), "NTLM", credential);
request.Credentials = credentialCache;

// SEND ...

来源:http://predicatet.blogspot.com/2007/01/httpwebrequest-networkcredential-with.html

In order to use NTLM authentication for WebRequest cred info should be stored in CredentialCache:

var request = (HttpWebRequest)WebRequest.Create(url);
// INIT REQUEST HERE ...

var credential = new NetworkCredential(username, password, domain);
var credentialCache = new CredentialCache();
credentialCache.Add(new Uri(url), "NTLM", credential);
request.Credentials = credentialCache;

// SEND ...

Source: http://predicatet.blogspot.com/2007/01/httpwebrequest-networkcredential-with.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文