Monday, 29 April 2019

Finding Logged in user /Domain using System.Security

To find the Domain name using System.Security.Principal.WindowsIdentity

#C Code:-

 //Get the access if the username exists in the group 

     private bool GetUserInfo()
        {
            bool isAccess = false;
            ClsMailParameters.UsrSourceKey = Environment.UserName;
            if (CheckIdentity() == true)
            {
                isAccess = true;
            }
            return isAccess;
        }

// function for check the user exists in the group or not
//If exists return true otherwise false

private bool CheckIdentity()
        {
            bool bIsGroupie = false;
            System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();

            var sourceKey = identity.Name.ToString();

            // get a list of group names for the user

            ArrayList groups2 = new ArrayList();

            foreach (System.Security.Principal.IdentityReference group in identity.Groups)
            {
                groups2.Add(group.Translate(typeof(System.Security.Principal.NTAccount)).ToString());

            }

            for (int i = 0; i < groups2.Count; i++)
            {
                var ii = groups2[i].ToString();
                string checkValue = "GroupName"; // Give the group name
                try
                {
                    if (ii.ToUpper() == checkValue.ToUpper())
                    {
                        bIsGroupie = true;
                        break;
                    }
                }
                catch { }
            }
            return bIsGroupie;
        }

No comments:

Post a Comment

What is Agile,advantage and disadvantages

Introduction:- It is a software development life cycle used for software development, that is characterized by the division of tasks in...