Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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,
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 16-Dec-12 23:18pm
Edited 16-Dec-12 23:23pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
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.
  Permalink  
Comments
PushpaAmal at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,725
1 OriginalGriff 6,696
2 CPallini 5,315
3 George Jonsson 3,589
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100