locked
Allocating Roles automatically when New Users are registering ... RRS feed

  • Question

  • User-2078661001 posted

    I have a site with 4 different roles.

    Is it possible to automatically allocate a role to a new applicant / user when they are registering, rather than an administrator allocating a role through the ASP.NET Configuration Security Tab.    I want to avoid this administration overhead.

    Any help would be appreciated

    Stuart

    Thursday, November 12, 2015 7:46 AM

Answers

  • User-2078661001 posted

    The following worked as a step in the create user wizard, inserting the following code in an active step change handler ...

    Roles.AddUserToRole(CreateUserWithRole.UserName, roleName)

    I also declared the roleName as a string ="Locum" to ensure the applicant was allocated to the correct role.

    Thanks to all for their help in solving this.Smile

    Stuart

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 13, 2015 11:15 AM

All replies

  • User475983607 posted

    Confused...

    What is stopping you from adding a user to a role during the registration process?

    Roles.AddUserToRole(UserName, roleName);

    http://www.asp.net/web-forms/overview/older-versions-security/roles/assigning-roles-to-users-cs

    Thursday, November 12, 2015 8:19 AM
  • User-2078661001 posted

    Thanks for that mgebhard,

    This looks to be what I am looking for, but I am not too sure where the code should go.

    The way the site is designed applicants for the "locum" role will all use the same registration wizard.   All I am trying to do is allocate them to the Locum Role as they are registering.

    Do I use your suggested code as an additional wizard step, or somewhere in the code behind page ?

    Stuart

    Thursday, November 12, 2015 9:16 AM
  • User475983607 posted

    Do I use your suggested code as an additional wizard step, or somewhere in the code behind page ?

    Since I do not have the code base I can't answer the question.  Somewhere, you are creating a user account.  I assume when an account is successfully created is where you would execute the the logic.  Do you have a senior person around that can help you?

    Roles.AddUserToRole(UserName, roleName);

    http://www.asp.net/web-forms/overview/older-versions-security/membership/creating-user-accounts-cs

    Thursday, November 12, 2015 9:25 AM
  • User1738843376 posted

    At some point you shuold have an SQL query in the lines of this:

    INSERT INTO Users (column1, column2) VALUES (value1, value2);

    Simply add the query to add the role you wish to be granted automatically, so the query looks something like this:

    INSERT INTO Users (column1, column2) VALUES (value1, value2); INSERT INTO UserRoles (column1, column2) VALUES (value1, value2);

    You can make it more complex if you do it on a Stored Procedure and use

            BEGIN
    
    		SET XACT_ABORT ON;
    
    		BEGIN TRY
    		
    			BEGIN TRANSACTION;
    			
    				INSERT INTO Users (column1, column2) VALUES (value1, value2);										
    			COMMIT TRANSACTION;
    			
    		END TRY
    		
    		BEGIN CATCH
    		
                            IF XACT_STATE() = 0
                               INSERT INTO UserRoles (column1, column2) VALUES (value1, value2);
    
    		END CATCH
    			
    	END

    Using this will only add the role if the user was inserted successfully

    Friday, November 13, 2015 8:32 AM
  • User-2078661001 posted

    The following worked as a step in the create user wizard, inserting the following code in an active step change handler ...

    Roles.AddUserToRole(CreateUserWithRole.UserName, roleName)

    I also declared the roleName as a string ="Locum" to ensure the applicant was allocated to the correct role.

    Thanks to all for their help in solving this.Smile

    Stuart

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 13, 2015 11:15 AM