2010-12-21 108 views
1

我已經寫了一個代碼,並執行良好我提供它的用戶名和密碼它實現POST但我不知道登錄是成功或沒有任何方法來檢查?如何檢查用戶是否已成功登錄webrequest

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

回答

2

您需要HTTP響應代碼。通常200表示成功,401表示禁止或302 - 重定向到登錄頁面。

編輯:實際上,您正在提交到登錄頁面,所以302可能表示登錄成功並重定向到默認用戶頁面,這取決於服務器上啓用的認證類型以及login.aspx後面的邏輯。

+3

當使用不正確的用戶名/密碼登錄時,例如在Yahoo郵件中返回什麼響應代碼?依靠HTTP響應代碼是一個錯誤。 `200 OK`是必要的,但不夠。大多數情況下,即使登錄數據不正確,您也會得到'200 OK',但頁面包含有關登錄失敗的詳細信息。 – Xaqron 2010-12-21 22:45:54

0

看起來像你只需要看看在ResponseStream看到返回的信息。

0

如果您使用基於cookie的身份驗證,則服務器將在登錄成功時使用auth cookie進行響應。

// Creating WebRequest 
var req = (HttpWebRequest)WebRequest.Create(ServerPath + controllerName + "/" + actionName); 
var coockieContainer = new CookieContainer(); 

// AuthCookie is static variable defined. Initially it will be null. But when server 
// respond with auth cookie then we have to store it and add to container for further 
// communication. 

if (AuthCookie != null) 
{ 
    coockieContainer.Add(AuthCookie); 
} 
req.CookieContainer = coockieContainer; 

req.Headers.Add("SOAPAction", "\"\""); 
req.ContentType = "application/json; charset=utf-8"; 
req.ContentLength = bytes.Length; 
req.Accept = "application/json, text/javascript, */*"; 
req.Method = "POST"; 
var stm = req.GetRequestStream(); 
stm.Write(bytes, 0, bytes.Length); 
stm.Close(); 
var resp = req.GetResponse(); 

// If server respond with auth cookie then we have to store it. 
// Here I have used ".ASPXAUTH" name. You can have your own defined name 
if (AuthCookie == null) 
{ 
    AuthCookie = ((HttpWebResponse)resp).Cookies.Cast<Cookie>().Where(cook => 
cook.Name.Equals(".ASPXAUTH")).FirstOrDefault(); 
    // Print the properties of each cookie. 
    Console.WriteLine("Cookie:"); 
    Console.WriteLine("{0} = {1}", AuthCookie.Name, AuthCookie.Value); 
    Console.WriteLine("Domain: {0}", AuthCookie.Domain); 
    Console.WriteLine("Path: {0}", AuthCookie.Path); 
    Console.WriteLine("Port: {0}", AuthCookie.Port); 
    Console.WriteLine("Secure: {0}", AuthCookie.Secure); 
    Console.WriteLine("When issued: {0}", AuthCookie.TimeStamp); 
    Console.WriteLine("Expires: {0} (expired? {1})", AuthCookie.Expires, AuthCookie.Expired); 
    Console.WriteLine("Don't save: {0}", AuthCookie.Discard); 
    Console.WriteLine("Comment: {0}", AuthCookie.Comment); 
    Console.WriteLine("Uri for comments: {0}", AuthCookie.CommentUri); 
    Console.WriteLine("Version: RFC {0}", AuthCookie.Version == 1 ? "2109" : "2965"); 

    // Show the string representation of the cookie. 
    Console.WriteLine("String: {0}", AuthCookie.ToString()); 

} 

var stmr = new StreamReader(resp.GetResponseStream()); 

var json = stmr.ReadToEnd();