I’m having a hard time figuring out how to work with the two string arrays passed into AddUsersToRoles.
Here’s what I think I need to do:
- First get the int UserID for the string[] usernames.
- Then get the int RoleID for string[] roles.
- Concat the two arrays together.
- Save the data to UserRole (a database that contains 3 columns: UserRoleID, UserID, RoleID).
Here’s what it looks like:
//override and implement a custom 'adduserstoroles' from the abstract method in RoleProvider
public override void AddUsersToRoles(string[] usernames, string[] roleNames)
{
int[] userid;
foreach(var username in usernames)
{
User user = AuthRepository.GetUser(username);
//Error 1: An object reference is required for the non-static field, method, or property 'CustomAuth.AuthRepository.GetUser(string)'
userid = user.ID;
//Error 2: Cannot implicitly convert type 'int' to 'int[]'
}
int[] roleid;
foreach(var rolename in roleNames)
{
Role role = db.Roles.Where(r => r.RoleName == rolename).SingleOrDefault();
roleid = role.RoleID;
//Error 3: Cannot implicitly convert type 'int' to 'int[]'
}
var userroles = userid.Concat(roleid).ToArray();
foreach(var userrole in userroles)
{
UserRole userRole = new UserRole();
db.UserRoles.Add(userRole);
}
}
Now, I don’t know if I’m going off the deep end on this. I’ve searched around for examples of how others have handled this type of thing but I can’t find any answers.
for error 2 and 3, you’re trying to assign a plain old int to an array of ints, which doesn’t make any sense. You probably just want to make userid and roleid List’s of ints
Then in the foreach loops, you can just
and
for error 1, it looks like AuthRepository isn’t a static class, so you’d need to create an instance of it before using it.