0

这是我用来验证自己并采取行动的方法:

var uri = new Uri(@"http://localhost:5898/forums/AddPost/1");
var request = WebRequest.Create(uri);
request.Credentials = new CredentialCache { { new Uri(@"http://localhost:5898/Account/Login"), "Basic", new NetworkCredential("asdf", "ghijkl") } };
request.Method = "POST";

const string strPost = "Content=TestAplikacji&AddedValue=0";
StreamWriter myWriter = null;

request.ContentLength = strPost.Length;
request.ContentType = "application/x-www-form-urlencoded";

string response;
try
{
    myWriter = new StreamWriter(request.GetRequestStream());
    myWriter.Write(strPost);
}
catch
{

}
finally
{
    if (myWriter != null) myWriter.Close();
}

var objResponse = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(objResponse.GetResponseStream()))
{
    response = sr.ReadToEnd();
    sr.Close();
}

当代码执行时,在response我得到登录名和密码的验证错误。但是我提供的登录名和密码 ( "asdf", "ghijkl") 满足了这些要求,这意味着http://localhost:5898/Account/Login没有收到我提供的凭据。我究竟做错了什么?

4

1 回答 1

3

我相信,如果您想使用网络凭据,您应该使用 Windows 身份验证/基本身份验证。

对于表单身份验证,您需要像浏览器所做的那样将usernameand发布到页面并从响应中读取身份验证 cookie。password您应该使用此 cookie 与经过身份验证的页面进行进一步通信

于 2012-10-09T15:00:57.797 回答