• The Microsoft way of performing Network and Internet connection Awareness in C#

    by  • September 6, 2011 • .Net, Journal, Programming • 1 Comment

    Starting from Vista, the PC seemed to have the magically ability to correctly detect network and internet connection information, and whether more information was required to authenticate; usually when connecting via a public wifi hotspot which redirects to an intermediate login page.

    This was achieved via a service introduced in Windows Vista called Network Connectivity Status Indicator (NCSI).

    NCSI examines network connectivity in a variety of ways to give the most accurate state of connectivity. It does this attempting to connect to a special webpage hosted by Microsoft.

    According to a SuperUser Blog post the checks are performed as the following:

    There are a few variations of the connection checking process:

    1. NCSI performs a DNS lookup on www.msftncsi.com, then requests http://www.msftncsi.com/ncsi.txt. This file is a plain-text file and contains only the textMicrosoft NCSI.
    2. NCSI sends a DNS lookup request for dns.msftncsi.com. This DNS address should resolve to 131.107.255.255. If the address does not match, then it is assumed that the internet connection is not functioning correctly.

    The exact sequence of when which test is run is not documented; however, a little bit of digging around with a packet sniffing tool like Wireshark reveals some info. It appears that on any connection, the first thing NCSI does is requests the text file (step 1 above). NCSI expects a 200 OK response header with the proper text returned. If the response is never received, or if there is a redirect, then a DNS request for dns.msftncsi.com is made. If DNS resolves properly but the page is inaccessible, then it is assumed that there is a working internet connection, but an in-browser authentication page is blocking access to the file. This results in the pop-up balloon above. If DNS resolution fails or returns the wrong address, then it is assumed that the internet connection is completely unsuccessful, and the “no internet access” error is shown.

    Now we know how Microsoft performs connection checking in Vista & Windows 7, we can port this over to C# for a ‘correct’ and ‘accurate’ method of checking connection which should give the same response.

    Most internet connection checkers attempt to download a webpage that they believe should never fail, eg. www.google.com and either respond with a valid connection if they receive a 200 OK response or the page successfully downloads. This has the problem if the DNS redirects the page; in the case of public wifi.

    You can download my implementation of the NCSI checks in C# from here

    About

    Software engineer. Tea drinker

    http://MrPfister.com

    One Response to The Microsoft way of performing Network and Internet connection Awareness in C#

    1. Daniel
      June 10, 2015 at 8:47 am

      The link to your C#-Implementation is broken – I’m very interested in that!

    Leave a Reply

    Your email address will not be published. Required fields are marked *