Ozeki Camera Recorder Stat

How to create camera users and modify their passwords, how to delete a camera user in C#

This example demonstrates how you can create, delete camera users and how you can modify the passwords of your users in C#. To implement this example, you need to have OZEKI Camera SDK installed, and a reference to OzekiSDK.dll should be added to your Visual Studio project.

How to create camera users and modify their passwords, how to delete a camera user using C#?

To establish the connection properly between your application and an IP camera you should apply the same code snippet what you have used in the example (How to connect to an IP camera device using C#?). Important: you should study this article in order to find out how to setup your Windows Forms/WPF Application correctly.

Getting started

To get started it is recomended to Download and Install the latest version of Ozeki Camera SDK. After installation you can find the example code discussed in this page with full source code in the following location on your harddisk:

Download Ozeki Camera SDK: http://www.camera-sdk.com/p_13-download-camera-onvif.html
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
Configure_Camera_Users_WF\Configure_Camera_Users_WF.sln
WPF version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
Configure_Camera_Users_WPF\Configure_Camera_Users_WPF.sln

To compile this example you will need Microsoft Visual Studio installed on your computer.

In addition to the methods discussed in the previous tutorials the additional methods of this example are the following:

First of all, when the camera connected, you should fill the ComboBox_UserList and ComboBox_Role comboboxes by selecting an user and a role from the comboboxes.:

if (e.State == IPCameraState.Connected)
FillComboboxes();

In the FillComboboxes() method you should query the elements of the CameraUserLevel list and get the users of your camera with the GetUserList() method:

Combobox_Role.DataSource = Enum.GetValues(typeof(CameraUserLevel));
var userList = _camera.UserManager.GetUsersList();

ComboBox_UserList.DataSource = userList;

You can set and display the actual user in the combobox with the following statement:

var user = userList.SingleOrDefault(item => item.UserName == _camera.UserName);
ComboBox_UserList.SelectedItem = user;

Whenever the selected user has been changed, you should refresh the Combobox_Role.Text attribute with the role of this user that can be done in the ComboBox_UserList_SelectedIndexChanged method. This method will be automatically called when the current user changed in the combobox:

var user = (CameraUser)ComboBox_UserList.SelectedItem;
Combobox_Role.SelectedItem = user.UserLevel;

In addition, the application has three more functions, the click events of the buttons Add, Modify and Delete.

When you click on the Add button, first you should check whether the given necessary informations are correct then you should create a new CameraUser and set role, username and password to this new user. Finally, you should add this user to your camera's UserManager property with the AddCameraUser() method and fill the comboboxes again with the new data:

var role = (CameraUserLevel)Combobox_Role.SelectedItem;
var newUser = new CameraUser(role)
            {
                UserName = TextBox_UserName.Text,
                Password = TextBox_Password.Text
            };
             _camera.UserManager.AddCameraUser(newUser);
             FillComboboxes();

In the Modify button's click event, you should check whether the given necessary informations are correct then you should create a new CameraUser and set role, username and password to this new user. In this case, you should call the ModifyCameraUser() method and this method's first parameter will be the old user's username, the second parameter will be the new username:

var role = (CameraUserLevel)Combobox_Role.SelectedItem;
var user = (CameraUser)ComboBox_UserList.SelectedItem;
var modifyUser = new CameraUser(role)
            {
                UserName = TextBox_UserName.Text,

                Password = TextBox_Password.Text
            };
            _camera.UserManager.ModifyCameraUser(user.UserName, modifyUser);
            FillComboboxes();

When you press the Delete button, the program deletes the selected user with the RemoveCameraUser() method than it's going to fill the comboboxes again with the new data.

var user = (CameraUser)ComboBox_UserList.SelectedItem;
_camera.UserManager.RemoveCameraUser(user.UserName);
FillComboboxes();

Create camera users and modify their passwords, delete a camera user example in C#

Windows Form WPF  

Form1.cs

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Ozeki.Media;
using Ozeki.Camera;


namespace ConfigureOnvifCameraRemotely06
{
    public partial class Form1 : Form
    {
        private IIPCamera _camera;
        private DrawingImageProvider _imageProvider;
        private MediaConnector _connector;
        private VideoViewerWF _videoViewerWf;

        public Form1()
        {
            InitializeComponent();
            _connector = new MediaConnector();
            _imageProvider = new DrawingImageProvider();
            _videoViewerWf = new VideoViewerWF();
            SetVideoViewer();
        }

        private void SetVideoViewer()
        {
            CameraBox.Controls.Add(_videoViewerWf);
            _videoViewerWf.Size = new Size(260, 180);
            _videoViewerWf.BackColor = Color.Black;
            _videoViewerWf.TabStop = false;
            _videoViewerWf.FlipMode = FlipMode.None;
            _videoViewerWf.Location = new Point(30, 30);
            _videoViewerWf.Name = "_videoViewerWf";
        }

        private void button_Connect_Click(object sender, EventArgs e)
        {
            _camera = new IPCamera("192.168.112.109:8080", "user", "qwe123");
            _camera.CameraStateChanged += _camera_CameraStateChanged;
            _connector.Connect(_camera.VideoChannel, _imageProvider);
            _videoViewerWf.SetImageProvider(_imageProvider);
            _videoViewerWf.Start();
            _camera.Start();
        }

        private void _camera_CameraStateChanged(object sender, CameraStateEventArgs e)
        {
            if (e.State == CameraState.Connected)
                FillComboboxes();
        }

        private void FillComboboxes()
        {
            InvokeGuiThread(() =>
            {
                Combobox_Role.DataSource = Enum.GetValues(typeof(CameraUserLevel));
                var userList = _camera.UserManager.GetUsersList();
                ComboBox_UserList.DataSource = userList;
                var user = userList.SingleOrDefault(item => item.UserName == _camera.UserName);
                ComboBox_UserList.SelectedItem = user;
            });
        }

        private void ComboBox_UserList_SelectedIndexChanged(object sender, EventArgs e)
        {
            var user = (CameraUser)ComboBox_UserList.SelectedItem;
            Combobox_Role.SelectedItem = user.UserLevel;
        }

        private void Button_AddUser_Click(object sender, EventArgs e)
        {
            var role = (CameraUserLevel)Combobox_Role.SelectedItem;
            if (String.IsNullOrEmpty(TextBox_UserName.Text))
            {
                label_Status.Text = "Username is empty!";
                return;
            }

            if (String.IsNullOrEmpty(TextBox_Password.Text) || !TextBox_Password.Text.Equals(TextBox_Password2.Text))
            {
                label_Status.Text = "The passwords are different or empty!";
                return;
            }

            var newUser = new CameraUser(role)
            {
                UserName = TextBox_UserName.Text,
                Password = TextBox_Password.Text
            };
            _camera.UserManager.AddCameraUser(newUser);
            FillComboboxes();
            label_Status.Text = "User: " + newUser.UserName + " has been added!";
        }

        private void Button_ModifyUser_Click(object sender, EventArgs e)
        {
            var role = (CameraUserLevel)Combobox_Role.SelectedItem;
            var user = (CameraUser)ComboBox_UserList.SelectedItem;

            if (String.IsNullOrEmpty(TextBox_UserName.Text))
            {
                label_Status.Text = "Username is empty!";
                return;
            }

            if (String.IsNullOrEmpty(TextBox_Password.Text) || !TextBox_Password.Text.Equals(TextBox_Password2.Text))
            {
                label_Status.Text = "The passwords are different or empty!";
                return;
            }

            var modifyUser = new CameraUser(role)
            {
                UserName = TextBox_UserName.Text,

                Password = TextBox_Password.Text
            };
            _camera.UserManager.ModifyCameraUser(user.UserName, modifyUser);
            FillComboboxes();
            label_Status.Text = "User: " + user.UserName + " has been modified!";
        }

        private void Button_DeleteUser_Click(object sender, EventArgs e)
        {
            var user = (CameraUser)ComboBox_UserList.SelectedItem;

            if (user == null || TextBox_UserName.Text != user.UserName)
            {
                label_Status.Text = "Username is wrong!";
                return;
            }
            _camera.UserManager.RemoveCameraUser(user.UserName);
            FillComboboxes();
            label_Status.Text = "User: " + user.UserName + " has been deleted!";
        }

        private void InvokeGuiThread(Action action)
        {
            BeginInvoke(action);
        }
    }
}
	

Code 1 - Create camera users and modify their passwords, delete a camera user example in C#

Please note that none of the cancel and disconnect methods are included in the example because of the demonstrating intent and briefness of the article.

GUI

The Graphical User Interface of an application for creating camera users, modifying their passwords and deleting camera users in C#
Figure 1 - The graphical user interface of your application

After the successful implementation of the functions and the GUI elements, the application will work properly. Pressing the connect button will load in the image of the IP camera device connected to your PC into the panel that you can see on the picture.

Below you can find the code that belongs to the interface of the previously presented application. With the help of this section your Windows Forms Application will be able to work properly.

Form1.Designer.cs

	using System.Windows.Forms;
	
	namespace ConfigureOnvifCameraRemotely06
	{
	    partial class Form1
	    {
	        private System.ComponentModel.IContainer components = null;
	
	        protected override void Dispose(bool disposing)
	        {
	            if (disposing && (components != null))
	            {
	                components.Dispose();
	            }
	            base.Dispose(disposing);
	        }
	
	        private void InitializeComponent()
	        {
	            this.groupBox1 = new System.Windows.Forms.GroupBox();
	            this.button_Connect = new System.Windows.Forms.Button();
	            this.CameraBox = new System.Windows.Forms.GroupBox();
	            this.groupBox21 = new System.Windows.Forms.GroupBox();
	            this.label_Status = new System.Windows.Forms.Label();
	            this.label1 = new System.Windows.Forms.Label();
	            this.Combobox_Role = new System.Windows.Forms.ComboBox();
	            this.label39 = new System.Windows.Forms.Label();
	            this.Button_DeleteUser = new System.Windows.Forms.Button();
	            this.Button_ModifyUser = new System.Windows.Forms.Button();
	            this.Button_AddUser = new System.Windows.Forms.Button();
	            this.TextBox_Password2 = new System.Windows.Forms.TextBox();
	            this.label38 = new System.Windows.Forms.Label();
	            this.TextBox_Password = new System.Windows.Forms.TextBox();
	            this.label37 = new System.Windows.Forms.Label();
	            this.ComboBox_UserList = new System.Windows.Forms.ComboBox();
	            this.TextBox_UserName = new System.Windows.Forms.TextBox();
	            this.label36 = new System.Windows.Forms.Label();
	            this.label35 = new System.Windows.Forms.Label();
	            this.groupBox1.SuspendLayout();
	            this.groupBox21.SuspendLayout();
	            this.SuspendLayout();
	            // 
	            // groupBox1
	            // 
	            this.groupBox1.Controls.Add(this.button_Connect);
	            this.groupBox1.Location = new System.Drawing.Point(10, 10);
	            this.groupBox1.Name = "groupBox1";
	            this.groupBox1.Size = new System.Drawing.Size(100, 60);
	            this.groupBox1.TabIndex = 0;
	            this.groupBox1.TabStop = false;
	            this.groupBox1.Text = "Connect";
	            // 
	            // button_Connect
	            // 
	            this.button_Connect.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
	            this.button_Connect.ForeColor = System.Drawing.Color.Black;
	            this.button_Connect.Location = new System.Drawing.Point(10, 20);
	            this.button_Connect.Name = "button_Connect";
	            this.button_Connect.Size = new System.Drawing.Size(75, 25);
	            this.button_Connect.TabIndex = 6;
	            this.button_Connect.Text = "Connect";
	            this.button_Connect.UseVisualStyleBackColor = true;
	            this.button_Connect.Click += new System.EventHandler(this.button_Connect_Click);
	            // 
	            // CameraBox
	            // 
	            this.CameraBox.Location = new System.Drawing.Point(10, 80);
	            this.CameraBox.Name = "CameraBox";
	            this.CameraBox.Size = new System.Drawing.Size(325, 230);
	            this.CameraBox.TabIndex = 3;
	            this.CameraBox.TabStop = false;
	            this.CameraBox.Text = "Live camera ";
	            // 
	            // groupBox21
	            // 
	            this.groupBox21.Controls.Add(this.label_Status);
	            this.groupBox21.Controls.Add(this.label1);
	            this.groupBox21.Controls.Add(this.Combobox_Role);
	            this.groupBox21.Controls.Add(this.label39);
	            this.groupBox21.Controls.Add(this.Button_DeleteUser);
	            this.groupBox21.Controls.Add(this.Button_ModifyUser);
	            this.groupBox21.Controls.Add(this.Button_AddUser);
	            this.groupBox21.Controls.Add(this.TextBox_Password2);
	            this.groupBox21.Controls.Add(this.label38);
	            this.groupBox21.Controls.Add(this.TextBox_Password);
	            this.groupBox21.Controls.Add(this.label37);
	            this.groupBox21.Controls.Add(this.ComboBox_UserList);
	            this.groupBox21.Controls.Add(this.TextBox_UserName);
	            this.groupBox21.Controls.Add(this.label36);
	            this.groupBox21.Controls.Add(this.label35);
	            this.groupBox21.Location = new System.Drawing.Point(10, 320);
	            this.groupBox21.Name = "groupBox21";
	            this.groupBox21.Size = new System.Drawing.Size(325, 200);
	            this.groupBox21.TabIndex = 9;
	            this.groupBox21.TabStop = false;
	            this.groupBox21.Text = "User settings";
	            // 
	            // label_Status
	            // 
	            this.label_Status.AutoSize = true;
	            this.label_Status.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
	            this.label_Status.Location = new System.Drawing.Point(105, 170);
	            this.label_Status.Name = "label_Status";
	            this.label_Status.Size = new System.Drawing.Size(2, 15);
	            this.label_Status.TabIndex = 15;
	            // 
	            // label1
	            // 
	            this.label1.AutoSize = true;
	            this.label1.Location = new System.Drawing.Point(10, 170);
	            this.label1.Name = "label1";
	            this.label1.Size = new System.Drawing.Size(40, 13);
	            this.label1.TabIndex = 14;
	            this.label1.Text = "Status:";
	            // 
	            // Combobox_Role
	            // 
	            this.Combobox_Role.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
	            this.Combobox_Role.FormattingEnabled = true;
	            this.Combobox_Role.Location = new System.Drawing.Point(105, 47);
	            this.Combobox_Role.Name = "Combobox_Role";
	            this.Combobox_Role.Size = new System.Drawing.Size(120, 21);
	            this.Combobox_Role.TabIndex = 13;
	            // 
	            // label39
	            // 
	            this.label39.AutoSize = true;
	            this.label39.Location = new System.Drawing.Point(10, 50);
	            this.label39.Name = "label39";
	            this.label39.Size = new System.Drawing.Size(32, 13);
	            this.label39.TabIndex = 12;
	            this.label39.Text = "Role:";
	            // 
	            // Button_DeleteUser
	            // 
	            this.Button_DeleteUser.Location = new System.Drawing.Point(240, 77);
	            this.Button_DeleteUser.Name = "Button_DeleteUser";
	            this.Button_DeleteUser.Size = new System.Drawing.Size(75, 23);
	            this.Button_DeleteUser.TabIndex = 11;
	            this.Button_DeleteUser.Text = "Delete";
	            this.Button_DeleteUser.UseVisualStyleBackColor = true;
	            this.Button_DeleteUser.Click += new System.EventHandler(this.Button_DeleteUser_Click);
	            // 
	            // Button_ModifyUser
	            // 
	            this.Button_ModifyUser.Location = new System.Drawing.Point(240, 47);
	            this.Button_ModifyUser.Name = "Button_ModifyUser";
	            this.Button_ModifyUser.Size = new System.Drawing.Size(75, 23);
	            this.Button_ModifyUser.TabIndex = 10;
	            this.Button_ModifyUser.Text = "Modify";
	            this.Button_ModifyUser.UseVisualStyleBackColor = true;
	            this.Button_ModifyUser.Click += new System.EventHandler(this.Button_ModifyUser_Click);
	            // 
	            // Button_AddUser
	            // 
	            this.Button_AddUser.Location = new System.Drawing.Point(240, 17);
	            this.Button_AddUser.Name = "Button_AddUser";
	            this.Button_AddUser.Size = new System.Drawing.Size(75, 23);
	            this.Button_AddUser.TabIndex = 9;
	            this.Button_AddUser.Text = "Add";
	            this.Button_AddUser.UseVisualStyleBackColor = true;
	            this.Button_AddUser.Click += new System.EventHandler(this.Button_AddUser_Click);
	            // 
	            // TextBox_Password2
	            // 
	            this.TextBox_Password2.Location = new System.Drawing.Point(105, 137);
	            this.TextBox_Password2.Name = "TextBox_Password2";
	            this.TextBox_Password2.Size = new System.Drawing.Size(120, 20);
	            this.TextBox_Password2.TabIndex = 8;
	            // 
	            // label38
	            // 
	            this.label38.AutoSize = true;
	            this.label38.Location = new System.Drawing.Point(10, 140);
	            this.label38.Name = "label38";
	            this.label38.Size = new System.Drawing.Size(93, 13);
	            this.label38.TabIndex = 7;
	            this.label38.Text = "Repeat password:";
	            // 
	            // TextBox_Password
	            // 
	            this.TextBox_Password.Location = new System.Drawing.Point(105, 107);
	            this.TextBox_Password.Name = "TextBox_Password";
	            this.TextBox_Password.Size = new System.Drawing.Size(120, 20);
	            this.TextBox_Password.TabIndex = 6;
	            // 
	            // label37
	            // 
	            this.label37.AutoSize = true;
	            this.label37.Location = new System.Drawing.Point(10, 110);
	            this.label37.Name = "label37";
	            this.label37.Size = new System.Drawing.Size(56, 13);
	            this.label37.TabIndex = 5;
	            this.label37.Text = "Password:";
	            // 
	            // ComboBox_UserList
	            // 
	            this.ComboBox_UserList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
	            this.ComboBox_UserList.FormattingEnabled = true;
	            this.ComboBox_UserList.Location = new System.Drawing.Point(105, 17);
	            this.ComboBox_UserList.Name = "ComboBox_UserList";
	            this.ComboBox_UserList.Size = new System.Drawing.Size(120, 21);
	            this.ComboBox_UserList.TabIndex = 4;
	            this.ComboBox_UserList.SelectedIndexChanged += new System.EventHandler(this.ComboBox_UserList_SelectedIndexChanged);
	            // 
	            // TextBox_UserName
	            // 
	            this.TextBox_UserName.Location = new System.Drawing.Point(105, 77);
	            this.TextBox_UserName.Name = "TextBox_UserName";
	            this.TextBox_UserName.Size = new System.Drawing.Size(120, 20);
	            this.TextBox_UserName.TabIndex = 3;
	            // 
	            // label36
	            // 
	            this.label36.AutoSize = true;
	            this.label36.Location = new System.Drawing.Point(10, 80);
	            this.label36.Name = "label36";
	            this.label36.Size = new System.Drawing.Size(58, 13);
	            this.label36.TabIndex = 2;
	            this.label36.Text = "Username:";
	            // 
	            // label35
	            // 
	            this.label35.AutoSize = true;
	            this.label35.Location = new System.Drawing.Point(10, 20);
	            this.label35.Name = "label35";
	            this.label35.Size = new System.Drawing.Size(67, 13);
	            this.label35.TabIndex = 0;
	            this.label35.Text = "Current user:";
	            // 
	            // Form1
	            // 
	            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
	            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
	            this.ClientSize = new System.Drawing.Size(354, 534);
	            this.Controls.Add(this.groupBox21);
	            this.Controls.Add(this.CameraBox);
	            this.Controls.Add(this.groupBox1);
	            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
	            this.MaximizeBox = false;
	            this.Name = "Form1";
	            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
	            this.Text = "Configuring Onvif Camera Users";
	            this.groupBox1.ResumeLayout(false);
	            this.groupBox21.ResumeLayout(false);
	            this.groupBox21.PerformLayout();
	            this.ResumeLayout(false);
	
	        }
	
	        private System.Windows.Forms.GroupBox groupBox1;
	        private System.Windows.Forms.Button button_Connect;
	        private System.Windows.Forms.GroupBox CameraBox;
	        private GroupBox groupBox21;
	        private ComboBox Combobox_Role;
	        private Label label39;
	        private Button Button_DeleteUser;
	        private Button Button_ModifyUser;
	        private Button Button_AddUser;
	        private TextBox TextBox_Password2;
	        private Label label38;
	        private TextBox TextBox_Password;
	        private Label label37;
	        private ComboBox ComboBox_UserList;
	        private TextBox TextBox_UserName;
	        private Label label36;
	        private Label label35;
	        private Label label_Status;
	        private Label label1;
	
	    }
	}
	

Code 2 - GUI example in C#

DISCLAIMER: Please note that the following features will only work if your IP camera supports the given function. You should check the user manual of your IP camera to make sure it supports the feature that you wish to implement in C#.

Related Pages

FAQ

Below you can find the answers for the most frequently asked questions related to this topic:

  1. How can I get the URL of the camera?

    You can get the URL from the producer of the camera. (In the 10th tutorial you can find information on how to create an own IP camera discoverer program.)

  2. I have not managed to build the solution. How to solve it?

    • Please set the Target framework property of the project to .NET 4.0.
    • You should add the System.Drawing.dll and the OzekiSDK.dll to the references of the solution.
    • Please import the missing classes.