Try our conversational search powered by Generative AI!

Adding a user to Groups on logging in




I'm trying to add a user to a group on login, in the loggedin event of the Login 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.



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);
May 04, 2010 18:06

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)

May 05, 2010 8:52

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.

May 05, 2010 9:11

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

May 05, 2010 9:14

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.

May 05, 2010 9:43

What happens when you use GetGroupByName then?

May 05, 2010 11:47

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?

May 05, 2010 11:58

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))
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.