This documentation is archived and is not being maintained.

WindowsPrincipal.IsInRole Method

Determines whether the current principal belongs to a specified Windows user group.

Overload List

Determines whether the current principal belongs to the Windows user group with the specified role identifier (RID).

[Visual Basic] Overloads Public Overridable Function IsInRole(Integer) As Boolean
[C#] public virtual bool IsInRole(int);
[C++] public: virtual bool IsInRole(int);
[JScript] public function IsInRole(int) : Boolean;

Determines whether the current principal belongs to the Windows user group with the specified name.

[Visual Basic] Overloads Public Overridable Function IsInRole(String) As Boolean Implements IPrincipal.IsInRole
[C#] public virtual bool IsInRole(string);
[C++] public: virtual bool IsInRole(String*);
[JScript] public function IsInRole(String) : Boolean;

Determines whether the current principal belongs to the Windows user group with the specified WindowsBuiltInRole.

[Visual Basic] Overloads Public Overridable Function IsInRole(WindowsBuiltInRole) As Boolean
[C#] public virtual bool IsInRole(WindowsBuiltInRole);
[C++] public: virtual bool IsInRole(WindowsBuiltInRole);
[JScript] public function IsInRole(WindowsBuiltInRole) : Boolean;

Example

[Visual Basic, C#, C++] The following sample demonstrates how to call IsInRole with a string representing a built-in role.

[Visual Basic, C#, C++] Note   When testing for newly created role information, such as a new user or a new group, it is important to log out and log in to force the propagation of role information within the domain. Not doing so can cause the IsInRole test to return false.
[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of IsInRole. For other examples that might be available, see the individual overload topics.
[Visual Basic] 

   Public Shared Sub DemonstrateWindowsBuiltInRoleEnum()
      Dim myDomain As AppDomain = Thread.GetDomain()
      
      myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
      Dim myPrincipal As WindowsPrincipal = CType(Thread.CurrentPrincipal, WindowsPrincipal)
      
      
      Console.WriteLine("{0} belongs to: ", myPrincipal.Identity.Name.ToString())
      
      Dim wbirFields As Array = [Enum].GetValues(GetType(WindowsBuiltInRole))
      
      Dim roleName As Object
      For Each roleName In  wbirFields
         Try

            Console.WriteLine("{0}? {1}.", roleName, _
                              myPrincipal.IsInRole(CType(roleName, WindowsBuiltInRole)))

         Catch

        Console.WriteLine("{0}: Could not obtain the role for this RID.", roleName)

         End Try
      Next roleName

   End Sub 'DemonstrateWindowsBuiltInRoleEnum
   

[C#] 

   public static void DemonstrateWindowsBuiltInRoleEnum()
   {
    AppDomain myDomain = Thread.GetDomain();

    myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
    WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;

    
    Console.WriteLine("{0} belongs to: ", myPrincipal.Identity.Name.ToString());

    Array wbirFields = Enum.GetValues(typeof(WindowsBuiltInRole));

    foreach (object roleName in wbirFields)
        {
       try
         {
            Console.WriteLine("{0}? {1}.", roleName,
                myPrincipal.IsInRole((WindowsBuiltInRole)roleName));
       } 
       catch (Exception)
           {
        Console.WriteLine("{0}: Could not obtain role for this RID.",
                roleName);
       }
        }        

   }


[C++] 
public:
   static void DemonstrateWindowsBuiltInRoleEnum() {
      AppDomain*  myDomain = Thread::GetDomain();

      myDomain->SetPrincipalPolicy(PrincipalPolicy::WindowsPrincipal);
      WindowsPrincipal*  myPrincipal = dynamic_cast<WindowsPrincipal*>(Thread::CurrentPrincipal);

      Console::WriteLine(S"{0} belongs to: ", myPrincipal->Identity->Name);

      Array*  wbirFields = Enum::GetValues(__typeof(WindowsBuiltInRole));

      System::Collections::IEnumerator* myEnum = wbirFields->GetEnumerator();
      while (myEnum->MoveNext()) {
         Object* roleName = __try_cast<Object*>(myEnum->Current);
         try {
            Console::WriteLine(S"{0}? {1}.", roleName,
               __box(myPrincipal->IsInRole(dynamic_cast<String __gc *>(roleName))));
         } catch (Exception*) {
            Console::WriteLine(S"{0}: Could not obtain role for this RID.",
               roleName);
         }
      }
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

WindowsPrincipal Class | WindowsPrincipal Members | System.Security.Principal Namespace

Show: