Redirect to external URL with basic authentication in MVC

3

I am developing a portal in C # MVC3 and in a given operation I need to open another window in a separate window. To open the page of this new portal I need to send some credentials, since it is implemented with basic authentication.

I'd like to know how I can do it. I already tried controller :

var request = (HttpWebRequest)WebRequest.Create(redirectUrl);

request.Method = "GET";
request.UseDefaultCredentials = false;
request.PreAuthenticate = true;

var cred = new NetworkCredential("user1", "pass123");
var cache = new CredentialCache();
cache.Add(new Uri(redirectUrl), "Basic", cred);

request.Credentials = cache;
var response = (HttpWebResponse) request.GetResponse();

return Redirect(response.ResponseUri.ToString());

What is the best way to do this?

    
asked by anonymous 31.03.2014 / 13:11

1 answer

1

It is not possible, since you would need to generate the exact cookie for the portal you are trying to access.

If the credentials are the same as your portal, you could do something like:

HttpWebRequest req =     (HttpWebRequest)WebRequest.Create(uri); 
// Add the current authentication cookie to the request 
HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; 
Cookie authenticationCookie = new Cookie( FormsAuthentication.FormsCookieName, cookie.Value, cookie.Path, HttpContext.Current.Request.Url.Authority); 
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(authenticationCookie); 
WebResponse res = req.GetResponse();

But I believe this is not your case.

    
30.07.2014 / 02:04