Click here to Skip to main content
15,885,192 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I want to redirect my page to some URL,which needs ID and password for login.
So i want to pass ID and Password while redirecting.So that i dont get login window and directly i can see the content.

Here is the code,
C#
public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
          HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("url");
         request.Method = "POST";
          request.UseDefaultCredentials = false;
         request.PreAuthenticate = true;
         request.Credentials = new NetworkCredential("id", "pwd", "domain");
          HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
          this.Response.Write(response.StatusCode);
    }
    
    
        
}


Please correct as i am getting error as no response from the server.
Posted
Updated 16-Dec-12 23:23pm
v2

1 solution

Depending on the site the NetworkCredential wont be used. Take a look at the actual Log In form for a few things:

- What is the action of the Log In form?
- What is the name of the User Name field?
- What is the name of the Password field?

Then with that information you can make sure you POST to the right URL, some sites post log in requests to another page. Then you can add your User Name and Password to the content of the Request.

something like this:
Assuming UserName is the name of the ID textbox and Password is the name of the Password textbox.
C#
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("url");
request.Method = "POST";
request.UseDefaultCredentials = false;
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential("id", "pwd", "domain");

string formData = "UserName=" + id + "&Password=" + pwd;
using (var writer = new StreamWriter(request.GetRequestStream()))
{
       writer.Write(formData);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
this.Response.Write(response.StatusCode);


This code was written directly into the browser so it probably wont compile.
 
Share this answer
 
Comments
PushpaAmal 18-Dec-12 3:52am    
Hello,

Yes you are right.I checked the username and password field.
But after changing that also m getting below error

"The underlying connection was closed: An unexpected error occurred on a receive."

for HttpWebResponse response = (HttpWebResponse)request.GetResponse();


Please reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900