[英]Forms Authentication - Require SSL - Setting Authentication Cookie Fails
[英]Forms authentication with NetworkCredentials fails
这是我用来认证自己并采取行动的方式:
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
没有收到我提供的凭据。 我究竟做错了什么?
我相信,如果要使用网络凭据,则应使用Windows身份验证/基本身份验证。
对于表单身份验证,您需要将username
和password
到页面上,就像浏览器将执行的操作一样,并从响应中读取身份验证cookie。 您应该使用此Cookie来进一步与经过身份验证的页面进行通信
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.