I am having problems with my FormsAuthentication, for some reason whey i try to give roles to my user i get the error “The user ‘X’ was not found.”. This error makes no sense as i have just added the user the line before.
The following code
FormsAuthentication.SetAuthCookie(member.Id.ToString(), true);
Roles.AddUserToRole(member.Id.ToString(), "Admin");
Gives me the error (member.Id is 3)
The user '3' was not found
Edit: There is no requirement for the username to actuly be the username, it just has to match what i set as the users username in the FormsAuthentication! It is commen practice to use the users id instead (transformed to a string)! But this dose still not change my problem, so my user’s name is ”3”!
This is done at user login:
// Set roles
foreach(var r in Roles.GetRolesForUser(user.Id.ToString()))
Roles.RemoveUserFromRole(user.Id.ToString(), r);
IList<UserRole> roles = _usersServices.GetUserRoles((int)user.Id);
foreach (var userRole in roles)
{
if(userRole.Expires == null || userRole.Expires > DateTime.Now)
{
if(!Roles.RoleExists(userRole.Name))
Roles.CreateRole(userRole.Name);
Roles.AddUserToRole(user.Id.ToString(), userRole.Name);
}
}
Solution: The problem was that i had forgot to set the roleManager in my web.config, so even though i populated my values at userlogin, did they have nowhere to be stored!
I changed my role manager in
web.configand now it works.It was:
Now it is: