locked
How to send Auth cookie in WebView Request?

    Question

  • I am building a XAML / C# app that authenticates to SharePoint Online which retrieves data that includes html snippets. When I do a WebView.NavigateToString() or inject the HTML with an InvokeScriptAsync, the <image tags whose source in SharePoint online get a 302 redirect. I have tried setting the auth cookies in JavaScript with the document.cookie command, and have tried setting them by using the WebView.NavigateWithHttpRequestMessage method, but neither actually send the auth cookies along with the request. 

    Here is the code:

    HttpRequestMessage message = new HttpRequestMessage();
                message.Method = HttpMethod.Get;
                message.Headers.Cookie.Add(new HttpCookiePairHeaderValue("FedAuth", fedAuthCookie));
                message.Headers.Cookie.Add(new HttpCookiePairHeaderValue("rtFa", rtFaCookie));
                message.RequestUri = new Uri("https://mysite.sharepoint.com");
                sender.NavigateWithHttpRequestMessage(message);

    When I inspect the request with Fiddler, the cookies are not there.

    Tuesday, February 18, 2014 3:59 PM

Answers

  • Hi Eric,

    You can add cookies to a WebView control using code that looks like this. Hopefully it helps resolve your issue.

                try
                {
                    Uri baseUri = new Uri(txtURI.Text);
                    Windows.Web.Http.Filters.HttpBaseProtocolFilter filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
                    Windows.Web.Http.HttpCookie cookie = new Windows.Web.Http.HttpCookie("cookieName", baseUri.Host, "/");
                    cookie.Value = "cookieValue";
                    filter.CookieManager.SetCookie(cookie, false);
    
                    Windows.Web.Http.HttpRequestMessage httpRequestMessage = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Get, baseUri);
                    wvTest.NavigateWithHttpRequestMessage(httpRequestMessage);
    
                }
                catch (Exception oEx)
                {
                    // handle exception
                }


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    • Marked as answer by Eric S. Grover Wednesday, February 19, 2014 2:58 AM
    Wednesday, February 19, 2014 2:37 AM
    Moderator

All replies

  • Hi Eric,

    You can add cookies to a WebView control using code that looks like this. Hopefully it helps resolve your issue.

                try
                {
                    Uri baseUri = new Uri(txtURI.Text);
                    Windows.Web.Http.Filters.HttpBaseProtocolFilter filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
                    Windows.Web.Http.HttpCookie cookie = new Windows.Web.Http.HttpCookie("cookieName", baseUri.Host, "/");
                    cookie.Value = "cookieValue";
                    filter.CookieManager.SetCookie(cookie, false);
    
                    Windows.Web.Http.HttpRequestMessage httpRequestMessage = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Get, baseUri);
                    wvTest.NavigateWithHttpRequestMessage(httpRequestMessage);
    
                }
                catch (Exception oEx)
                {
                    // handle exception
                }


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    • Marked as answer by Eric S. Grover Wednesday, February 19, 2014 2:58 AM
    Wednesday, February 19, 2014 2:37 AM
    Moderator
  • Fantastic!  It solved my problem.  Thanks.
    Wednesday, February 19, 2014 2:58 AM