[英].NET - Consuming HTTP services on (Domino) server with form based authentication
我在C#(.NET)中编写一个实用程序,它将使用HTTP从Domino Web服务器请求数据。
但是,此服务器使用基于表单的身份验证,而不是基本HTTP身份验
我试图找到如何使用具有基于表单的身份验证的服务器进行HTTP GETS / POSTS的代码编写。
我已经尝试了我能想到的每个谷歌查询,但只要在查询中使用“基于表单的身份验证”这些词语,所有结果只适用于实现服务服务器端,通常在ASP.NET或Sharepoint中,关于从客户端使用此类服务的结果。
我在Stack Overflow中看到了一个相关问题的Java代码,但是用另一种语言在外国API中查找相关部分将是一次冒险。 如果有可用的.NET示例代码或文档,我将非常感激。
我相信有人可以提供更好的答案,但考虑到这一点,我会假设您托管Web服务的服务器具有某种登录机制来确定您已经过身份验证。 所以我的方法是首先发布到登录页面,接受表单身份验证cookie,然后继续处理您想要的任何后续请求。 以下所有请求中都已包含表单身份验证cookie。
感谢所有提供提示的人。
我想分享我开发的代码,以防它帮助其他人。 虽然它是专门为Domino服务器编写的,但它应该很容易修改为任何基于cookie的身份验证服务器的帖子中的实际字段名称。
要使用该类:
限制:
这个简单的版本将失败的cookie超时。 它不会尝试确定登录是否成功。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml.Linq;
namespace WebDomino
{
/// <summary>
/// A stateful (authenticated) session with a Domino server. Current version only supports
/// forms based authentication, and does not permit bypassing authentication.
/// </summary>
public class DominoHttpSession
{
/// <summary>
/// Username with which to authenticate to the Domino server, must be a legal web user name.
/// </summary>
public string UserName { set; get; }
/// <summary>
/// Web password of the authenticating account.
/// </summary>
public string Password { set; get; }
/// <summary>
/// The server on which the session will exist. At this time, all connections must use
/// the same server. Untested but probably will work: switching server name before establishing
/// a connection, as long as the authentication cookies are shared.
/// </summary>
public string ServerHostName { set; get; }
/// <summary>
/// The session cookies. Provided in case client code wants to analyze cookie content, but
/// more likely only used internally to hold the authentication cookie from the server.
/// </summary>
public CookieContainer Cookies { get { return cookies; } }
private CookieContainer cookies = new CookieContainer();
/// <summary>
/// Sends an HTTP GET to the server, expecting an XML response.
/// </summary>
/// <param name="url">The full url to GET; proper syntax is the responsibility of the caller.</param>
/// <returns>The XElement representing the returned XML text</returns>
public XElement GetXml(string url)
{
return XElement.Parse(Get(url));
}
public string Get(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = cookies;
request.Method = "GET";
using (var responseStream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(responseStream))
{
var result = reader.ReadToEnd();
return result;
}
}
/// <summary>
/// Must be called to establish the session with the server.
/// </summary>
public void Authenticate()
{
ServicePointManager.Expect100Continue = false;
var request = (HttpWebRequest)WebRequest.Create(String.Format("http://{0}//names.nsf?Login", ServerHostName));
request.CookieContainer = cookies;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (var requestStream = request.GetRequestStream())
using (var writer = new StreamWriter(requestStream))
{
writer.Write("Username={0}&Password={1}", UserName,Password);
}
using (var responseStream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(responseStream))
{
var result = reader.ReadToEnd();
// Success is assumed here--in production code it should not be
}
}
public ViewReader GetViewReader(string dbPath, string viewName)
{
return new ViewReader(this)
{
DbPath = dbPath,
View = viewName
};
}
}
}
在基于会话的身份验证(您称之为“基于表单的身份验证”)期间,Domino会生成一个所谓的LTPA令牌 (cookie),以进一步验证用户身份。
最简单的方法是通过将带有必要表单数据的登录表单发送到服务器来模拟浏览器/用户,从服务器响应中提取LTPA cookie并在进一步的请求中使用它。 例如,您可以使用WatiN来简化此操作。
或者你可以自己计算cookie的价值。 LTPA cookie的格式,...很容易通过谷歌找到,但它并不简单,你需要服务器的一些数据。
如果您对服务器知道管理员这样做,您可以要求他添加一个网站规则,允许对一部分请求使用基本身份验证 ,而基于会话的身份验证则用于其余的Web现场。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.