Problem Adding users to Selected Role - (always adds to first role)


Hey all,

Below is my aspx.vb code and I am stumped as to why when I enter a user name then select a role the user is added to a role, but not the one I select in my dropdown.

The user is always added to the first role in the list in the dropdown not the role I selected. As well the RemoveUserFromRole doesn't work, I have to go into my DB and remove them manually.

Imports

System.Web.Security

Partial

Class AddRemoveUserRoles

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

DataBind()

End Sub

Public Sub AddUserToRole_Click(ByVal sender As Object, ByVal e As EventArgs)

Dim username As String = UsernameTextBox.Text.ToString

Dim roleName As String = RoleDropDownList.SelectedItem.Value.ToString

Roles.AddUserToRole(username, roleName)

End Sub

Public Sub RemoveUserFromRole_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Dim username As String = UsernameTextBox.Text.ToString

Dim roleName As String = RoleDropDownList.SelectedItem.Value.ToString

Roles.RemoveUserFromRole(username, roleName)

End Sub

Public Overrides Sub DataBind()

RoleList.DataSource = Roles.GetAllRoles()

RoleList.DataBind()

End Sub

End

Class
www.someguy.ca rantings from some Canadian guy
Follow me on twitter as well twitter.com/SomeCanadianGuy
0
Rivelyn
11/10/2006 3:43:54 PM
asp.net.security 27051 articles. 1 followers. Follow

9 Replies
832 Views

Similar Articles

[PageSpeed] 29

I've been trying many different code styles but still to no avail, I am sure it likely just something small I am over looking...
www.someguy.ca rantings from some Canadian guy
Follow me on twitter as well twitter.com/SomeCanadianGuy
0
Rivelyn
11/10/2006 7:11:40 PM
No ideas as to why this is not working?
www.someguy.ca rantings from some Canadian guy
Follow me on twitter as well twitter.com/SomeCanadianGuy
0
Rivelyn
11/11/2006 6:16:17 AM

do some debugging

add a break point on this line

Roles.AddUserToRole(username, roleName)

and move the mouse over the roleName

and see what is the roleName value that passed to the  AddUserToRole function(i think it will be the roleId Not RoleName)

 because the RoleDropDownList.SelectedItem.Value return the RoleId

and instead of this  

 Dim roleName As String = RoleDropDownList.SelectedItem.Value.ToString

write this

 Dim roleName As String = RoleDropDownList.SelectedItem.Text

 


Regards,

Anas Ghanem.


Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!

0
anas
11/11/2006 9:49:32 AM

But I am adding the information to the UserinRoles Table, the table colums are UserId and RoleId. Both of wich are uniqueidentifiers so I need the value of the text box inserted into the table not text itself.

Oh wait is that maybe it, roelName as String is not correct. Should roleName not be a new uniqueidentifier?

Anyway I will try what you sugested, thank you.


www.someguy.ca rantings from some Canadian guy
Follow me on twitter as well twitter.com/SomeCanadianGuy
0
Rivelyn
11/11/2006 3:07:05 PM
Sorry I ment a new guid
www.someguy.ca rantings from some Canadian guy
Follow me on twitter as well twitter.com/SomeCanadianGuy
0
Rivelyn
11/11/2006 3:08:53 PM

Maybe I am tired or something but I am really getting frustraited with this.

I have followed so many online tutorials and step by step examples from different source and everyone says this is easy, yet I still get errors.

I am working along with a video from that Scott guy from Vertigo Software on the Membership and Roles. Here is his code that works fine for him on the video

Public Sub ActivateStep(ByVal sender As Object, ByVal e As System.EventArgs)

DropDownList1.DataSource = Roles.GetAllRoles()

DropDownList1.DataBind()

End Sub

Public Sub DeactivateStep(ByVal sender As Object, ByVal e As System.EventArgs)

Roles.AddUsersToRole(User.Identity.Name, DropDownList1.SelectedValue)

End Sub

But when I do it I get the "User.Identity.Name" Error Value of type 'String' cannot be converted to '1-dimensional array of String'.

What the heck am I missing...lol....I just added a step to the add new user wizard to select a role that I have already created.....


www.someguy.ca rantings from some Canadian guy
Follow me on twitter as well twitter.com/SomeCanadianGuy
0
Rivelyn
11/12/2006 1:26:15 PM
Rivelyn:

Public Sub DeactivateStep(ByVal sender As Object, ByVal e As System.EventArgs)

Roles.AddUsersToRole(User.Identity.Name, DropDownList1.SelectedValue)

End Sub

But when I do it I get the "User.Identity.Name" Error Value of type 'String' cannot be converted to '1-dimensional array of String'.

What the heck am I missing...lol....I just added a step to the add new user wizard to select a role that I have already created.....

man You are using the wrong Function

Use AddUserToRole   Not  AddUsersToRole
and also this is a goooooood example for your case from microsoft 
im Very sure that it will help you 
http://msdn2.microsoft.com/en-us/library/system.web.security.roles.addusertorole.aspx
 
and this is a list of  the available Role functions For Users ..  Use The Bold One 
 
Public method Static   AddUsersToRole  	 Adds the specified users to the specified role.
Public method Static 	AddUsersToRoles 	Adds the specified users to the specified roles.
Public method Static 	AddUserToRole 	Adds the specified user to the specified role.
Public method Static 	AddUserToRoles 	Adds the specified user to the specified roles.
 
Reference : MSDN .. 
 
 
 
good luck 

Regards,

Anas Ghanem.


Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!

0
anas
11/12/2006 1:40:47 PM

OMG!.....I want to curl up into a ball and cry from embarrassment....I can't believe I actually over looked that......

Thank you very much for your observation anas....

You’re my hero!!!!!

 


www.someguy.ca rantings from some Canadian guy
Follow me on twitter as well twitter.com/SomeCanadianGuy
0
Rivelyn
11/12/2006 1:55:58 PM

if you solve your problem

and i hope that ... i want you just to mark the thread as answered ...

thanx


Regards,

Anas Ghanem.


Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!

0
anas
11/12/2006 2:04:11 PM
Reply:

Similar Artilces:

Roles
I'm using a CreateUserwizard control and in the code behind for CreatedUser event I am trying to add the following single line of code:  Roles.AddUserToRole ... etc. However, Intellisense does not show Roles. (I am seeing RoleServiceManager but not Roles) Any idea why? I have Visual Studio 2008 and the project is targeting .NET Framework V3.5. Also, I have <roleManager enabled="true"> in the web.config file. Intellisence will work automatically, There is no relation to web.config, if any problem use,using System.Web.Security; ...

Roles, roles, roles
Hey is it posible o have a role for a user to only update the content? I don't want that user to change skins or to make other admin changes... up I think if you allow a role to edit contents on a module level he wont be able to change anything else. Did you try that? cheers, erikErik van Ballegoij, The Netherlands if you allow a role to edit contents in a module lets say discussions module, then that role will be able to edit, delete the threads. so for a role to be able to add new thread only, do we need to write our own code?-keeara g------------------ keeara, see...

Creating User Account Without Default Security Role (Registered User Role)
  My DNN version is 3.1.1. The portal is set to Private Registration. Unauthenicated users have access to the basic pages of the site. We require my company's current customers to register and be approved to have access to all other pages that contain information restricted to them (the major part of the site). As you know, authorizing a user account applys the Registered User security role by default and this is how I control access to the customer areas.  We want to create a page that will be used by Marketing to allow potential customers by login to...

Problem : Get a list of user belonged to a role or to know if a user already has a role
What code can I write to know if a user belonged to a Role already ? and How can I get a list of a  user already in an existing role ? Please have a look at my code below : Protected Sub AddUsers_OnClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddUsersToRole.Click' I wanna add some code here to check if a user already belonged to a Role' Please insert your code here. Thanks a lot. ' A role must be selected If RolesListBox.SelectedItem Is Nothing Then Msg.Text = "Please select a role." Exit Sub End If ...

Roles.AddUserToRole is not adding user to specified role?
 Hi Iv created a basic site with a login and registration page. Everything is working correctly but when I try to use this bit of code to assign a user to a role on sign up  it just adds the user as an anonymous user and does not assign them to specified role. What am I doing wrong here.  <div id="registrationForm">                         <h3><asp:Label ID="registerLabel" runat="server" Text="Enter your ...

Can not retrieve Roles or Add Users to Roles.
I am completely stumped, I have followed more tutorials then I can count and still have no idea what is wrong. Please can someone have a look at my code and tell me what I am missing. Shortend for easier read. <%@ Page Language="VB" MasterPageFile="~/Administration/MasterPage.master" AutoEventWireup="false" CodeFile="PortalUser.aspx.vb" Inherits="Administration_PortalUser" title="eNet- Portal Users" %> <script runat="server"> Public Sub ActivateStep(ByVal sender As Object, ByVal e As System.EventArgs) DropDownList1.DataSource = Roles.GetAllRoles() DropDownList1.DataBind() ...

Add roles for user problem
Hello, I am trying to add roles for a user using the example found at http://weblogs.asp.net/scottgu/pages/Recipe_3A00_-Implementing-Role_2D00_Based-Security-with-ASP.NET-2.0-using-Windows-Authentication-and-SQL-Server.aspx .  I was able to run the demo with my db and edit roles fine so there should not be a problem with roleprovider.  My problem is I would like to create a new user then add roles.  The code add the user fine but no roles are added. Any ideas on what i'm missing or why the code below will not add roles?   Thanks,   KLB ...

(Add users to roles) problem
i used a SQL 2000 database to host the memebership and role providers database for my applicationwhen i try to add users to the roles i created , i get the following error :"cannot resolve collation conflict for equal to operation"when i tested and tracked this error , it found out that it occures @ line 44 of the stored procedure : aspnet_UsersInRoles_AddUsersToRolesi got this error when i copied the sp code from the aspnet database hosted @ the sqlserver 2005 express that is created with the application to my SQL 2000 database ANY IDEA HOW TO SOLVE THIS ?  I would use...

problem in add user to role
hello i'm using asp.net 2 with VB i have a register page in my site and i'm using the aspnetdb mempership and users tabls in that page "register" i have a create user wizard and in the created user event i have this code :Roles.AddUserToRole(User.Identity.Name, "teacher")"teacher" is the role that i want the current register user to be in it but it doesnt work when i go to the pages has the role teacher it show up the login page which mean the user is not in role can any one help me plz .thanks Here are a couple of ideas. I noticed you are doing this in a "created user event". Are you sur...

Problem adding new user to role
Good day! I have a problem with adding Authorized User to created by me new role. 1. I have connected to Jaguar Manager, have created new Role "My_User" and have tried to add Authorized User to new role. 2. After I entered the name of new user, I received: "Error Adding Member: hostname:9000-connection refused" and such message in srv.log file: "Apr 24 12:15:14 2002: Warning: DLL name is different, using previous name Apr 24 12:15:14 2002: Warning: DLL name is different, using previous name Apr 24 12:15:17 2002: Starting Jaguar CTS 3.0.1 Apr 24 1...

Adding roles to add user wizzard
Hi I have seen a few posts where they have added a user role to create user wizzard but they all add loads of other stuff to the information the user has to enter I have the add user wizzard under an admin folder and by default the wizzard does not add a role to the user I just want a radio button or drop down box to add a role to the user when they are created  how can I do this withough adding loads of new fields to the form  thanks mike Scott's approach works fine, just remove any of the Profile related stuff if you don't need it: http://weblogs.asp.net/scottgu...

Add user wizard and Adding roles
I am creating a website that has role based authentication.  I have created several roles for my site: Super, Admin, Teacher, Student. I want to dynamically be able to choose them during the add user wizard.  Is there a tutorial out their that or walkthrough for adding a Wizard step to the UserWizard for Roles (VB Please).  I have been able to add the step to the adduserwizard, and have been able to dynamically call the roles into a check box control. Now how to I add user to roles when selected? (VB Please)Thanks Scott Here is the code that you need:  Dim role As Stri...

Membership and Role Management
Hello, I've configured SQL server 2005 as my back-end data store via aspnet_regsql.exe. I presume it was succesvol. Creating users en roles programmatically is no problem. When I start the Web Site Administration Tool I can see the created users and roles. But when I want to show them by code I get an empty result. ... ... Roles.GetAllRoles()...... membership.GetAllUsers() The roleManager is enabled in web.config.<roleManager defaultProvider="SqlProvider" enabled="true" cacheRolesInCookie="true" cookieName=".ASPROLES" cookieTimeout="30" cookiePath="/" cookieRequireSS...

Add User Wizard fail after selection of roles
hi pls help....i have followed the video on membership & roles in the "How do I" series of video on http://www.asp.net/learn/videos/video-45.aspx where the selection of roles when creating new user was taught  Using the add user wizard, after entry of the new user details, I manage to be directed to the list box where are the roles in my website are shown for selection. The problem lies when after selection of the desired roles and clicking on the Finish button, my browser show "Internet Explorer cannot display the webpage" . I was expec...

Problem in adding user/roles. please advise
I am a student, In Visual studio 2005 , I created two roles Admin and User from security tab. When I am trying to create a user it is not allowing. giving error " Password minimum length is 7 and alphanumeric value 1". Please suggest something. Thanks Kalloo    in the web.config there is a section on membership that sets the user requirements (password length, unique email, etc)...  Please set your requirements here <membership ...>  <providers>    <add minRequiredPasswordLength=.. minRequiredNonalphanumericCharacters=.....

Web resources about - Problem Adding users to Selected Role - (always adds to first role) - asp.net.security

Portal:Bollywood/Selected biography - Wikipedia, the free encyclopedia
Write three or four paragraphs in the text field using information from the selected notable article, you may find it useful to examine the existing ...

F8: Facebook’s FbStart Initiative Will Give Selected Developers A $30K Kick-Start
... successful applications is likely a whole lot easier with $30,000 in free tools and services to start off with, and that’s exactly what selected ...

LEGO Shop - The LEGO Technic team has selected the winner... - Facebook
The LEGO Technic team has selected the winner of the Co-Creation Challenge! LEGO will be producing a limited run of 20,000 of this stunning set.... ...

Grand Doctor Ma Selected Students-baby365 on the App Store on iTunes
Get Grand Doctor Ma Selected Students-baby365 on the App Store. See screenshots and ratings, and read customer reviews.

Selected Cheney Vice Presidential Photographs
... for photos found within these albums are Paul Morse, Karen Ballard, Kimberlee Hewitt, Shealah Craighead, and Chris Greenberg. These selected ...

[iOS Advice] How To Clarify Your Email Replies By Quoting Selected Text - YouTube
Follow me on Twitter: http://www.twitter.com/macmixing Like my Facebook page: http://www.facebook.com/macmixing RATE, COMMENT, SUBSCRIBE! :-) ...

UAE Under-19 cricketers for ACC Premier League tour of Malaysia selected - The National
Coach Aaqib Javed seeking fresh blood for senior team after missing out on World Twenty20

Animal of the Day: Lucius Has Been Selected as the U.K.’s ‘Happiest Cat’
He’s got one blue eye, one green eye and a heart of gold. British mobile phone company O2 recently held some “cat auditions” to find “the UK’s ...

Australian director Sue Brooks 'gobsmacked' as Looking for Grace selected for Venice Film Festival competition ...
... filmmaker Sue Brooks says she is "gobsmacked". Her drama Looking For Grace , shot in the Western Australian wheat belt, has been selected for ...

Three Canberra products selected in AFL state combines
Belconnen junior Chris Jansen has been selected in South Australia's state AFL combine.

Resources last updated: 1/16/2016 2:41:52 PM