Chintan

Chintan

  • NA
  • 11
  • 15.8k

Auto Login to external website using asp.net c#

Oct 11 2012 3:32 AM
I have the username, password and url of the external website. I want to login into the website automatically and redirect to the index page of the external website. I did used POST, WebRequest but couldn't log in. I tried using below code but couldn't log in
string appURL = "http://blahblah.com";
   
string strPostData = String.Format("userName={0}&password={1}",
   
"username", "password");
   
// Setup the http request.      
   
HttpWebRequest wrWebRequest = WebRequest.Create(appURL) as
   
HttpWebRequest;
    wrWebRequest
.Method = "post";
    wrWebRequest
.ContentLength = strPostData.Length;
    wrWebRequest
.ContentType = "application/x-www-form-urlencoded";
   
CookieContainer cookieContainer = new CookieContainer();
    wrWebRequest
.CookieContainer = cookieContainer;
   
// Post to the login form.      
   
StreamWriter swRequestWriter = new
   
StreamWriter(wrWebRequest.GetRequestStream());
    swRequestWriter
.Write(strPostData);
    swRequestWriter
.Close();
   
// Get the response.      
   
HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();
   
// Read the response      
   
StreamReader srResponseReader = new
   
StreamReader(hwrWebResponse.GetResponseStream());
   
string strResponseData = srResponseReader.ReadToEnd();
    srResponseReader
.Close();

   
Response.Write(strResponseData);
   
return cookieContainer;

Could anyone please help and send me the code?


Answers (1)