How to manage PBX authentication settings?

In order to implement VoIP PBX registration and authentication functions in your SIP PBX your best choice is Ozeki VoIP SIP SDK. Ozeki SIP SDK ensures you all the necessary tools and background to develop a powerful and reliable SIP PBX. This guide explains with source code how to achieve VoIP PBX registration and authentication with Ozeki VoIP SIP SDK. Please note that, you might need to study the How to develop custom VoIP PBX functionality article to fully understand this guide.

What is authentication? When does it needed?

The user authentication is a basic feature of a PBX system. The standard implementation Ozeki VoIP SIP SDK provides does not contain restrictions for the extension that want to register to the PBX, so extensions with the correct username and password could register. This example is about to introduce how to implement rules for the authentication in order to set which SIP accounts your PBX will accept for registration.

If you develop pbx applications with Ozeki VoIP SIP SDK, you can easily configure the authentication process, as well as so much other things. With creating authenticaion rules, you can set which extensions, clients are allowed to register, or you can even set privileges to each of them.

How to implement PBX authentication using C#?

The example program in this article shows how you can define authentication rules for the PBX extensions' registration. Ozeki VoIP SIP SDK provides the basic PBX functionalities but you can redefine any behavior by overriding the provided methods. In this example code, the registration authentication method is redefined for only accepting the registration of SIP accounts of a certain range.

The following program code uses the background support of Ozeki VoIP SIP SDK, therefore you will need to download and install Ozeki SIP SDK on your computer before starting to use the program code. You will also need to have Visual Studio 2012 or compatible IDE and .NET Framework installed on your system, as the program code below is written in C# language.

The authentication process works with checking the username and password the extension sends during the registration process. The PBX checks if the name-password pair is a valid one and if so, the registration will be allowed or it will be denied otherwise.

To restrict the authentications even if the username-password pair is valid, you have to override the OnAuthenticationRequest() method of the PBXBase class; you can set simple or even more complex terms to allow extensions to register, by only creating a few statements.
The method return with an AuthenticationResult value, which contains:

  • registration has been accepted: the extension as ISIPExtension object, and information about the register process as a RequestAuthenticationInfo object.
  • registration has been denied: a "false" boolean value to indicate that the registration has beed denied, and an optional string parameter: the reason of the rejection, which would be shown at the client softphone's status bar (if that is able to handle it).

In this example the PBX will allow registration only for the extension with the "1001" username. You can, of course set more restricted rules for the allowed extensions.

PBX authentication example in C#

using System;
using Ozeki.Network;
using Ozeki.VoIP;

namespace PBX_Authentication
{
    class MainClass
    {
        private static void Main(string[] args)
        {
            var myPBX = new MyPBX(NetworkAddressHelper.GetLocalIP().ToString(), 20000, 30000);
            myPBX.Start();

            Console.ReadLine();
            myPBX.Stop();
        }
    }


    class MyPBX : PBXBase
    {
        string localAddress;

        public MyPBX(string localAddress, int minPortRange, int maxPortRange)
            : base(minPortRange, maxPortRange)
        {
            this.localAddress = localAddress;
            Console.WriteLine("PBX starting...");
            Console.WriteLine("Local address: " + localAddress);
        }

        protected override void OnStart()
        {
            Console.WriteLine("PBX started.");
            SetListenPort(localAddress, 5060, TransportType.Udp);

            Console.WriteLine("Listened port: 5060(UDP)");
            base.OnStart();
        }

        protected override RegisterResult OnRegisterReceived(ISIPExtension extension, SIPAddress from, int expires)
        {
            Console.WriteLine("Register received from: " + extension.ExtensionID);
            return base.OnRegisterReceived(extension, from, expires);
        }

        protected override void OnUnregisterReceived(ISIPExtension extension)
        {
            Console.WriteLine("Unregister received from: " + extension.ExtensionID);
            base.OnUnregisterReceived(extension);
        }

        protected override void OnCallRequestReceived(ISessionCall call)
        {
            Console.WriteLine("Call request received. Caller: " + call.DialInfo.CallerID + " callee: " + call.DialInfo.Dialed);
            base.OnCallRequestReceived(call);
        }

        protected override AuthenticationResult OnAuthenticationRequest(ISIPExtension extension, RequestAuthenticationInfo requestAuthInfo)
        {
            if (extension.ExtensionID == "1001")
            {
                Console.WriteLine("Authentication accepted.");
                return new AuthenticationResult(true);
            }
            else
            {
                Console.WriteLine("Authentication denied!");
                return new AuthenticationResult(false, "Not allowed username.");  // second parameter is optional
            }
        }

    }
}

You can find a more complex example with a step-by-step guide at the Training chapter's VoIP PBX authentication article.


Related Pages

More information