Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

Download:

Datei 1: Fritzbox_Tools.zip

Fritzbox: WPF Login and evaluation by c # code

 

Project code in C # for logging into an AVM Fritzbox with webrequest or webBrowser

This C # code can be used as a basis for reading an AVM Fritzbox such as the telephone book, the call lists or the contacts.

 

Subject: WPF, Visual Studio, AVM Fritzbox, automation router and telephone exchange

 

Video Tutorial

 

MainWindows, xaml:

In Mainwindow I have a password field, into which one the password to the log in to the Fritz box can enter.

By clicking on the start button, a session is determined and an encrypted code is created together with the password, which allows access to the web pages of the Fritzbox via Webrequest.

Alternatively one can read out with a webBrowser Control the fritzbox.

 

<Window x:Class="Fritzbox_Tools.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:Fritzbox_Tools"

        mc:Ignorable="d"

        Title="Fritzbox Tools" Height="350" Width="525">

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="25"/>

            <RowDefinition Height="284*"/>

            <RowDefinition Height="284*"/>

        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  >

            <Button x:Name="btnStart" Grid.Row="0" Content="Start" Width="75"  Click="btnStart_Click" FontWeight="Bold" />

            <Label x:Name="lblPassword" Grid.Row="0" Content="Passwort:" VerticalAlignment="Stretch"  Margin="10,0,0,0"  />

            <TextBox x:Name="tbxPassword" Grid.Row="0" TextWrapping="NoWrap"  Text="DemoPass" VerticalAlignment="Stretch" Width="76" VerticalContentAlignment="Center" Foreground="Blue" Background="LightGreen"  />

            <Label x:Name="lblChallenge" Grid.Row="0" Content="Challenge:" VerticalAlignment="Stretch"  Margin="10,0,0,0" />

            <TextBox x:Name="tbxCallenge" TextWrapping="NoWrap"  VerticalAlignment="Stretch" Width="100"  Foreground="Blue"  VerticalContentAlignment="Center" ></TextBox>

            <Label x:Name="lblSID" Grid.Row="0" Content="SID:" VerticalAlignment="Stretch"  Margin="10,0,0,0" />

            <TextBox x:Name="tbxSID" TextWrapping="NoWrap"    VerticalAlignment="Stretch"   Width="100" Foreground="Blue" VerticalContentAlignment="Center" ></TextBox>

        </StackPanel>

        <ScrollViewer Grid.Row="1" >

            <TextBlock x:Name="tbxLog" TextWrapping="Wrap" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch"   ><InlineUIContainer>                   

                </InlineUIContainer></TextBlock>

        </ScrollViewer>

       

        <WebBrowser Grid.Row="2" x:Name="ctlBrowser" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Visibility="Visible"  />

    </Grid>

</Window>

 

 

MainWindow c # code

In the main page is the C # code for reading and accessing the AVM Fritzbox.

 

The main method test here consists first of the login and subsequent reading of a website.

Logging in using the method: GetSessionId (..) A SID code, which must be passed as a parameter for Web queries.

Afterwards, you can read a page as a webrequest with page read () and then evaluate it using an htmlDocument.

Or you use a webbrowser control to control the access and also read the values ​​via a htmlDocument.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

using System.IO;                    //StreamReader

using System.Net;                   //Http

using System.Security.Cryptography; //Hash Md5

using System.Xml.Linq;              //XDocument

using HtmlAgilityPack;

 

namespace Fritzbox_Tools

{

    /// <summary>

    /// Interaktionslogik für MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

 

        private void btnStart_Click(object sender, RoutedEventArgs e)

        {

            //tbxLog.Text = tbxLog.Text = fl_Get_MD5Hash_of_String("17e17c0e-DemoPass");

            Test();

        }

 

        public void Test()

        {

            //-------------------< Test() >-------------------           

            //*test login and get first page

 

            //--< login >--

            string benutzername = "xxxxxxxx";

            string kennwort =tbxPassword.Text ;

            // SessionID ermitteln

            string sid = GetSessionId(benutzername, kennwort);

            tbxSID.Text = sid;

            //--</ login >--

           

            //----< tests >----

 

            //< get home page >

            string sHtml_of_website = SeiteEinlesen(@"http://fritz.box/home/home.lua", sid);

            log(sHtml_of_website);

            //</ get home page >

 

            string sTest = "<html>hallo this is a html-String</html>";

 

            //--< loadHTML String to Browser >--

            clsWebbrowser_Errors.SuppressscriptErrors(ctlBrowser, true);

            ctlBrowser.NavigateToString(sTest);

            //--</ loadHTML String to Browser >--

 

            //ctlBrowser.Navigate("http://fritz.box/home/home.lua");

            //mshtml.HTMLDocument doc = ctlBrowser.Document as mshtml.HTMLDocument;

            //doc = ctlBrowser.Document as HtmlDocument;

 

            //< as htmldoc from webreqest >

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

            doc.LoadHtml(sTest);

            //</ as htmldoc from webreqest >

 

            //----</ tests >----

            //-------------------</ Test() >-------------------

        }

 

        public string SeiteEinlesen(string url, string sid)

        {

            //-------------------< SeiteEinlesen() >-------------------

            //*read page with sid access, by webrequest

            Uri uri = new Uri(url + "?sid=" + sid);

            HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;

            StreamReader reader = new StreamReader(response.GetResponseStream());

            string str = reader.ReadToEnd();

            return str; //*return string-result

            //-------------------</ SeiteEinlesen() >-------------------

        }

 

        public string GetSessionId(string benutzername, string kennwort)

        {

            //-------------------< GetSessionId() >-------------------

            //*get the current sessionID for fritz.box

            XDocument doc = XDocument.Load(@"http://fritz.box/login_sid.lua");

            string sid = fl_Get_Value_of_Node_in_XDocument_by_NodeName(doc, "SID");

            if (sid == "0000000000000000")

            {

                string challenge = fl_Get_Value_of_Node_in_XDocument_by_NodeName(doc, "Challenge");

                tbxCallenge.Text = challenge;

                log("Challenge: " + challenge );

 

                string sResponse = fl_GetResponse_by_TempUser_Passwort(challenge, kennwort);

                log("Response of TempUser_Passwort: " + sResponse);

 

                string uri = @"http://fritz.box/login_sid.lua?username=" + benutzername + @"&response=" + sResponse;

                doc = XDocument.Load(uri);

 

                sid = fl_Get_Value_of_Node_in_XDocument_by_NodeName(doc, "SID");

                tbxSID.Text = sid;

                log("SID:" + sid);

            }

            return sid;

            //-------------------</ GetSessionId() >-------------------

        }

 

        public string fl_GetResponse_by_TempUser_Passwort(string challenge, string kennwort)

        {

            //-------------------< get fritzbox-challenge+hashcode () >-------------------

            return challenge + "-" + fl_Get_MD5Hash_of_String(challenge + "-" + kennwort);

            //-------------------</ get fritzbox-challenge+hashcode () >-------------------

        }

 

        public string fl_Get_MD5Hash_of_String(string input)        

        {

            //-------------------< fl_Get_MD5Hash_of_String() >-------------------

            //*create hashcode from string

            MD5 md5Hasher = MD5.Create();

            byte[] data = md5Hasher.ComputeHash(Encoding.Unicode.GetBytes(input));

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < data.Length; i++)

            {

                sb.Append(data[i].ToString("x2"));

            }

            return sb.ToString();

            //-------------------</ fl_Get_MD5Hash_of_String() >-------------------

        }

 

 

 

        #region ----< helper-methods >------

        public string fl_Get_Value_of_Node_in_XDocument_by_NodeName(XDocument doc, string name)

        {

            XElement info = doc.FirstNode as XElement;

            return info.Element(name).Value;

        }

       

        private void log(string sText)

        {

            tbxLog.Text = DateTime.Now.ToLongTimeString() + ": " + sText + Environment.NewLine + tbxLog.Text;

        }

        #endregion ----</ helper-methods >------

 

    }

}

 

 

Additional class: clsWebbrowser_Errors

In order to disable the script-errors in a wpf webbrowser control, I have installed an additional static class file, which turns off the error messages.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

using System.Reflection;        //*Fieldinfo

using System.Windows.Controls;  //*Webbrowser Control

 

namespace Fritzbox_Tools

{

    public static class clsWebbrowser_Errors

    {

        //*set wpf webbrowser Control to silent

        //*code source: https://social.msdn.microsoft.com/Forums/vstudio/en-US/4f686de1-8884-4a8d-8ec5-ae4eff8ce6db

 

        public static void SuppressscriptErrors(this WebBrowser webBrowser, bool hide)

        {

            FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);

            if (fiComWebBrowser == null)

                return;

            object objComWebBrowser = fiComWebBrowser.GetValue(webBrowser);

            if (objComWebBrowser == null)

                return;

 

            objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { hide });

        }

    }

}

 

 

Credentials:

The project requires references

htmlAgilityPack : Bearbeitung von HtmlDocuments und HTMLElementen unter WPF

Microsoft.mshtml: only for demo, under wpf not extensive, can therefore be replaced by htmlAgilityPack.

Mobile
»
WPF, C# Code: Login into an AVM Fritzbox

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de