This site was last updated:Monday 13 October 2008, 11:30 GMT

Educational Community

AUTHENTICATING USERS IN C#
(3 votes, average: 5.00 out of 5)
Written by Christopher   
Thursday, 12 June 2008 18:15

INTRODUCTION

Unauthorized access of sensitive data must be prohibited in a business application. Unauthorized users must not be able to view other people’s information. In a windows program, sometimes it is necessary to provide different information in guests and users. A guest should not be able to see important data of the program’s flow. The authentication methods require the System.Security.Principal namespace.

AUTHENTICATION IN C#

On a network authentication is accomplished by the username/password concept. This allows for authentication of the user’s identify and for authorization of your privileges. The .Net environment provided the System.Security.Principal.WindowsIdentity class that represents a Windows user account. The class provides access to the current user’s name, authentication type and account token. To create an instance of this class you just have to call one of these methods:

·         GetAnonymous: Returns a WindowsIdentity object that represents an anonymous user.

·         GetCurrent: Returns a WindowsIdentity object that represents the current windows user. You can use this method to investigate the current user’s memberships and privileges.

·         Impersonate: Returns a WindowsImpersonationContext object that represents a specified user on the system. You can use it to impersonate a particular user account.

After creating the WindowsIdentity  object you can access several properties that provide information:

·         AuthenticationType:  A string representing the authentication type.

·         IsAnonymous: A Boolean value that is set to true when the user is anonymous.

·         IsAuthenticated: A Boolean value that is set to true when the user is authenticated.

·         IsGuest: A Boolean value that is set to true if the user is a guest.

·         IsSystem: A Boolean value that is set to true if the user is part of the system.

·         Name: A string representing the authentication domain and the user name of the user.

·         Token: An integer, representing the user’s authentication token, assigned by the computer that authenticated the user.

The following snippet of code demonstrates the use of such an authentication technique:

using System.Security.Principal;

 

namespace AuthenticatingUsers

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

          // Store the current user

            WindowsIdentity myID = WindowsIdentity.GetCurrent();

          // Display the name and authentication type

            label1.Text = myID.Name;

            label2.Text = myID.AuthenticationType;

 

            // Check user's authentication status and act accordingly

            if (myID.IsGuest)

                label3.Text = "welcome guest";

            if (myID.IsAuthenticated)

                label3.Text = "Welcome " + myID.Name;

 

        }

    }

}

You can also add a WindowsPrincipal class object to investigate in which groups the user is member. To query for built-in groups you must pass to the WindowsPrincipal.IsInRole method a member of the System.Security.Principal.WindowsBuiltInRole class. See the following example how this works:

       private void main()

        {

          //Create a windowsIdentity object

            WindowsIdentity myID = WindowsIdentity.GetCurrent();

         //Create a WindowsPrincipal object

            WindowsPrincipal myPrincipal = new WindowsPrincipal(myId);

 

            if (myPrincipal.IsInRole(WindowsBuiltInRole.Administrator))

                Console.WriteLine("The current user is an administrator");

        }

Trackback(0)
Comments (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 

User Menu

None
Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor KNOGE.com offers free video and text tutorials on various softwares also free resources to improve your economy and start making money online. THIS IS ONLY A TEST AD TO DISPLAY HOW IT MIGHT LOOK Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor

Adobe InDesign CS3 - Working with objects

Objects in Adobe InDesign CS3 are the basic building block of any design that you do. In this video tutorial you will learn how to create those blocks easily in...

InDesign Videos | Christopher

Read More

Adobe InDesign CS3 - Working with Panels & More

In this Adobe InDesign CS3 video tutorial you will learn how to set keys and work with panels, you will also learn how to create customized keyboard shortcuts for optimized...

InDesign Videos | Christopher

Read More

Adobe Photoshop CS3 - Create customized rust on cars

This Adobe Photoshop CS3 video tutorial will teach you how to create your own customized rust on cars for a more grungy and perhaps dusty effect. This technique does also...

Photoshop Videos | Christopher

Read More
100%
-
+
3
Show options