Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 16, 2013 @ 13:02
    Fuji Kusaka
    0

    Get Current MemberGroup

    Hello everyone,

    I have a website with 10 different Member Group where all members have access to certain specific content on the website.

    Can somehow tell me how do i get to display the MemberGroup of a Login User ?

    var CUser = Member.GetCurrentMember();
        
        if(AlnUser != null){
            var CUserName = CUser.Text;
            var CLastName = CUser.getProperty("userLastName").Value.ToString(); @CUserName @CLastName @CUser.MemberGroup
  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 16, 2013 @ 13:26
    Fuji Kusaka
    0

    Still not working though

     var userName = CUser.LoginName;
        var userGroup = CUser.Id;
       
       
        var roles = System.Web.Security.Roles.GetRolesForUser(1066);

        @roles.ToString()
  • Anders Bjerner 487 posts 2989 karma points MVP 8x admin c-trib
    Oct 16, 2013 @ 13:30
    Anders Bjerner
    2

    The method only takes a login name as parameter - not a user ID, so based on your code, try the following:

    string[] roles = System.Web.Security.Roles.GetRolesForUser(userName);
    

    The method will then return a string array of the member groups the user is associated with ;)

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 16, 2013 @ 13:33
    Fuji Kusaka
    100

    Hi Anders,

    Still the same output System.String[]. Am i missing any other Namespace apart from System.Web.Security ? 

    var roles =System.Web.Security.Roles.GetRolesForUser(username); or
    String[] r = System.Web.Security.Roles.GetRolesForUser(userName);
  • Anders Bjerner 487 posts 2989 karma points MVP 8x admin c-trib
    Oct 16, 2013 @ 13:39
    Anders Bjerner
    0

    I'm not sure I understand the last part. Are you getting any errors? The method will return a string array, but it doesn't matter whether you write var, string[] or String[] - it means the same in the end.

    It's not necessary to import any namespaces to make it work.

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 16, 2013 @ 13:42
    Fuji Kusaka
    0

    No not getting any errors but would like to display the MemberGroup to which this member belongs to . But here how do i get it instead of System.string[] ?

  • Anders Bjerner 487 posts 2989 karma points MVP 8x admin c-trib
    Oct 16, 2013 @ 13:49
    Anders Bjerner
    2

    The method returns an array since a member can be a part of multiple groups.

    Bu you can check for a specific member group like this:

    string[] roles = System.Web.Security.Roles.GetRolesForUser(userName);
    
    bool isInSomeGroup = roles.Contains("SomeGroup");
    bool isInSomeOtherGroup = roles.Contains("SomeOtherGroup");
    

    This will require the System.Linq namespace.

    You can also list all the groups like this:

    <ul>
    @foreach (string role in roles) {
        <li>@role</li>
    }
    </ul>
    
  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 16, 2013 @ 14:04
    Fuji Kusaka
    0

    Yes you right. Thanks for the help 

Please Sign in or register to post replies

Write your reply to:

Draft