Application Authentication

Introduction

Application authentication consists of restricting access to an application. You can first create an application and take care of authentication later, but it is better to design the concept before starting. In this series of articles, we are going to create a complete application, step by step, and in different sections that each deals with one particular issue.

Imagine you want to create an application for a department store. One of the things you can take care of, as far as people who access the program are concerned, is authentication.
In this example, we will create an application for file processing. We will use either Microsoft Visual Studio 2010 Professional or Microsoft Visual C# 2010 Express.

Practical LearningPractical Learning: Creating a Folder for the Application

  1. Log on to the server or the computer that will hold the records for the application
  2. Create a folder named Fun Department Store
  3. Right-click that folder and click Share

    Sharing a Folder
  4. Click the arrow of the combo box in the File Sharing window and select Everyone
  5. Click Add
  6. Click the down-pointing arrow on the right side of Everyone:
    • If you are sharing from Microsoft Windows Server 2008, select Contributor

      Sharing
    • If you are sharing from Microsoft Windows 7, select Read/Write
  7. Click Share

    Sharing
  8. Click Close
Practical LearningPractical Learning: Starting the Project

  1. Launch either Microsoft Visual C# Express or Microsoft Visual Studio
  2. To start an application, on the main menu, click File -> New Project ...
  3. If you are using Microsoft Visual Studio, in the left list, click Visual C#.
    In the middle list, click Empty Project
  4. Set the Name to FunDS1
  5. Click OK
  6. On the main menu, click Project -> FunDepartmentStore1 Properties...
  7. In the Output Type combo box, select Windows Application
  8. Close the Properties window
  9. On the main menu, click Project -> Add Reference...
  10. In the Add Reference dialog box, click .NET
  11. In the list view, click System.Data
  12. Press and hold Ctrl
  13. Click System
  14. Click System.Drawing
  15. Click System.Windows.Forms
  16. Click System.Xml
  17. Release Ctrl
  18. Click OK
  19. To save the project, on the Standard toolbar, click the Save All button
  20. Accept the suggested path but make a note of it (you will need it) and click OK
  21. To create a file for the project, on the main menu, click Project -> Add New Item...
  22. In the middle list, click Code File
  23. Set the name to Employee
  24. Click Add
  25. In the empty document, type the following:
    using System;
    
    [Serializable]
    public class Employee
    {
        public string EmployeeNumber { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Title { get; set; }
        public bool   Manager { get; set; }
        public double HourlySalary { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
    }
  26. To create a file for the project, on the main menu, click Project -> Add New Item...
  27. In the middle list, click Code File
  28. Set the name to StoreItem
  29. Click Add
  30. In the empty document, type the following:
    using System;
    
    [Serializable]
    public class StoreItem
    {
        public string ItemNumber { get; set; }
        public DateTime ArrivalDate { get; set; }
        public string Manufacturer { get; set; }
        public string Category { get; set; }
        public string SubCategory { get; set; }
        public string ItemName { get; set; }
        public string ItemSize { get; set; }
        public double UnitPrice { get; set; }
        public double DiscountRate { get; set; }
        public string SaleStatus { get; set; }
    }
  31. To create a new file, on the main menu, click Project -> Add File...
  32. In the middle list, click Code File
  33. Set the Name to StoreInventory
  34. Click Add
  35. In the empty document, type the following:
    using System;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using System.ComponentModel;
    
    public class StoreInventory : Form
    {
        private ColumnHeader colIndex;
        private ColumnHeader colItemNumber;
        private ColumnHeader colArrivalDate;
        private ColumnHeader colManufacturer;
        private ColumnHeader colCategory;
        private ColumnHeader colSubCategory;
        private ColumnHeader colItemName;
        private ColumnHeader colItemSize;
        private ColumnHeader colUnitPrice;
        private ColumnHeader colSaleStatus;
    
        private ListView lvwStoreItems;
    
        private Button btnClose;
    
        public StoreInventory()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            colIndex = new ColumnHeader();
            colItemNumber = new ColumnHeader();
            colArrivalDate = new ColumnHeader();
            colManufacturer = new ColumnHeader();
            colCategory = new ColumnHeader();
            colSubCategory = new ColumnHeader();
            colItemName = new ColumnHeader();
            colItemSize = new ColumnHeader();
            colUnitPrice = new ColumnHeader();
            colSaleStatus = new ColumnHeader();
    
            lvwStoreItems = new ListView();
            btnClose = new Button();
    
            SuspendLayout();
    
            colIndex.Text = "Index";
            colIndex.Width = 40;
    
            colItemNumber.Text = "Item #";
            colItemNumber.TextAlign = HorizontalAlignment.Center;
            colItemNumber.Width = 50;
    
            colArrivalDate.Text = "Arrival Date";
            colArrivalDate.Width = 70;
    
            colManufacturer.Text = "Manufacturer";
            colManufacturer.Width = 100;
    
            colCategory.Text = "Category";
            colSubCategory.Text = "Sub-Category";
            colSubCategory.Width = 80;
    
            colItemName.Text = "Item Name/Description";
            colItemName.Width = 220;
    
            colItemSize.Text = "Size";
            colItemSize.TextAlign = HorizontalAlignment.Center;
    
            colUnitPrice.Text = "Unit Price";
            colUnitPrice.TextAlign = HorizontalAlignment.Right;
    
            colSaleStatus.Text = "Status";
            colSaleStatus.Width = 70;
    
            btnClose.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            btnClose.Location = new Point(772, 182);
            btnClose.Size = new Size(75, 23);
            btnClose.TabIndex = 33;
            btnClose.Text = "Close";
            btnClose.UseVisualStyleBackColor = true;
            btnClose.Click += new System.EventHandler(btnCloseClicked);
    
            lvwStoreItems.Anchor = AnchorStyles.Top | AnchorStyles.Bottom |
                                   AnchorStyles.Left | AnchorStyles.Right;
            lvwStoreItems.Columns.AddRange(new ColumnHeader[] {
                colIndex, colItemNumber, colArrivalDate, colManufacturer,
                colCategory, colSubCategory, colItemName,
                colItemSize, colUnitPrice, colSaleStatus});
            lvwStoreItems.FullRowSelect = true;
            lvwStoreItems.GridLines = true;
            lvwStoreItems.Location = new Point(12, 12);
            lvwStoreItems.Size = new Size(835, 160);
            lvwStoreItems.TabIndex = 32;
            lvwStoreItems.UseCompatibleStateImageBehavior = false;
            lvwStoreItems.View = System.Windows.Forms.View.Details;
    
            ClientSize = new Size(859, 213);
            Controls.Add(btnClose);
            Controls.Add(lvwStoreItems);
            ShowInTaskbar = false;
            StartPosition = FormStartPosition.CenterScreen;
            Text = "Fun Department Store - Current Store Inventory";
            Load += new System.EventHandler(StoreInventoryLoaded);
    
            ResumeLayout(false);
        }
    
        private void ShowInventory()
        {
    
        }
    
        private void StoreInventoryLoaded(object sender, EventArgs e)
        {
            ShowInventory();
        }
    
        private void btnCloseClicked(object sender, EventArgs e)
        {
            Close();
        }
    }
  36. To create a new file, on the main menu, click Project -> Add New Item...
  37. In the middle list, click Code File
  38. Set the Name to Switchboard
  39. Click Add
  40. In the empty document, type the following:
    using System;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using System.ComponentModel;
    
    public class Switchboard : Form
    {
        private Button btnStoreInventory;
        private Button btnCreateStoreItem;
        private Button btnEmployees;
        private Button btnLogInUser;
        private Button btnClose;
    
        public Switchboard()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            btnStoreInventory = new Button();
            btnCreateStoreItem = new Button();
            btnEmployees = new Button();
            btnLogInUser = new Button();
            btnClose = new Button();
    
            SuspendLayout();
            
            btnStoreInventory.Font = new Font("Georgia", 18F,
                FontStyle.Bold, GraphicsUnit.Point, 0);
            btnStoreInventory.Location = new Point(12, 12);
            btnStoreInventory.Size = new Size(227, 98);
            btnStoreInventory.TabIndex = 23;
            btnStoreInventory.Text = "View Store Inventory";
            btnStoreInventory.UseVisualStyleBackColor = true;
            btnStoreInventory.Click +=
                new System.EventHandler(btnStoreInventoryClicked);
    
            btnCreateStoreItem.Font = new Font("Georgia", 18F,
                FontStyle.Bold, GraphicsUnit.Point, 0);
            btnCreateStoreItem.Location = new Point(255, 13);
            btnCreateStoreItem.Size = new Size(227, 98);
            btnCreateStoreItem.TabIndex = 25;
            btnCreateStoreItem.Text = "Create Store Item";
            btnCreateStoreItem.UseVisualStyleBackColor = true;
            btnCreateStoreItem.Click +=
                new System.EventHandler(btnCreateStoreItemClicked);
    
            btnEmployees.Font = new Font("Georgia", 18F,
                FontStyle.Bold, GraphicsUnit.Point, 0);
            btnEmployees.Location = new Point(12, 128);
            btnEmployees.Size = new Size(227, 98);
            btnEmployees.TabIndex = 27;
            btnEmployees.Text = "Employees";
            btnEmployees.UseVisualStyleBackColor = true;
            btnEmployees.Click +=
                new System.EventHandler(btnEmployeesClicked);
    
            btnLogInUser.Font = new Font("Georgia", 18F,
                FontStyle.Bold, GraphicsUnit.Point, 0);
            btnLogInUser.Location = new Point(255, 128);
            btnLogInUser.Size = new Size(227, 98);
            btnLogInUser.TabIndex = 26;
            btnLogInUser.Text = "Log in as a Different Employee";
            btnLogInUser.UseVisualStyleBackColor = true;
            btnLogInUser.Click +=
                new System.EventHandler(btnLogInUserClicked);
    
            btnClose.Font = new Font("Georgia", 18F,
                FontStyle.Bold, GraphicsUnit.Point, 0);
            btnClose.Location = new Point(11, 241);
            btnClose.Size = new Size(471, 67);
            btnClose.TabIndex = 24;
            btnClose.Text = "Close Application";
            btnClose.UseVisualStyleBackColor = true;
            btnClose.Click += new System.EventHandler(btnCloseClicked);
    
            ClientSize = new Size(494, 319);
            Controls.Add(btnEmployees);
            Controls.Add(btnLogInUser);
            Controls.Add(btnCreateStoreItem);
            Controls.Add(btnClose);
            Controls.Add(btnStoreInventory);
            Name = "Switchboard";
            StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            Text = "Fun Department Store";
    
            ResumeLayout(false);
        }
    
        private void btnStoreInventoryClicked(object sender, EventArgs e)
        {
            StoreInventory si = new StoreInventory();
            si.ShowDialog();
        }
    
        private void btnCreateStoreItemClicked(object sender, EventArgs e)
        {
    
        }
    
        private void btnEmployeesClicked(object sender, EventArgs e)
        {
    
        }
    
        private void btnLogInUserClicked(object sender, EventArgs e)
        {
    
        }
    
        private void btnCloseClicked(object sender, EventArgs e)
        {
            Close();
        }
    }
    
    public class DepartmentStore
    {
        public static int Main()
        {
            Application.Run(new Switchboard());
            return 0;
        }
    }
  41. To execute the application, press F5
  42. Click the View Store Inventory button
  43. Close the Store Inventory form
  44. Close the Switchboard form
  45. To add a new file, on the main menu, click Project -> New Item...
  46. In the middle list, click Code File
  47. Change the Name to CreateStoreItem
  48. Click Add
  49. In the empty document, type the following:
    using System;
    using System.IO;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Collections.Generic;
    using System.Runtime.Serialization.Formatters.Binary;
    
    public class CreateStoreItem : Form
    {
        private Label lblItemNumber;
        private TextBox txtItemNumber;
        private Label lblArrivalDate;
        private MaskedTextBox txtArrivalDate;
        private Label lblManufacturer;
        private TextBox txtManufacturer;
        private Label lblCategory;
        private ComboBox cbxCategories;
        private Label lblSubCategory;
        private ComboBox cbxSubCategories;
        private Label lblItemName;
        private TextBox txtItemName;
        private Label lblItemSize;
        private TextBox txtItemSize;
        private Label lblUnitPrice;
        private TextBox txtUnitPrice;
        private Label lblDiscountRate;
        private TextBox txtDiscountRate;
        private Label lblPercent;
        private Label lblSaleStatus;
        private ComboBox cbxSaleStatus;
    
        private Button btnReset;
        private Button btnCreate;
        private Button btnClose;
    
        public CreateStoreItem()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            lblItemNumber = new Label();
            txtItemNumber = new TextBox();
            lblArrivalDate = new Label();
            txtArrivalDate = new MaskedTextBox();
            lblManufacturer = new Label();
            txtManufacturer = new TextBox();
            lblCategory = new Label();
            cbxCategories = new ComboBox();
            lblSubCategory = new Label();
            cbxSubCategories = new ComboBox();
            lblItemName = new Label();
            txtItemName = new TextBox();
            lblItemSize = new Label();
            txtItemSize = new TextBox();
            lblUnitPrice = new Label();
            txtUnitPrice = new TextBox();
            txtDiscountRate = new TextBox();
            lblDiscountRate = new Label();
            lblPercent = new Label();
            lblSaleStatus = new Label();
            cbxSaleStatus = new ComboBox();
    
            btnClose = new Button();
            btnCreate = new Button();
            btnReset = new Button();
            
            SuspendLayout();
    
            lblItemNumber.AutoSize = true;
            lblItemNumber.Location = new Point(11, 14);
            lblItemNumber.Size = new Size(40, 13);
            lblItemNumber.TabIndex = 0;
            lblItemNumber.Text = "Item #:";
    
            txtItemNumber.Location = new Point(95, 11);
            txtItemNumber.Size = new Size(96, 20);
            txtItemNumber.TabIndex = 1;
    
            lblArrivalDate.AutoSize = true;
            lblArrivalDate.Location = new Point(223, 14);
            lblArrivalDate.Size = new Size(65, 13);
            lblArrivalDate.TabIndex = 2;
            lblArrivalDate.Text = "Arrival Date:";
    
            txtArrivalDate.Location = new Point(303, 11);
            txtArrivalDate.Mask = "00/00/0000";
            txtArrivalDate.Size = new Size(121, 20);
            txtArrivalDate.TabIndex = 3;
            txtArrivalDate.ValidatingType = typeof(System.DateTime);
    
            lblManufacturer.AutoSize = true;
            lblManufacturer.Location = new Point(11, 43);
            lblManufacturer.Size = new Size(73, 13);
            lblManufacturer.TabIndex = 4;
            lblManufacturer.Text = "Manufacturer:";
    
            txtManufacturer.Location = new Point(95, 40);
            txtManufacturer.Size = new Size(330, 20);
            txtManufacturer.TabIndex = 5;
    
            lblCategory.AutoSize = true;
            lblCategory.Location = new Point(11, 72);
            lblCategory.Size = new Size(52, 13);
            lblCategory.TabIndex = 6;
            lblCategory.Text = "Category:";
    
            cbxCategories.FormattingEnabled = true;
            cbxCategories.Items.AddRange(new object[] {
                "Men", "Girls", "Boys",
                "Babies", "Women", "Other"});
            cbxCategories.Location = new Point(95, 70);
            cbxCategories.Size = new Size(116, 21);
            cbxCategories.TabIndex = 7;
    
            lblSubCategory.AutoSize = true;
            lblSubCategory.Location = new Point(223, 73);
            lblSubCategory.Size = new Size(74, 13);
            lblSubCategory.TabIndex = 8;
            lblSubCategory.Text = "Sub-Category:";
    
            cbxSubCategories.FormattingEnabled = true;
            cbxSubCategories.Items.AddRange(new object[] {
                "Skirts", "Pants", "Shirts", "Shoes",
                "Blouse", "Beauty", "Dresses", "Clothing",
                "Sweater", "Watches", "Handbags", "Miscellaneous"});
            cbxSubCategories.Location = new Point(303, 70);
            cbxSubCategories.Size = new Size(121, 21);
            cbxSubCategories.TabIndex = 9;
    
            lblItemName.AutoSize = true;
            lblItemName.Location = new Point(11, 104);
            lblItemName.Size = new Size(61, 13);
            lblItemName.TabIndex = 10;
            lblItemName.Text = "Item Name:";
    
            txtItemName.Location = new Point(95, 100);
            txtItemName.Size = new Size(329, 20);
            txtItemName.TabIndex = 11;
    
            lblItemSize.AutoSize = true;
            lblItemSize.Location = new Point(12, 132);
            lblItemSize.Size = new Size(30, 13);
            lblItemSize.TabIndex = 12;
            lblItemSize.Text = "Size:";
    
            txtItemSize.Location = new Point(95, 129);
            txtItemSize.Size = new Size(96, 20);
            txtItemSize.TabIndex = 13;
    
            lblUnitPrice.AutoSize = true;
            lblUnitPrice.Location = new Point(224, 135);
            lblUnitPrice.Size = new Size(56, 13);
            lblUnitPrice.TabIndex = 14;
            lblUnitPrice.Text = "Unit Price:";
    
            txtUnitPrice.Location = new Point(304, 132);
            txtUnitPrice.Size = new Size(121, 20);
            txtUnitPrice.TabIndex = 15;
            txtUnitPrice.Text = "0.00";
            txtUnitPrice.TextAlign = HorizontalAlignment.Right;
    
            lblDiscountRate.AutoSize = true;
            lblDiscountRate.Location = new Point(11, 161);
            lblDiscountRate.Size = new Size(78, 13);
            lblDiscountRate.TabIndex = 16;
            lblDiscountRate.Text = "Discount Rate:";
    
            txtDiscountRate.Location = new Point(95, 158);
            txtDiscountRate.Size = new Size(96, 20);
            txtDiscountRate.TabIndex = 17;
            txtDiscountRate.Text = "0.00";
            txtDiscountRate.TextAlign = HorizontalAlignment.Right;
    
            lblPercent.AutoSize = true;
            lblPercent.Font = new Font("Microsoft Sans Serif", 9.75F,
                                       FontStyle.Bold,
                                       GraphicsUnit.Point, ((byte)(0)));
            lblPercent.Location = new Point(191, 161);
            lblPercent.Size = new Size(21, 16);
            lblPercent.TabIndex = 18;
            lblPercent.Text = "%";
    
            lblSaleStatus.AutoSize = true;
            lblSaleStatus.Location = new Point(224, 164);
            lblSaleStatus.Size = new Size(64, 13);
            lblSaleStatus.TabIndex = 19;
            lblSaleStatus.Text = "Sale Status:";
    
            cbxSaleStatus.FormattingEnabled = true;
            cbxSaleStatus.Items.AddRange(new object[] {
                "Sold", "In Stock",
                "On Display", "Processing", "Other"});
            cbxSaleStatus.Location = new Point(304, 161);
            cbxSaleStatus.Size = new Size(121, 21);
            cbxSaleStatus.TabIndex = 20;
    
            btnReset.Location = new Point(125, 193);
            btnReset.Size = new Size(95, 23);
            btnReset.TabIndex = 23;
            btnReset.Text = "Reset";
            btnReset.UseVisualStyleBackColor = true;
            btnReset.Click += new System.EventHandler(btnResetClicked);
    
            btnCreate.Location = new Point(226, 193);
            btnCreate.Size = new Size(108, 23);
            btnCreate.TabIndex = 21;
            btnCreate.Text = "Create";
            btnCreate.UseVisualStyleBackColor = true;
            btnCreate.Click += new System.EventHandler(btnCreateClicked);
    
            btnClose.Location = new Point(340, 193);
            btnClose.Size = new Size(83, 23);
            btnClose.TabIndex = 22;
            btnClose.Text = "Close";
            btnClose.UseVisualStyleBackColor = true;
            btnClose.Click += new System.EventHandler(btnCloseClicked);
    
            ClientSize = new Size(437, 230);
            Controls.Add(txtManufacturer);
            Controls.Add(lblPercent);
            Controls.Add(txtDiscountRate);
            Controls.Add(lblDiscountRate);
            Controls.Add(txtArrivalDate);
            Controls.Add(lblArrivalDate);
            Controls.Add(btnClose);
            Controls.Add(btnCreate);
            Controls.Add(btnReset);
            Controls.Add(txtUnitPrice);
            Controls.Add(lblUnitPrice);
            Controls.Add(cbxSaleStatus);
            Controls.Add(lblSaleStatus);
            Controls.Add(txtItemSize);
            Controls.Add(lblItemSize);
            Controls.Add(txtItemName);
            Controls.Add(lblItemName);
            Controls.Add(cbxSubCategories);
            Controls.Add(lblSubCategory);
            Controls.Add(cbxCategories);
            Controls.Add(lblCategory);
            Controls.Add(lblMan

No comments:

Post a Comment

Related Scripting

Related Posts Plugin for WordPress, Blogger...

Disqus for Functions