ppittle
asked on
Generate an ICredentials object from HttpContext.Current
I have a Solution with two Web Applications. A page (pageA) in Web Applications A needs to display the content from a page (pageB) in Web Application B. I set up this up as so:
public class pageA : System.Web.UI.Page
{
protected override Page_Load(object sender,EventArgs e)
{
System.Net.WebClient client = new WebClient();
Stream htmlStream = client.OpenRead("http://localhost/WebApplicationB/pageB.aspx");
Response.Write(new StreamReader(htmlStream).R eadToEnd() );
}
}
Everything works fine if WebApplicationB is set up in IIS for anonymous access. However, I need to set up the site to use Basic Authentication. When WebApplicationB requires authentication, my code above will throw a WebException saying a 401 unauthorized HTML status code was returned. This is to be expected as my WebClient object isn't passing any credentials.
If I modify the code as so:
System.Net.WebClient client = new WebClient();
client.Credentials = new NetworkCredential("validUs erName","v alidPasswo rd");
Everything is happy again.
However, I don't want to hard code a username / password and I don't want to store it in a web.config or other settings file. Since my WebApplicationA is using the same authentication as WebApplicationB is there a way to get the Identity of the user making the request to WebApplicatioA\PageA and use that to build a NetworkCrendential or ICredentials object?
In other words,
System.Net.WebClient client = new WebClient();
client.Credentials = (NetworkCredential)HttpCon text.Curre nt.Request .User;
Thanks,
PJ
public class pageA : System.Web.UI.Page
{
protected override Page_Load(object sender,EventArgs e)
{
System.Net.WebClient client = new WebClient();
Stream htmlStream = client.OpenRead("http://localhost/WebApplicationB/pageB.aspx");
Response.Write(new StreamReader(htmlStream).R
}
}
Everything works fine if WebApplicationB is set up in IIS for anonymous access. However, I need to set up the site to use Basic Authentication. When WebApplicationB requires authentication, my code above will throw a WebException saying a 401 unauthorized HTML status code was returned. This is to be expected as my WebClient object isn't passing any credentials.
If I modify the code as so:
System.Net.WebClient client = new WebClient();
client.Credentials = new NetworkCredential("validUs
Everything is happy again.
However, I don't want to hard code a username / password and I don't want to store it in a web.config or other settings file. Since my WebApplicationA is using the same authentication as WebApplicationB is there a way to get the Identity of the user making the request to WebApplicatioA\PageA and use that to build a NetworkCrendential or ICredentials object?
In other words,
System.Net.WebClient client = new WebClient();
client.Credentials = (NetworkCredential)HttpCon
Thanks,
PJ
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER