locked
Displaying External Content In MVC RRS feed

  • Question

  • User1929067570 posted

    Hi,

    How can i display an external website (Has an authentication required popup) in partial view, and handling the username and password in controller?

    I have an example to redirect but i have 401 unauthorized error : 

    var request = (HttpWebRequest)WebRequest.Create("url");
                request.Method = "GET";
                var cred = new NetworkCredential("user", "pass");
                var cache = new CredentialCache();
                cache.Add(new Uri("url"), "Basic", cred);
                request.Credentials = cache;
                var response = (HttpWebResponse)request.GetResponse();
                return Redirect(response.ResponseUri.ToString());

    Monday, March 4, 2019 3:32 PM

All replies

  • User475983607 posted

    samirkarim

    Hi,

    How can i display an external website (Has an authentication required popup) in partial view, and handling the username and password in controller?

    I have an example to redirect but i have 401 unauthorized error : 

    var request = (HttpWebRequest)WebRequest.Create("url");
                request.Method = "GET";
                var cred = new NetworkCredential("user", "pass");
                var cache = new CredentialCache();
                cache.Add(new Uri("url"), "Basic", cred);
                request.Credentials = cache;
                var response = (HttpWebResponse)request.GetResponse();
                return Redirect(response.ResponseUri.ToString());

    This is a duplicate post.

    https://forums.asp.net/p/2153419/6253224.aspx?Displaying+External+Content+In+MVC

    There's a lot of potential issues with the code shown.  Login is usually a POST not a GET.  The code indicates HTTP Basic authentication but the credentials are not base64 encoded. I assume the remote site returns an authentication cookie or an authentication token but neither situation is handled in the code sample.  It looks like you are trying to proxy authentication which more than likely will not work as the client will not be able to decode the token.

    I recommend that you use a proxy like Fiddler to record the HTTP request/responses required to login to the remote site using code.  This can also be accomplished by using your browser's dev tools (F12).

    Probably your best bet is to contact the remote site owner and explain what you are trying to do.

    Monday, March 4, 2019 4:01 PM