isInRole doesn't work for one user, but works for everyone else

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

  1. #1

    Default isInRole doesn't work for one user, but works for everyone else

    I have an ASP.NET/C# application in which I verify that the current user is a
    member of a list of roles before giving them access to particular functions
    of the application (read vs update). I am using the IsInRole method of the
    IPrincipal object to check for role membership. Currently, I am just
    checking the domain/username against a list of domain/usernames, and will
    eventually created Groups.

    This is working well for all users, except one. Although my application is
    correctly identifying this user with the correct domain/username, the
    isinrole call returns false.

    My code is below:

    from the .aspx.cs:

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!((Security)(Application["security"])).userInRole("edit",
    HttpContext.Current.User))
    edit = false;
    else
    edit = true;


    }

    This code is from a C# object (called "Security") and is called from the
    page above:


    public Boolean userInRole(String role, IPrincipal principal)
    {
    Boolean inRole = false;

    AppDomain.CurrentDomain.SetPrincipalPolicy(Princip alPolicy.WindowsPrincipal);

    //get users from hashtable
    String[] users = (String[])securityRolesMap[role];

    //loop through users to see is the current user matches

    for(int i=0;i< users.Length;i++)
    {
    String user = users[i];
    if (principal.IsInRole(users[i].ToLower()))
    {
    inRole = true;
    break;
    }
    }

    return inRole;

    }


    Any ideas why this would work okay for everyone except one user?

    petersonrj Guest

  2. Similar Questions and Discussions

    1. User.IsInRole is always FALSE
      Hi, I have the following problem... Pre-requisites: Installation of an Asp.net webservice on a IIS5 server (win2k). Anonymous access is not...
    2. Custom implementation for User.IsInRole??
      I have an app where im using FormsAuthenticaton, and doing my own authentication against a users table in my db. I have no problem actually getting...
    3. 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...
    4. IsInRole doesn't works correctly
      In my ASP.NET Application i check whether user that opens application is a member of my created Windows Group(Managers). if...
    5. User.IsInRole not redirecting
      Hi there, I have been reading up on Authorization and role based security for a couple of days now, and am trying to implement this in my...
  3. #2

    Default isInRole doesn't work for one user, but works for everyone else

    Hi,

    i must admin - i don't really understand your logic.

    why don't you just call User.IsInRole("role"); ???

    another note - the documentation states that your are only allowed to call SetPrincipalPolicy once per AppDomain - maybe something is wrong here...

    You only have to call SetPrincipalPolicy if no plumbing has populated Thread.CurrentPrincipal for you (e.g. in a console / winforms app) - but ASP.NET does that.



    ---
    Dominick Baier - DevelopMentor
    [url]http://www.leastprivilege.com[/url]

    nntp://news.microsoft.com/microsoft.public.dotnet.framework.aspnet.security/<4DACDDCC-5AC0-495A-A583-C44B3F8CC6FE@microsoft.com>

    I have an ASP.NET/C# application in which I verify that the current user is a
    member of a list of roles before giving them access to particular functions
    of the application (read vs update). I am using the IsInRole method of the
    IPrincipal object to check for role membership. Currently, I am just
    checking the domain/username against a list of domain/usernames, and will
    eventually created Groups.

    This is working well for all users, except one. Although my application is
    correctly identifying this user with the correct domain/username, the
    isinrole call returns false.

    My code is below:

    from the .aspx.cs:

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!((Security)(Application["security"])).userInRole("edit",
    HttpContext.Current.User))
    edit = false;
    else
    edit = true;


    }

    This code is from a C# object (called "Security") and is called from the
    page above:


    public Boolean userInRole(String role, IPrincipal principal)
    {
    Boolean inRole = false;

    AppDomain.CurrentDomain.SetPrincipalPolicy(Princip alPolicy.WindowsPrincipal);

    //get users from hashtable
    String[] users = (String[])securityRolesMap[role];

    //loop through users to see is the current user matches

    for(int i=0;i< users.Length;i++)
    {
    String user = users[i];
    if (principal.IsInRole(users[i].ToLower()))
    {
    inRole = true;
    break;
    }
    }

    return inRole;

    }


    Any ideas why this would work okay for everyone except one user?


    [microsoft.public.dotnet.framework.aspnet.security]
    Dominick Baier Guest

  4. #3

    Default RE: isInRole doesn't work for one user, but works for everyone else

    Dominick,

    Thanks for the information on SetPrincipalPolicy method. I removed that
    from my code.

    The userInRole method that I created is intended to be a reusable method
    throughout my application, as I need this functionality in multiple places.
    So, I really am just calling User.IsInRole("role") since User is an
    IPrincipal.

    For the user for which the call wasn't working, I created an AD group and
    added them as a member. The isInRole works fine for that user when comparing
    to a group, just not against their user id. I'm still not sure why, but at
    least I've got the app working.

    Thanks for your help!


    "Dominick Baier" wrote:
    > Hi,
    >
    > i must admin - i don't really understand your logic.
    >
    > why don't you just call User.IsInRole("role"); ???
    >
    > another note - the documentation states that your are only allowed to call SetPrincipalPolicy once per AppDomain - maybe something is wrong here...
    >
    > You only have to call SetPrincipalPolicy if no plumbing has populated Thread.CurrentPrincipal for you (e.g. in a console / winforms app) - but ASP.NET does that.
    >
    >
    >
    > ---
    > Dominick Baier - DevelopMentor
    > [url]http://www.leastprivilege.com[/url]
    >
    > nntp://news.microsoft.com/microsoft.public.dotnet.framework.aspnet.security/<4DACDDCC-5AC0-495A-A583-C44B3F8CC6FE@microsoft.com>
    >
    > I have an ASP.NET/C# application in which I verify that the current user is a
    > member of a list of roles before giving them access to particular functions
    > of the application (read vs update). I am using the IsInRole method of the
    > IPrincipal object to check for role membership. Currently, I am just
    > checking the domain/username against a list of domain/usernames, and will
    > eventually created Groups.
    >
    > This is working well for all users, except one. Although my application is
    > correctly identifying this user with the correct domain/username, the
    > isinrole call returns false.
    >
    > My code is below:
    >
    > from the .aspx.cs:
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > if (!((Security)(Application["security"])).userInRole("edit",
    > HttpContext.Current.User))
    > edit = false;
    > else
    > edit = true;
    >
    >
    > }
    >
    > This code is from a C# object (called "Security") and is called from the
    > page above:
    >
    >
    > public Boolean userInRole(String role, IPrincipal principal)
    > {
    > Boolean inRole = false;
    >
    > AppDomain.CurrentDomain.SetPrincipalPolicy(Princip alPolicy.WindowsPrincipal);
    >
    > //get users from hashtable
    > String[] users = (String[])securityRolesMap[role];
    >
    > //loop through users to see is the current user matches
    >
    > for(int i=0;i< users.Length;i++)
    > {
    > String user = users[i];
    > if (principal.IsInRole(users[i].ToLower()))
    > {
    > inRole = true;
    > break;
    > }
    > }
    >
    > return inRole;
    >
    > }
    >
    >
    > Any ideas why this would work okay for everyone except one user?
    >
    >
    > [microsoft.public.dotnet.framework.aspnet.security]
    >
    petersonrj Guest

  5. #4

    Default Re: isInRole doesn't work for one user, but works for everyone else

    Have u set ur IIS settings..
    Go the Virtual Directory ur aplication is on IIS and clear the check box
    Anonymous Access

    "petersonrj" <petersonrj@discussions.microsoft.com> wrote in message
    news:B5C2F563-B837-4B06-9D1F-680BBE8CD7FD@microsoft.com...
    > Dominick,
    >
    > Thanks for the information on SetPrincipalPolicy method. I removed that
    > from my code.
    >
    > The userInRole method that I created is intended to be a reusable method
    > throughout my application, as I need this functionality in multiple
    places.
    > So, I really am just calling User.IsInRole("role") since User is an
    > IPrincipal.
    >
    > For the user for which the call wasn't working, I created an AD group and
    > added them as a member. The isInRole works fine for that user when
    comparing
    > to a group, just not against their user id. I'm still not sure why, but
    at
    > least I've got the app working.
    >
    > Thanks for your help!
    >
    >
    > "Dominick Baier" wrote:
    >
    > > Hi,
    > >
    > > i must admin - i don't really understand your logic.
    > >
    > > why don't you just call User.IsInRole("role"); ???
    > >
    > > another note - the documentation states that your are only allowed to
    call SetPrincipalPolicy once per AppDomain - maybe something is wrong
    here...
    > >
    > > You only have to call SetPrincipalPolicy if no plumbing has populated
    Thread.CurrentPrincipal for you (e.g. in a console / winforms app) - but
    ASP.NET does that.
    > >
    > >
    > >
    > > ---
    > > Dominick Baier - DevelopMentor
    > > [url]http://www.leastprivilege.com[/url]
    > >
    > >
    nntp://news.microsoft.com/microsoft.public.dotnet.framework.aspnet.security/
    <4DACDDCC-5AC0-495A-A583-C44B3F8CC6FE@microsoft.com>
    > >
    > > I have an ASP.NET/C# application in which I verify that the current
    user is a
    > > member of a list of roles before giving them access to particular
    functions
    > > of the application (read vs update). I am using the IsInRole method of
    the
    > > IPrincipal object to check for role membership. Currently, I am just
    > > checking the domain/username against a list of domain/usernames, and
    will
    > > eventually created Groups.
    > >
    > > This is working well for all users, except one. Although my application
    is
    > > correctly identifying this user with the correct domain/username, the
    > > isinrole call returns false.
    > >
    > > My code is below:
    > >
    > > from the .aspx.cs:
    > >
    > > private void Page_Load(object sender, System.EventArgs e)
    > > {
    > > if (!((Security)(Application["security"])).userInRole("edit",
    > > HttpContext.Current.User))
    > > edit = false;
    > > else
    > > edit = true;
    > >
    > >
    > > }
    > >
    > > This code is from a C# object (called "Security") and is called from
    the
    > > page above:
    > >
    > >
    > > public Boolean userInRole(String role, IPrincipal principal)
    > > {
    > > Boolean inRole = false;
    > >
    > >
    AppDomain.CurrentDomain.SetPrincipalPolicy(Princip alPolicy.WindowsPrincipal)
    ;
    > >
    > > //get users from hashtable
    > > String[] users = (String[])securityRolesMap[role];
    > >
    > > //loop through users to see is the current user matches
    > >
    > > for(int i=0;i< users.Length;i++)
    > > {
    > > String user = users[i];
    > > if (principal.IsInRole(users[i].ToLower()))
    > > {
    > > inRole = true;
    > > break;
    > > }
    > > }
    > >
    > > return inRole;
    > >
    > > }
    > >
    > >
    > > Any ideas why this would work okay for everyone except one user?
    > >
    > >
    > > [microsoft.public.dotnet.framework.aspnet.security]
    > >

    Patrick.O.Ige 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