locked
Windows.web.http.httpclient 401 auto login prompt RRS feed

  • Question

  • When using the Windows.Web.Http.HttpClient in my Windows Store Application we have an authentication server that we communicate with. Whenever we get a 401 response back the application hangs and we automatically get a login prompt (that isn't part of our application).

    What can we do to get rid of this auto popup for the login prompt and just handle the 401 response ourselves?

    Tuesday, December 3, 2013 12:02 AM

Answers

All replies

  • You'll have to share the code that makes this happen.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, December 3, 2013 12:59 PM
    Moderator
  • Hi J,

    You can control this with the AllowUI parameter of the BaseProtocolFilter implementation:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.web.http.filters.httpbaseprotocolfilter.allowui.aspx

    I will whip up a quick sample for you!

    Jeff


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    • Marked as answer by jshore Tuesday, December 3, 2013 3:14 PM
    Tuesday, December 3, 2013 3:02 PM
    Moderator
  • Oh My God!! Thank you that worked!! 
    Tuesday, December 3, 2013 3:14 PM
  • Here you go!

     public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                // default filter (does have some properties you can set)
                m_BPF = new HttpBaseProtocolFilter();
                // no UI allowed like for auth prompt
                m_BPF.AllowUI = false;
                // create the client with the BPF
                m_HttpClient = new HttpClient(m_BPF);
                
    
            }
    
            HttpClient m_HttpClient;
            HttpBaseProtocolFilter m_BPF;
            public async Task NoUIForAuth(Uri theUri)
            {
                // ALWAYS use try catch
                try
                {
                    HttpRequestMessage aTempReq = new HttpRequestMessage(HttpMethod.Get, theUri);
                    HttpResponseMessage aResp2 = await m_HttpClient.SendRequestAsync(aTempReq);
                    if (aResp2.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        //todo handle auth or report back
                    }
                }
                catch (Exception ex)
                { 
                    //todo log and or handle (could be WebException too)
                }
            }


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Tuesday, December 3, 2013 3:19 PM
    Moderator