Membership Role Assignment through CreateUserWizard

Last post 04-01-2007 6:50 AM by mstubb. 16 replies.

Sort Posts:

  • Membership Role Assignment through CreateUserWizard

    03-18-2007, 1:15 PM
    • Member
      point Member
    • drabindran
    • Member since 03-18-2007, 5:12 PM
    • Posts 1

    Hey just a question.  When using the wizard provided is there away to automatically assign that new user to a role which exists?

    Thanks!

  • Re: Membership Role Assignment through CreateUserWizard

    03-18-2007, 6:24 PM
    • Contributor
      2,507 point Contributor
    • omerkamal
    • Member since 02-06-2006, 2:47 PM
    • Germany
    • Posts 513

     

    after you have created the User call AddUserToRole static method of Roles Object.

     Roles.AddUserToRole("UserName", "RoleName"); 
      
  • Re: Membership Role Assignment through CreateUserWizard

    03-19-2007, 4:26 AM
    • Member
      30 point Member
    • mstubb
    • Member since 02-28-2007, 1:28 PM
    • Southampton - Hampshire England
    • Posts 19

    Hi there

    This sort of answers a question I was going to ask but can you provide more info as to where you put this line of code. Iam currently building my site on the back of the Job Site starter kit. Currently I have no option but to keep the drop down for the user to select which role, when I only ever want all users to be in one role. I did try and take away this option but the page brought up errors, so I have just left it in.

    I would appreciate your help.

    Best Regards
    Mark
  • Re: Membership Role Assignment through CreateUserWizard

    03-20-2007, 1:53 AM
    Answer

    you could add code to the createduser event like this of the wizard:

     

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)

    {

     

    string[] usersArray = new string[1];

    usersArray[0] = CreateUserWizard1.UserName;

     

    Roles.AddUsersToRole(usersArray, "Admin");

    }

    }

    http://forums.asp.net/thread/1272697.aspx

    Zhao Ji Ma
    Sincerely,
    Microsoft Online Community Support

    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: Membership Role Assignment through CreateUserWizard

    03-20-2007, 1:26 PM
    • Member
      30 point Member
    • mstubb
    • Member since 02-28-2007, 1:28 PM
    • Southampton - Hampshire England
    • Posts 19

    Hi Thanks for the reply. However Iam still a little confused.

     My CreatAcccount.aspx.vb code looks like this.Where do I add the code, as where ever I add it I get an error, ie wavy lines below the code.

    Partial Class createAccount

    Inherits System.Web.UI.Page

    Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser

     

    End Sub

    Protected Sub Password_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    End

    Class
    Best Regards
    Mark
  • Re: Membership Role Assignment through CreateUserWizard

    03-20-2007, 10:20 PM
    • Participant
      781 point Participant
    • hypercode
    • Member since 07-15-2006, 2:42 PM
    • Posts 250

    You can put it here. However, I used it in the "CreatingUser" event.

    Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser

     Roles.AddUserToRole("UserName", "RoleName");

    End Sub

  • Re: Membership Role Assignment through CreateUserWizard

    03-20-2007, 10:48 PM
    Answer

    Hi,

    What is the error message?

    Since the function name is the event handler name, so you should tell the control which method is the handler in ASPX file:

          <asp:createuserwizard id="CreateUserWizard1" runat="server" oncreateduser="CreateUserWizard1_CreatedUser">

    Please check this too.

    Zhao Ji Ma
    Sincerely,
    Microsoft Online Community Support

    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: Membership Role Assignment through CreateUserWizard

    03-28-2007, 2:28 PM
    • Member
      30 point Member
    • mstubb
    • Member since 02-28-2007, 1:28 PM
    • Southampton - Hampshire England
    • Posts 19

    Hi there

    Thanks for the prompt reply, I would have answered sooner but I have been away. I added the code as you suggested but it did not like the ; after the ) so it looked like this

    Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser

     Roles.AddUserToRole("UserName", "RoleName")

    End Sub

    However when I tested it I gor the following error

     

    The user 'UserName' is already in role 'user'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Configuration.Provider.ProviderException: The user 'UserName' is already in role 'user'.

    Source Error:

    Line 5:      Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser
    Line 6:  
    Line 7:          Roles.AddUserToRole("Username", "user")
    Line 8:  
    Line 9:      End Sub

    Source File: D:\Web Design\hush samples\Hush Star\createAccount.aspx.vb    Line: 7

    Best Regards
    Mark
  • Re: Membership Role Assignment through CreateUserWizard

    03-28-2007, 5:50 PM
    • Participant
      781 point Participant
    • hypercode
    • Member since 07-15-2006, 2:42 PM
    • Posts 250

    Take thisout out of the <asp> tag and try it: oncreateduser="CreateUserWizard1_CreatedUser

     

    <asp:createuserwizard id="CreateUserWizard1" runat="server" oncreateduser="CreateUserWizard1_CreatedUser">

  • Re: Membership Role Assignment through CreateUserWizard

    03-29-2007, 2:12 PM
    • Member
      30 point Member
    • mstubb
    • Member since 02-28-2007, 1:28 PM
    • Southampton - Hampshire England
    • Posts 19

    Hi sorry Iam still none the wiser. Thanks for your help and patients but this is really getting to me, to the point where I cannot get it to work and want to give up.

    I have started again and created a new page called join.aspx I have added the within the custom content a CreateUser Task Wizard using the default settings so it looks like this.

    <%

    @ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="join.aspx.vb" Inherits="join" title="Untitled Page" %><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
    <WizardSteps><asp:CreateUserWizardStep runat="server">
    </asp:CreateUserWizardStep>
    <asp:CompleteWizardStep runat="server">
    </asp:CompleteWizardStep></WizardSteps>
    </asp:CreateUserWizard>
    </
    asp:Content>

     The join.aspx.vb looks like this

    Partial Class join

    Inherits System.Web.UI.Page

    End

    Class

    What do I have to add to the code in the aspx. and aspx.vb pages so that I can ensure all new people who sign up become members of the user Role.

     

    Best Regards
    Mark
  • Re: Membership Role Assignment through CreateUserWizard

    03-29-2007, 5:13 PM
    • Participant
      781 point Participant
    • hypercode
    • Member since 07-15-2006, 2:42 PM
    • Posts 250

    First, do you have a Role created? If not, use the Web Site Administration Tool to create a Role.

    Then put this on the join.aspx.vb page:

    Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser

     Roles.AddUserToRole("UserName", "RoleName");

    End Sub

    .................

    Change RoleName in the Sub to the name of the Role that you created. Leave "UserName" as is.

    Go through some tutorials on this stuff and then you'll understand it better. Here's a link to some tutorials:

    http://msdn.microsoft.com/vstudio/express/beginner/reference/aspnet/default.aspx

    Good Luck

  • Re: Membership Role Assignment through CreateUserWizard

    03-29-2007, 7:32 PM
    • Participant
      792 point Participant
    • accelerator
    • Member since 12-01-2003, 10:35 AM
    • United Kingdom
    • Posts 192

    Hi

    I think there could possibly be an error with this line: 

    Roles.AddUserToRole("UserName", "RoleName");

    Looking at some code examples, I found the following for illustration, it will need modifying for your code: 

    Roles.AddUserToRole(Username.Text, RoleList.SelectedItem.Value)

    Maybe yours should be:

    Roles.AddUserToRole(UserName.Text, RoleName.Text);

    The point I am trying to make is that it might be that the way you have it at the moment, it is not reading the Text property of the UserName properly.  So say you try and create a UserName of Morgan, with your existing code this is read as Username, but if you used "Username.Text" instead it would be correctly read as Morgan. 

    Rgds

    Accelerator


     

  • Re: Membership Role Assignment through CreateUserWizard

    03-30-2007, 12:06 PM
    • Member
      30 point Member
    • mstubb
    • Member since 02-28-2007, 1:28 PM
    • Southampton - Hampshire England
    • Posts 19

    Hi There

    Thanks for taking the time to reply to my question.  My join.aspx.vb now looks like this, though now with errors!!

    Partial Class join

    Inherits System.Web.UI.Page

    Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser

    Roles.AddUserToRole(UserName.Text, RoleName.Text);

    End Sub

    End Class

    I now have blue wavey lines below the UserName and RoleName and also below the ; at the end of the line after the ) I have shown it above using the underline to simulate what I can see. error smart tag messages for the two blue wavey lines are

    Name 'UserName' is not declared
    and
    Name 'RoleName' is not declared
    and Finally
    Chracter not valid

    All I want to do is use the default Create user Wizard and make every user a member of the user role. I am quickly loosing my mind. I really feel out of my deapth and running out of time. Can anyone please help me.  

     

    Best Regards
    Mark
  • Re: Membership Role Assignment through CreateUserWizard

    03-30-2007, 12:08 PM
    • Member
      30 point Member
    • mstubb
    • Member since 02-28-2007, 1:28 PM
    • Southampton - Hampshire England
    • Posts 19

    Hi There

    Thanks for taking the time to reply to my question.  My join.aspx.vb now looks like this, though now with errors!!

    Partial Class join

    Inherits System.Web.UI.Page

    Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser

    Roles.AddUserToRole(UserName.Text, RoleName.Text);

    End Sub

    End Class

    I now have blue wavy lines below the UserName and RoleName and also below the ; at the end of the line after the ) I have shown it above using the underline to simulate what I can see. error smart tag messages for the two blue wavy lines are

    Name 'UserName' is not declared
    and
    Name 'RoleName' is not declared
    and Finally
    Character not valid

    All I want to do is use the default Create user Wizard and make every user a member of the user role. I am quickly loosing my mind. I really feel out of my depth and running out of time. Can anyone please help me.  

     

    Best Regards
    Mark
  • Re: Membership Role Assignment through CreateUserWizard

    03-30-2007, 2:32 PM
    Answer
    • Contributor
      2,507 point Contributor
    • omerkamal
    • Member since 02-06-2006, 2:47 PM
    • Germany
    • Posts 513
    1. Make sure your TextBox which take ths User Name is Called "UserName".
    2. I think your TextBox Lied in the CreateUserWizard1. so replace the code like this:

    dim username as string= CType(WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl("UserName"), TextBox).Text

    dim role as string= CType(WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl("RoleName"), TextBox).Text

    Roles.AddUserToRole(username,role);

    Where:

    UserName and RoleName are the Text Boxes for User Name and the Role Name.

     Which are added to ContentTemplate of the CreateUserWizardStep.

    See the .aspx Page side:

     

    <asp:CreateUserWizard ID="WizardCreateUser" runat="server" ContinueDestinationPageUrl="~/Default.aspx"
          OnCreatedUser="WizardCreateUser_CreatedUser">
           <WizardSteps>
            <asp:CreateUserWizardStep ID="CreateUserWizardStep1"  runat="server">
              <ContentTemplate>
                  <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                  <asp:TextBox ID="RoleName" runat="server" ></asp:TextBox>
               </ContentTemplate>                           
             </asp:CreateUserWizardStep>
           <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server" />
           </WizardSteps>               
    </asp:CreateUserWizard>       
    
       

Page 1 of 2 (17 items) 1 2 Next >