Adding a user to Groups on logging in

Vote:
 

Hi,

 

I'm trying to add a user to a group on login, in the loggedin event of the Login asp.net control. After successfully authenticating the user and getting the user object from AuthenticateUser(username, password, out user) method i try to loop through a list of groups (as string names) however I see that the Community methods for gettin groups for a user is dependent on CurrentUser being set, and in the process of logging in; CurrentUser is set After the login page is completed and the application is redirected to a new page (In this case, MyPage).

Does anyone have any clues at to what to do? One veeery important thing; I'm using Community 4.

 

Code:

var memberships = crmMembership.GetMemberships(user.UserName) as List;
if (memberships != null)
                    {
                        user = (IUser)user.Clone();
                        foreach (var membership in memberships)
                        {
                            IGroup @group = CommunitySystem.CurrentContext.DefaultSecurity.GetGroupByName(membership);
                            user.Groups.Add(group);
                        }
                        CommunitySystem.CurrentContext.DefaultSecurity.UpdateUser(user);
                    }
#39119
May 04, 2010 18:06
Vote:
 

There is a method in the SecurityHandler (which is what is also available through CurrentContext.DefaultSecurity) for getting a user by username.

 

That should work even if CurrentUser is not set. (CurrentUser relies on System.Web.HttpContext.Current.User.Identity.Name)

#39155
May 05, 2010 8:52
Vote:
 

Thanks for your answer Håkan, but I need the groups for the user. The user object is no problem, I get that on Authenticate. The method GetGroupByName is dependent on CurrentUser, and that fails..

I am thinking about doing a Query, but I'm not too sure that will work for my purpose which is to remove all groups from a user where the group matches a name, and add the user again to an updated list of groups on login.

#39156
May 05, 2010 9:11
Vote:
 

If you already do have the (Community) user object, the .Groups property should work...

#39157
May 05, 2010 9:14
Vote:
 

That is true, but GetGroupByName does not work, which is my only issue here. I need to get a group from the community by a specific name and add the user to that group. The User objects Groups property is available because the User object is retrieved on login.

#39161
May 05, 2010 9:43
Vote:
 

What happens when you use GetGroupByName then?

#39165
May 05, 2010 11:47
Vote:
 

EPiServer.Common.Security.SecurityHandler.Instance.GetGroupByName(string name)

is not dependent in any way on CurrentUser. In what way does it fail when using GetGroupByName?

#39166
May 05, 2010 11:58
Vote:
 

Solved it. Thanks for the feedback on GetGroupByName, helped a lot in debugging. Turns out I have to be very careful on managing groups in EpiServer, Community and Windows when using those membership providers.

 

 

var memberships = crmMembership.GetMemberships(user.UserName) as List;
user = (IUser)user.Clone();
foreach (IGroup g in memberships
    .Select(m => SecurityHandler.Instance.GetGroupByName(m))
    .Where(g => g != null))
    {
        if(user.Groups.Contains(g))
            user.Groups.Remove(g);
    }
SecurityHandler.Instance.UpdateUser(user);
#39232
May 05, 2010 17:29
This thread is locked and should be used for reference only. Please use the Legacy add-ons forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.