Skip to content

Auto Login to external website using asp.net c#

Chintan

Asked Oct 10, 2012, 11:32 PM EDT1 answer
Share
LinkedInX
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
Java
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?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.