locked
Adding roles using AddUserToRole(string username, string roleName) does not work sometimes RRS feed

  • Question

  • User-1410985216 posted

    We are working on an ASP.NET Web Forms application using ASP.NET Membership for user related operations. When a user is created in the application, we add him to two roles, a main and a sub role, as per the below code snippet.

    Roles.AddUserToRole(userName, mainRoleName);
    if (mainRoleName == "Test")
    {
        Roles.AddUserToRole(userName, subRoleName);
    }
    

    Rarely, we face an issue where the user gets added to the subRoleName but is not part of the mainRoleName. Is there any specific reason for this to happen and anyone faced similar issues like the one mentioned?

    Monday, December 28, 2020 2:10 PM

All replies

  • User475983607 posted

    You have to understand a forum cannot answer this question.  The code you've shared uses variables for the role names; mainRoleName and subRoleName.  The community has no idea how these role variables are set or if it is possible for the variable to contain values that do not match roles in the database.

    When I consider troubleshooting steps,  I would look at either the mainRoleName does not equal "Test" or the subroleName does not exist in the database.  I'm pretty sure the code will throw an exception if the role does not exist in the database.   Do you have proper exception handling enable and are not hiding exceptions?   Are you logging error messages?  

    Monday, December 28, 2020 2:31 PM
  • User753101303 posted

    Hi,

    Or could it be that the role "Test" is later removed from this user but that the subRoleName is left?

    Monday, December 28, 2020 3:03 PM