如何检查用户是否已成功登录WebRequest [英] How to check if user have sucessfully logged in webrequest
问题描述
我已经编写了代码并执行得很好,我提供了用户名和密码来实现POST,但我不知道登录是否成功或没有办法检查吗?
var strId = UserName.Text;
var strName = UserPass.Text;
var encoding = new ASCIIEncoding();
var postData = "MainContent_LoginUser_UserName=" + strId + &MainContent_LoginUser_Password=" + strName + "&LoginButton";
byte[] data = encoding.GetBytes(postData);
var myRequest = (HttpWebRequest)WebRequest.Create("http://localhost:6226/WebSite1/Account/Login.aspx");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
var newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
var response = myRequest.GetResponse();
var responseStream = response.GetResponseStream();
var responseReader = new StreamReader(responseStream);
textBox1.Text = responseReader.ReadToEnd();
您需要HTTP响应代码.通常200表示成功,401被禁止或302-重定向到登录页面.
实际上,您正在提交登录页面,因此302可能指示成功登录并重定向到默认用户页面,这取决于服务器上启用了哪种身份验证类型以及login.aspx背后的逻辑. /p>
I have written a code and executes well I provide it username and password it implements POST but i dont know that login is sucessfull or not is there any way to check that?
var strId = UserName.Text;
var strName = UserPass.Text;
var encoding = new ASCIIEncoding();
var postData = "MainContent_LoginUser_UserName=" + strId + &MainContent_LoginUser_Password=" + strName + "&LoginButton";
byte[] data = encoding.GetBytes(postData);
var myRequest = (HttpWebRequest)WebRequest.Create("http://localhost:6226/WebSite1/Account/Login.aspx");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
var newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
var response = myRequest.GetResponse();
var responseStream = response.GetResponseStream();
var responseReader = new StreamReader(responseStream);
textBox1.Text = responseReader.ReadToEnd();
You need HTTP response code. Usually 200 means success, 401 is Forbidden or 302 - redirect to login page.
EDIT: Actually, you are submitting to the login page, so 302 may indicate successful sign in and redirect to the default user page, it depends on what authentication type is enabled on the server and the logic behind login.aspx.
这篇关于如何检查用户是否已成功登录WebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!