WindowsPrincipal.IsInRole doesn't reflect changes until restart

January 13, 2009 03:03 by Ryan Garaygay

Just an observation sometime ago that if you create a new Windows Role and add a user to it and create a WindowsPrincipal using that user, the IsInRole method doesn't reflect the membership change made until a restart is made.

For example, given the code below (Console Application project)


using System;

using System.Collections.Generic;

using System.Text;

using System.Security.Principal;

 

namespace WindowsPrincipalTest

{

    class Program

    {

        static void Main(string[] args)

        {

            WindowsIdentity ident = WindowsIdentity.GetCurrent();

            WindowsPrincipal principal = new WindowsPrincipal(ident);

            Console.WriteLine("IsAdmin = " + principal.IsInRole(WindowsBuiltInRole.Administrator));

            Console.WriteLine("IsCustomRole = " + principal.IsInRole("CustomRole"));

            Console.ReadKey();

        }

    }

}

assuming that you have no CustomRole when executing this code for the first time you see the following output



Then create a role named "CustomRole" (if not yet present) then add yourself (or the user which you will use to execute the sample code) as a member of that role.

I usually do this using ComputerManagement MMC (Start > Settings > Control Panel > Administrative Tools OR Start > Run > compmgmt.msc > OK) > System Tools > Local Users / Groups node.

After which, execute the code/application again and you should see the same output as above, IsCustomRole should still be false.

And you should notice that unless you restart your computer the membership change will not be reflected. (** just a reminder to make sure you save documents before restarting)

Digg It!DZone It!StumbleUponDel.icio.usReddit

Related posts

Comments

March 23. 2009 09:14

Dharmindar

Dear Brother,

I m about to attempt the Windows Workflow Foundation Certification Paper (MCTS) on 28-Jan-2009. Can you provide me any guidance or any valuable resource that you have.

As you have already passed this exam.

Thank you.

I have passed my WCF Exam and I am an MCTS for WCF.


We can always share each others knowledge and resources.

Thanks again.

Dharmindar

March 23. 2009 22:16

Ryan Garaygay

I used the WF Step by Step from MSPresss, the one from Wrox and the preparation guide online and research topics I'm not too familiar with

Ryan Garaygay

March 28. 2009 07:05

pingback

Pingback from weblogs.asp.net

WindowsPrincipal.IsInRole doesn't reflect changes until restart - ASP.NET Developer Notes

weblogs.asp.net

March 28. 2009 09:52

D'setiawan

thank for article very great

D'setiawan

March 29. 2009 05:50

Justin

Silly thing off the top of my mind. Log off / log back on? Without restarting? That should apply the principal correctly.

Justin

March 30. 2009 06:26

Earn Pera Online

Cool post! Very informative. Keep it up.
<a href="e-pera.blogspot.com/.../...rams.html">Earn Pera Online</a>

Earn Pera Online

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

February 9. 2010 12:59