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)
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.
If you already do have the (Community) user object, the .Groups property should work...
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.
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?
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);
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: