Xardas
Xardas

Reputation: 33

Problem with redirection to HttpWebResponse Uri with basic authentication

I need to redirect user outside my web application to the page that uses basic authentication.

Here's what I have been trying for quite a bit:

HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;    

String username = "username";
            String password = "password";
            String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
            req.Headers.Add("Authorization", "Basic " + encoded);

HttpWebResponse response = req.GetResponse() as HttpWebResponse;
return this.Redirect(response.ResponseUri.ToString());

I have also tried cache scenario, but with no success:

CredentialCache myCache = new CredentialCache();

            myCache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
            req.Credentials = myCache;

Can I get some help?

Upvotes: 0

Views: 232

Answers (1)

poke
poke

Reputation: 387775

redirect user outside my web application to the page that uses basic authentication

So you want to redirect someone to a different page, and that page requires basic authentication? So you want to send the user to that target page?

Then you won’t be able to just request that target page with basic authentication and doing something with it. You basically have two options here:

  1. Retrieve the content from that other page and present it yourself. This will not redirect the user but keep them on your site.
  2. Redirect to the URL of the target page but include the credentials as part of the URL so that the user’s browser will use these credentials to perform basic authentication agains that page.

    URLs with basic authentication credentials usually look like this: https://username:[email protected]/.

    So you will actually have to expose the credentials to the user if you want them to redirect there. Otherwise you will not be able to have them go there on their own, as there is no way to pass authentication without making that visible to the user.

Upvotes: 1

Related Questions