简体   繁体   English

使用NetworkCredentials进行表单身份验证失败

[英]Forms authentication with NetworkCredentials fails

This is what I use to authenticate myself and make an action: 这是我用来认证自己并采取行动的方式:

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();
}

When the code executes, in the response I get validation errors for login and password. 代码执行后,在response我将获得登录名和密码的验证错误。 But my provided login and password ( "asdf", "ghijkl" ) are satisfying those requirements, and that means, the http://localhost:5898/Account/Login is not receiving credentials that I have provided. 但是我提供的登录名和密码( "asdf", "ghijkl" )满足了这些要求,这意味着http://localhost:5898/Account/Login没有收到我提供的凭据。 What am I doing wrong? 我究竟做错了什么?

I believe, you should use Windows authentication / Basic Authentication, if you want to use the Network credential. 我相信,如果要使用网络凭据,则应使用Windows身份验证/基本身份验证。

For forms authentication you need to post the username and password to the page just like what a browser will do and read the authentication cookie from the response. 对于表单身份验证,您需要将usernamepassword到页面上,就像浏览器将执行的操作一样,并从响应中读取身份验证cookie。 You should be using this cookie for communicating further with authenticated pages 您应该使用此Cookie来进一步与经过身份验证的页面进行通信

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM