Results 1 to 6 of 6

Thread: Active Directory Membership and Role Provider - Get user name

  1. #1

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Active Directory Membership and Role Provider - Get user name

    I'm using the built in membership (System.Web.Security.Membership) and role (System.Web.Security.Roles) providers with active directory. I would like to get the user's name that is stored in active directory. When I use MembershipUser.UserName, I get their login id. Right now I run their email (MembershipUser.Email) through an alorithm to get their name, but this seems hokey. My second problem is that I need to determine all the users in a specific role. My first thought was to iterate through all the users, then determine if they are in the role I need:
    Code:
    For Each u As MembershipUser In Membership.GetAllUsers
        If Roles.IsUserInRole(u.UserName, "InformationManagementGroup") Then
            userList.Add(New UserInformation(u.UserName, u.Email))
        End If
    Next
    When I try this approach, I get an error: Method is only supported if the user name parameter matches the user name in the current Windows Identity.

    All help is greatly appreciated.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Active Directory Membership and Role Provider - Get user name

    Hey.Haven't done network membership but some thoughts:
    In what state do you try to get your user in roles?Are you logged in as a user?
    I have the impression that you must have an elevated account to be able to run your example.

    So an example(from msdn) would be on your web config, as administrator:
    Code:
    <membership defaultProvider="MyADMembershipProvider">
      <providers>
        <add
           name="MyADMembershipProvider"
           type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, 
                 Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
           connectionStringName="ADConnectionString"
           connectionUsername="testdomain\administrator" 
           connectionPassword="password"/>
      </providers>
     </membership>
    Also try this.

    Code:
     Dim provider As WindowsTokenRoleProvider = CType(Roles.Provider, WindowsTokenRoleProvider)
            For Each u As MembershipUser In Membership.GetAllUsers
                If provider.IsUserInRole(u.UserName,"InformationManagementGroup") Then
                  userList.Add(New UserInformation(u.UserName, u.Email))
        End If
    Next
    Also on account to getting their login id on membership.Username.I did a quick look on msdn an i think you need to do what you do(email).Although i'm not 100% sure here.Also if you say you get the id instead of their username then you provide the id on IsUserInRole and not their username.Again i don't know for sure how membership on active directory works and i don't have a network to do tests here,so this is my thoughts so you may get something out of all this.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Active Directory Membership and Role Provider - Get user name

    Hey,

    Remember, that when calling these methods using Active Directory provider, you need to use the fully qualified username and role name, i.e. including the domain name.

    Are you doing this?

    Gary

  4. #4

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Active Directory Membership and Role Provider - Get user name

    Code:
     Dim provider As WindowsTokenRoleProvider = CType(Roles.Provider, WindowsTokenRoleProvider)
            For Each u As MembershipUser In Membership.GetAllUsers
                If provider.IsUserInRole(u.UserName,"InformationManagementGroup") Then
                  userList.Add(New UserInformation(u.UserName, u.Email))
        End If
    Next
    I tried this, same error.

    Quote Originally Posted by gep13 View Post
    Hey,

    Remember, that when calling these methods using Active Directory provider, you need to use the fully qualified username and role name, i.e. including the domain name.

    Are you doing this?

    Gary
    Yes
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Active Directory Membership and Role Provider - Get user name

    Are you logged in as an admin when you try it?
    Can you show the web.config (without your password of course).
    Are you running this on IIS or on a local network computer project?
    If on IIS can you show the folder permissions(aka file permissions).
    Last edited by sapator; Jan 4th, 2011 at 08:03 PM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Active Directory Membership and Role Provider - Get user name

    I think the Membership and Role providers are great for authentication and authorization of the user. I was trying to leverage them as an active directory search mechanism, and I don't think they were meant to be used that way. So now I have a couple options. I can create a class to interface with the DirectoryServices assembly, which I believe will require me to get a user name and password. I'm not sure if our system admins will be okay with this. If they are not, I could create a batch job to pull active directory information, and store it in a database. If anyone else has an idea, please share.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

Posting Permissions

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



Click Here to Expand Forum to Full Width