By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

VB.Net Help : Assign user to role at registration

P: 19
Hi Guys im still new to all this and im building an app in asp.net 2 vb which will have 4 roles, Admin, Wholesaler, Landscaper, Customer.

When the user sets up there account i want a drop down list box and depending on what role they select (admin wont be included) i want to assign this role to there account. Im using asp.net

Does someone have some code or can you tell me a tutorial or a way of doing this please

Thank you in advance
Apr 20 '07 #1
Share this Question
Share on Google+
9 Replies


Frinavale
Expert Mod 5K+
P: 9,731
Hi Guys im still new to all this and im building an app in asp.net 2 vb which will have 4 roles, Admin, Wholesaler, Landscaper, Customer.

When the user sets up there account i want a drop down list box and depending on what role they select (admin wont be included) i want to assign this role to there account. Im using asp.net

Does someone have some code or can you tell me a tutorial or a way of doing this please

Thank you in advance
What have you tried already?

-Frinny
Apr 20 '07 #2

radcaesar
Expert 100+
P: 759
Teddy,
Just try something and if you stuck at some point, then we are glad to help you. What you had asked is not that much difficult.
Apr 20 '07 #3

P: 19
Hi As per the last message ive now got some code together and it works, im trying to let the user select there account type during registration so i can structure content dependent on the role, the problem i now have is that i have 4 roles, Administrator, Customer, Landscaper, Wholesales, Code below.

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>
  2. <script runat="server">
  3.     Protected Sub ActivateStep(ByVal sender As Object, ByVal e As System.EventArgs)
  4.         ListBox1.DataSource = Roles.GetAllRoles()
  5.         ListBox1.DataBind()
  6.     End Sub
  7.  
  8.     Protected Sub DeActivateStep(ByVal sender As Object, ByVal e As System.EventArgs)
  9.         Roles.AddUserToRole(User.Identity.Name, ListBox1.SelectedValue)
  10.     End Sub
  11. </script>
  12.  
  13. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  14.     <asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
  15.         <WizardSteps>
  16.             <asp:CreateUserWizardStep runat="server">
  17.             </asp:CreateUserWizardStep>
  18.             <asp:WizardStep runat="server" Title="Choose Account Type" OnActivate="ActivateStep" OnDeactivate="DeActivateStep">
  19.                 Select Account Type:<br />
  20.                 <br />
  21.                 <asp:ListBox ID="ListBox1" runat="server" CssClass="normaltext"></asp:ListBox>
  22.             </asp:WizardStep>
  23.             <asp:CompleteWizardStep runat="server">
  24.             </asp:CompleteWizardStep>
  25.         </WizardSteps>
  26.     </asp:CreateUserWizard>
  27.  
  28.  
  29. </asp:Content>
The problem is i dont want them to be able to add themseleves to the administrator role, but i couldnt work out another way to get the roles and assign them, i found a tutorial that got me this far but now im stuck, your help would be greatly appreciated

Thanks
Apr 21 '07 #4

P: 19
Teddy,
Just try something and if you stuck at some point, then we are glad to help you. What you had asked is not that much difficult.

Hi As per the last message ive now got some code together and it works, im trying to let the user select there account type during registration so i can structure content dependent on the role, the problem i now have is that i have 4 roles, Administrator, Customer, Landscaper, Wholesales, Code below.

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>
  2. <script runat="server">
  3.     Protected Sub ActivateStep(ByVal sender As Object, ByVal e As System.EventArgs)
  4.         ListBox1.DataSource = Roles.GetAllRoles()
  5.         ListBox1.DataBind()
  6.     End Sub
  7.  
  8.     Protected Sub DeActivateStep(ByVal sender As Object, ByVal e As System.EventArgs)
  9.         Roles.AddUserToRole(User.Identity.Name, ListBox1.SelectedValue)
  10.     End Sub
  11. </script>
  12.  
  13. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  14.     <asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
  15.         <WizardSteps>
  16.             <asp:CreateUserWizardStep runat="server">
  17.             </asp:CreateUserWizardStep>
  18.             <asp:WizardStep runat="server" Title="Choose Account Type" OnActivate="ActivateStep" OnDeactivate="DeActivateStep">
  19.                 Select Account Type:<br />
  20.                 <br />
  21.                 <asp:ListBox ID="ListBox1" runat="server" CssClass="normaltext"></asp:ListBox>
  22.             </asp:WizardStep>
  23.             <asp:CompleteWizardStep runat="server">
  24.             </asp:CompleteWizardStep>
  25.         </WizardSteps>
  26.     </asp:CreateUserWizard>
  27.  
  28.  
  29. </asp:Content> 

The problem is i dont want them to be able to add themseleves to the administrator role, but i couldnt work out another way to get the roles and assign them, i found a tutorial that got me this far but now im stuck, your help would be greatly appreciated

Thanks
Apr 21 '07 #5

P: 19
What have you tried already?

-Frinny
Hi As per the last message ive now got some code together and it works, im trying to let the user select there account type during registration so i can structure content dependent on the role, the problem i now have is that i have 4 roles, Administrator, Customer, Landscaper, Wholesales, Code below.

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>
  2. <script runat="server">
  3.     Protected Sub ActivateStep(ByVal sender As Object, ByVal e As System.EventArgs)
  4.         ListBox1.DataSource = Roles.GetAllRoles()
  5.         ListBox1.DataBind()
  6.     End Sub
  7.  
  8.     Protected Sub DeActivateStep(ByVal sender As Object, ByVal e As System.EventArgs)
  9.         Roles.AddUserToRole(User.Identity.Name, ListBox1.SelectedValue)
  10.     End Sub
  11. </script>
  12.  
  13. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  14.     <asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
  15.         <WizardSteps>
  16.             <asp:CreateUserWizardStep runat="server">
  17.             </asp:CreateUserWizardStep>
  18.             <asp:WizardStep runat="server" Title="Choose Account Type" OnActivate="ActivateStep" OnDeactivate="DeActivateStep">
  19.                 Select Account Type:<br />
  20.                 <br />
  21.                 <asp:ListBox ID="ListBox1" runat="server" CssClass="normaltext"></asp:ListBox>
  22.             </asp:WizardStep>
  23.             <asp:CompleteWizardStep runat="server">
  24.             </asp:CompleteWizardStep>
  25.         </WizardSteps>
  26.     </asp:CreateUserWizard>
  27.  
  28.  
  29. </asp:Content> 
The problem is i dont want them to be able to add themseleves to the administrator role, but i couldnt work out another way to get the roles and assign them, i found a tutorial that got me this far but now im stuck, your help would be greatly appreciated

Thanks
Apr 21 '07 #6

Frinavale
Expert Mod 5K+
P: 9,731
Hi As per the last message ive now got some code together and it works, im trying to let the user select there account type during registration so i can structure content dependent on the role, the problem i now have is that i have 4 roles, Administrator, Customer, Landscaper, Wholesales, Code below.

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>
  2. <script runat="server">
  3.     Protected Sub ActivateStep(ByVal sender As Object, ByVal e As System.EventArgs)
  4.         ListBox1.DataSource = Roles.GetAllRoles()
  5.         ListBox1.DataBind()
  6.     End Sub
  7.  
  8.     Protected Sub DeActivateStep(ByVal sender As Object, ByVal e As System.EventArgs)
  9.         Roles.AddUserToRole(User.Identity.Name, ListBox1.SelectedValue)
  10.     End Sub
  11. </script>
  12.  
  13. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  14.     <asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
  15.         <WizardSteps>
  16.             <asp:CreateUserWizardStep runat="server">
  17.             </asp:CreateUserWizardStep>
  18.             <asp:WizardStep runat="server" Title="Choose Account Type" OnActivate="ActivateStep" OnDeactivate="DeActivateStep">
  19.                 Select Account Type:<br />
  20.                 <br />
  21.                 <asp:ListBox ID="ListBox1" runat="server" CssClass="normaltext"></asp:ListBox>
  22.             </asp:WizardStep>
  23.             <asp:CompleteWizardStep runat="server">
  24.             </asp:CompleteWizardStep>
  25.         </WizardSteps>
  26.     </asp:CreateUserWizard>
  27.  
  28.  
  29. </asp:Content> 
The problem is i dont want them to be able to add themseleves to the administrator role, but i couldnt work out another way to get the roles and assign them, i found a tutorial that got me this far but now im stuck, your help would be greatly appreciated

Thanks

What is your Role's object type and could you post the code for the GetAllRoles() method.

Thanks
Apr 21 '07 #7

P: 19
What is your Role's object type and could you post the code for the GetAllRoles() method.

Thanks
Thanks for being patient and replying but was able to do it myself, i changed my approach slightly and have pasted my code up incase its useful to any other newbie like me in the future. Thanks to the guys that replied

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>
  2. <script runat="server">
  3.     Protected Sub cuwUser_CreatedUser1(ByVal sender As Object, ByVal e As System.EventArgs)
  4.         Roles.AddUserToRole(cuwUser.UserName, DropDownList1.SelectedValue)
  5.     End Sub
  6. </script>
  7.  
  8. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  9.     <asp:CreateUserWizard ID="cuwUser" runat="server">
  10.         <WizardSteps>
  11.             <asp:CreateUserWizardStep runat="server">
  12.             </asp:CreateUserWizardStep>
  13.             <asp:WizardStep runat="server" Title="Accout Type" OnDeactivate="cuwUser_CreatedUser1">
  14.                 Select Account Type:<br />
  15.                 <br />
  16.                 <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
  17.                     DataTextField="RoleName" DataValueField="RoleName">
  18.                 </asp:DropDownList>
  19.                 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringAccountType %>"
  20.                     SelectCommand="SELECT [RoleName] FROM [vw_aspnet_Roles] WHERE ([RoleName] NOT LIKE '%' + @RoleName + '%')">
  21.                     <SelectParameters>
  22.                         <asp:Parameter DefaultValue="Administrator" Name="RoleName" Type="String" />
  23.                     </SelectParameters>
  24.                 </asp:SqlDataSource>
  25.             </asp:WizardStep>
  26.             <asp:CompleteWizardStep runat="server">
  27.             </asp:CompleteWizardStep>
  28.         </WizardSteps>
  29.     </asp:CreateUserWizard>
  30.  
  31.  
  32. </asp:Content>
I decided to drop the GetAllRoles() Method, its not mine and is part of .net 2.0 and visual studio library but its really useful but i couldnt modify that, instead i added an sqldatasource for the roles table and bound this to a drop down, i then added the sql query so that it wouldnt show anything with administrator,
Apr 22 '07 #8

Frinavale
Expert Mod 5K+
P: 9,731
Thanks for being patient and replying but was able to do it myself, i changed my approach slightly and have pasted my code up incase its useful to any other newbie like me in the future. Thanks to the guys that replied

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>
  2. <script runat="server">
  3.     Protected Sub cuwUser_CreatedUser1(ByVal sender As Object, ByVal e As System.EventArgs)
  4.         Roles.AddUserToRole(cuwUser.UserName, DropDownList1.SelectedValue)
  5.     End Sub
  6. </script>
  7.  
  8. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  9.     <asp:CreateUserWizard ID="cuwUser" runat="server">
  10.         <WizardSteps>
  11.             <asp:CreateUserWizardStep runat="server">
  12.             </asp:CreateUserWizardStep>
  13.             <asp:WizardStep runat="server" Title="Accout Type" OnDeactivate="cuwUser_CreatedUser1">
  14.                 Select Account Type:<br />
  15.                 <br />
  16.                 <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
  17.                     DataTextField="RoleName" DataValueField="RoleName">
  18.                 </asp:DropDownList>
  19.                 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringAccountType %>"
  20.                     SelectCommand="SELECT [RoleName] FROM [vw_aspnet_Roles] WHERE ([RoleName] NOT LIKE '%' + @RoleName + '%')">
  21.                     <SelectParameters>
  22.                         <asp:Parameter DefaultValue="Administrator" Name="RoleName" Type="String" />
  23.                     </SelectParameters>
  24.                 </asp:SqlDataSource>
  25.             </asp:WizardStep>
  26.             <asp:CompleteWizardStep runat="server">
  27.             </asp:CompleteWizardStep>
  28.         </WizardSteps>
  29.     </asp:CreateUserWizard>
  30.  
  31.  
  32. </asp:Content>
I decided to drop the GetAllRoles() Method, its not mine and is part of .net 2.0 and visual studio library but its really useful but i couldnt modify that, instead i added an sqldatasource for the roles table and bound this to a drop down, i then added the sql query so that it wouldnt show anything with administrator,
Good job :)

Thanks for updating us!

-Frinny
Apr 23 '07 #9

P: 1
Just edit the section
Expand|Select|Wrap|Line Numbers
  1.  if (AvailableRoles.Items[i].Selected == true)
  2.                 Roles.AddUserToRole(CreateUserWizard2.UserName, AvailableRoles.Items[i].Value);
  3.  
  4.   To THIS CODE 
  5. If admin is selected  in place of Admin  a default local role will be added
  6.  if (AvailableRoles.Items[i].Selected == true)
  7.             { 
  8.  
  9.                 if (AvailableRoles.Items[i].Text != "admin")
  10.                 {
  11.                     Roles.AddUserToRole(CreateUserWizard1.UserName, AvailableRoles.Items[i].Value);
  12.                 }
  13.                 else
  14.                 {
  15.                     Roles.AddUserToRole(CreateUserWizard1.UserName, "Member");
  16.                 }
  17.             }
Dec 27 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.