---
title: "Getting Login User Status in C#."  
description: "This is a small blog of Code Access and Security in c#. In this blog we try to know status of user or we can say how we can know that the user who is"  
author: "Anonymous User"  
published: 2011-04-28  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/147/getting-login-user-status-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Getting Login User Status in C#.

This is a small blog of Code [Access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) and [Security](https://www.mindstick.com/articles/43813/new-security-technologies) in c#. In this blog we try to know [status of user](https://www.mindstick.com/forum/232/to-check-status-of-user-login-in-c-sharp) or we can say how we can know that the user who is login is administrator or user or guest and what is [account](https://www.mindstick.com/articles/33699/how-tech-is-disrupting-the-accounting-world) name.\

For performing this task we used to [import](https://www.mindstick.com/blog/294/how-to-import-or-export-sql-server-table-data-in-ms-excel-sheet-using-c-sharp-code) using [System](https://www.mindstick.com/articles/23411/the-most-effective-method-to-find-the-perfect-small-business-phone-system-for-your-business).Security.Principal [namespace](https://www.mindstick.com/articles/12552/namespace) and we used two classes which WindowsIdentity and WindowsPrincipal [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp).

Following example demonstrate that how to use these classes to know user status information in c#.net.

```
using System;using System.Security.Principal;   //This name space is required for using WindowsIdentity and WindowsPrinciple class.namespace UserTypeDemo{    class Program    {        static void Main(string[] args)        {            //Create an object of WindowsIdentity class using GetCurrent() method of WindowsIdentity class.            WindowsIdentity identity = WindowsIdentity.GetCurrent();            //Create an object of windows principle class using new keyword and pass WindowsIndentity object            //As parameter to constructor of WindowPrinciple class.            WindowsPrincipal principle = new WindowsPrincipal(identity);            //Display current user login name.            Console.WriteLine("Current Login User Name  :  {0}", principle.Identity.Name);            //Display type of user such as he is admin, guest or user.            if (principle.IsInRole(WindowsBuiltInRole.AccountOperator))                Console.WriteLine("Login User is : Account Operator.");            else                if (principle.IsInRole(WindowsBuiltInRole.Administrator))                    Console.WriteLine("Login User is : Administrator.");                else                    if (principle.IsInRole(WindowsBuiltInRole.BackupOperator))                        Console.WriteLine("Login User is : Backup opearator.");                    else                        if (principle.IsInRole(WindowsBuiltInRole.Guest))                            Console.WriteLine("Login User is : Guest.");                        else                            if (principle.IsInRole(WindowsBuiltInRole.PowerUser))                                Console.WriteLine("Login User is : Power User.");                            else                                if (principle.IsInRole(WindowsBuiltInRole.User))                                    Console.WriteLine("Login User is : User.");        }    }}
```

##### Output of the following code snippets is as follows:

Current Login [User Name](https://www.mindstick.com/interview/33836/how-to-check-current-user-name-of-database-in-sql-server): abs-user.

Login User is: User.

Press any key to continue . . .

---

Original Source: https://www.mindstick.com/blog/147/getting-login-user-status-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
