WindowsPrincipal.IsInRole() problem with non-builtin roles

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default WindowsPrincipal.IsInRole() problem with non-builtin roles

    Can't get WindowsPrincipal.IsInRole() to work for me when using
    Windows Authentication. Here's a snippit of code from my C#
    codebehind page:

    WindowsPrincipal wp = new WindowsPrincipal(
    WindowsIdentity.GetCurrent() );
    lblUser.Text = wp.Identity.Name;
    Label1.Text = wp.IsInRole(@"DOMAIN\group").ToString();


    where "DOMAIN\group" is a valid group name. The username shows up
    correctly as "DOMAIN\username" but for any non-builtin roles,
    IsInRole() returns false. Does anyone have suggestions as to why this
    is not working?



    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    naijacoder naijacoder Guest

  2. Similar Questions and Discussions

    1. WindowsPrincipal.IsInRole() is Being Flaky. Help!!
      Its just being inconsistent. I'm in 3 different Groups in AD. ..IsInRole("Groupx") returns true ..IsInRole("Groupy") returns true...
    2. isinrole reverts to windowsprincipal?
      I'm trying to assign all roles (AD and custom pulled from SQL Table) to users when they login to the app using Windows Authentication. The code...
    3. FormsAuthentication Roles Problem
      I want to use FormsAuthentication and allow access based on role. I have a /Admin directory on the web app, and want to allow role "admin", but...
    4. ASP.NET Context.User.IsInRole XP Problem
      Hi guys I am having a problem with the following line of code on Windows XP Pro. The variable userRole is a string depicting my role on the local...
    5. Problem using Allow Roles
      Dear All, I have an application secured using the following in the web.config file... <authorization> <deny users = "?" /> <allow roles =...
  3. #2

    Default Re: WindowsPrincipal.IsInRole() problem with non-builtin roles

    When using Windows authentication in ASP.NET, the WindowsPrincipal for the
    logged in user is in the HttpContext.User property, not the
    WindowsIdentity.GetCurrent(). They are the same IF you are impersonating,
    but otherwise they are not.

    HTH,

    Joe K.

    "naijacoder naijacoder" <naijacoder@toughguy.net> wrote in message
    news:urYFE4ljEHA.3536@TK2MSFTNGP12.phx.gbl...
    > Can't get WindowsPrincipal.IsInRole() to work for me when using
    > Windows Authentication. Here's a snippit of code from my C#
    > codebehind page:
    >
    > WindowsPrincipal wp = new WindowsPrincipal(
    > WindowsIdentity.GetCurrent() );
    > lblUser.Text = wp.Identity.Name;
    > Label1.Text = wp.IsInRole(@"DOMAIN\group").ToString();
    >
    >
    > where "DOMAIN\group" is a valid group name. The username shows up
    > correctly as "DOMAIN\username" but for any non-builtin roles,
    > IsInRole() returns false. Does anyone have suggestions as to why this
    > is not working?
    >
    >
    >
    > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!

    Joe Kaplan \(MVP - ADSI\) Guest

  4. #3

    Default Re: WindowsPrincipal.IsInRole() problem with non-builtin roles

    Agree with Joe's comment (always use the User property to avoid
    impersonatuion issues). Nevertheless, if you want to go further and check
    out what roles are beeing evaluated inside the IsInRole() method, you may
    use this little "hack" snippet to inspect the roles string array that use
    WindowsPrincipal for this evaluation.

    public static string[] Roles( WindowsIdentity identity )
    {
    // Parameters check
    if( identity == null )
    {
    throw new ArgumentNullException( "identity" );
    }
    if( identity.Name.Length < 1 )
    {
    return new string[0];
    }

    // Get roles
    string[] roles = (string[])CallPrivateMethod( identity, "GetRoles" );
    return roles;
    }

    //Note: This method will require 'ReflectionPermission'
    [ReflectionPermission( SecurityAction.Assert, MemberAccess=true,
    TypeInformation=true )]
    private static object CallPrivateMethod(object o, string methodName)
    {
    Type t = o.GetType();
    MethodInfo mi = t.GetMethod(methodName, BindingFlags.NonPublic |
    BindingFlags.Instance);
    if (mi == null)
    {
    throw new System.Reflection.ReflectionTypeLoadException(null ,null,
    String.Format("{0}.{1} method wasn't found. The runtime
    implementation may have changed!", t.FullName,
    methodName ) );
    }
    return mi.Invoke(o, null);
    }


    --
    Hernan de Lahitte
    Lagash Systems S.A.
    [url]http://weblogs.asp.net/hernandl[/url]


    This posting is provided "AS IS" with no warranties, and confers no rights.

    "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote
    in message news:u8kZ12pjEHA.2324@TK2MSFTNGP10.phx.gbl...
    > When using Windows authentication in ASP.NET, the WindowsPrincipal for the
    > logged in user is in the HttpContext.User property, not the
    > WindowsIdentity.GetCurrent(). They are the same IF you are impersonating,
    > but otherwise they are not.
    >
    > HTH,
    >
    > Joe K.
    >
    > "naijacoder naijacoder" <naijacoder@toughguy.net> wrote in message
    > news:urYFE4ljEHA.3536@TK2MSFTNGP12.phx.gbl...
    >> Can't get WindowsPrincipal.IsInRole() to work for me when using
    >> Windows Authentication. Here's a snippit of code from my C#
    >> codebehind page:
    >>
    >> WindowsPrincipal wp = new WindowsPrincipal(
    >> WindowsIdentity.GetCurrent() );
    >> lblUser.Text = wp.Identity.Name;
    >> Label1.Text = wp.IsInRole(@"DOMAIN\group").ToString();
    >>
    >>
    >> where "DOMAIN\group" is a valid group name. The username shows up
    >> correctly as "DOMAIN\username" but for any non-builtin roles,
    >> IsInRole() returns false. Does anyone have suggestions as to why this
    >> is not working?
    >>
    >>
    >>
    >> *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    >> Don't just participate in USENET...get rewarded for it!
    >
    >

    Hernan de Lahitte Guest

  5. #4

    Default Re: WindowsPrincipal.IsInRole() problem with non-builtin roles

    Hi Hernan de Lahitte,
    How are you and thanks for the code!
    I tried running the code for getting the actual roles but i keep getting
    errors.Can you pls explain how i can get the code working.Pls explain
    step by step.
    Thanks alot.


    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    naijacoder naijacoder Guest

  6. #5

    Default Re: WindowsPrincipal.IsInRole() problem with non-builtin roles

    Since you are using VB.NET, perhaps this sample (doing the same basic thing)
    will work for you:

    Function GetRoles(byval identity as WindowsIdentity) as String()

    Dim idType As Type
    idType = GetType(WindowsIdentity)
    Dim result As Object =
    idType.InvokeMember("_GetRoles",BindingFlags.Stati c Or
    BindingFlags.InvokeMethod Or BindingFlags.NonPublic,Nothing, identity, New
    Object() {identity.Token}, Nothing)
    Dim roles() As String = DirectCast(result, String())
    Return roles

    End Function

    Joe K.

    "naijacoder naijacoder" <naijacoder@toughguy.net> wrote in message
    news:OV1fKAIkEHA.3848@tk2msftngp13.phx.gbl...
    > Hi Hernan de Lahitte,
    > How are you and thanks for the code!
    > I tried running the code for getting the actual roles but i keep getting
    > errors.Can you pls explain how i can get the code working.Pls explain
    > step by step.
    > Thanks alot.
    >
    >
    > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!

    Joe Kaplan \(MVP - ADSI\) Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139