Question

I have created two roles administrator and user in roles table.Now i want to assign a default role of user when any user registers at my site. What will be the next step to assign a role to a user.I have registered user by using custom membership provider.I have created a custom role provider.

CreateUser Method in CustomMembership Class

   public string CreateUser(Registration reg)
    {
     reg.Token = Guid.NewGuid().ToString();
        Session.Store(reg);
        Session.SaveChanges();
        return reg.Token;
    }

AddUserToRole Method in Custom Role Provider

    public string AddUserToRoles(RoleManager role)
      {
         //assign user to role.
      }

I have created two fields in CreateUserRole method that are roleId and roleName. Now,my question is that i want to select the role of "user" from database and assign it to every user at the time of registration when the user registers that is CreateUser method.How can i achieve it??

Was it helpful?

Solution

Try this

public string CreateUser(Registration reg)
{
    reg.Token = Guid.NewGuid().ToString();
    Session.Store(reg);
    Session.SaveChanges();
    AddUserToRoles("user") //it depends here whether u want to pass role id or role name then create the overloaded function Accordingly
    return reg.Token;
}

create overrided for AddUserToRole

public string AddUserToRoles(string role)
{
    //assign user to role.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top