如何检查用户是否已成功登录WebRequest [英] How to check if user have sucessfully logged in webrequest

查看:104
本文介绍了如何检查用户是否已成功登录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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆