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

Roles in membership database

P: n/a
AC
Hi there. My asp.net 2.0 development website uses roles to control
access to sections of my site, configured using the asp.net
configuration tool, which is great. Except that isn't available once
published on the public, third-party hosted site. So I need to be
able to configure access through a new section of the site, available
only to admins/developers, which I'm about to write.

First off, I want to create a role in the database to represent users
who can access this area of the site - and I need to be able to do it
manually so I can test as I'm developing. I've created my new role in
my membership database by adding a row to the aspnet_Roles table and
added a user to that role by adding a row to the aspnet_UsersInRoles
table. However, Roles.GetRolesForUser() returns a zero-dimensioned
string array so I must have missed a trick somewhere ... can anyone
point me in the right direction please?

The membership database works fine in that any old user can come
along, register, gain access to members-only sections, etc., it's just
the roles-without-aspnet-config-tool I seem to be having trouble
with.

Thanks for your time,
AC

Mar 19 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a

You can't use that tool remotely as you've found out.
Here is the big 3 methods. I've wrappped them up for you.

// private List<string_toDoUserAndRoleValues = new List<string>();
ex john,admin
private void MapUsersToRoles()
{

char char1 = Convert.ToChar(",");

char[] splitter = { char1 };
foreach (string userAndRole in this._toDoUserAndRoleValues)
{

string[] splitUserNameAndRole = userAndRole.Split(splitter);

string userName = splitUserNameAndRole[0];
string roleName = splitUserNameAndRole[1];

if (!Roles.IsUserInRole (userName, roleName ))
{

Roles.AddUsersToRole(new string[] { userName } ,
roleName );
_actuallyImportedList.Add(string.Format("{0} / {1}",
userName, roleName));
}

}

}

private void CreateRole(string roleName)
{

if(!Roles.RoleExists (roleName ))
{
Roles.CreateRole(roleName);
this._allImportedRoles.Add(roleName);
}

}
private void CreateUser(string userName, string password)
{

if (null==System.Web.Security.Membership.GetUser (userName))
{
System.Web.Security.Membership.CreateUser(userName ,
password);
this._allImportedUser.Add(userName);
}

}

"AC" <an**********@gmail.comwrote in message
news:11**********************@p15g2000hsd.googlegr oups.com...
Hi there. My asp.net 2.0 development website uses roles to control
access to sections of my site, configured using the asp.net
configuration tool, which is great. Except that isn't available once
published on the public, third-party hosted site. So I need to be
able to configure access through a new section of the site, available
only to admins/developers, which I'm about to write.

First off, I want to create a role in the database to represent users
who can access this area of the site - and I need to be able to do it
manually so I can test as I'm developing. I've created my new role in
my membership database by adding a row to the aspnet_Roles table and
added a user to that role by adding a row to the aspnet_UsersInRoles
table. However, Roles.GetRolesForUser() returns a zero-dimensioned
string array so I must have missed a trick somewhere ... can anyone
point me in the right direction please?

The membership database works fine in that any old user can come
along, register, gain access to members-only sections, etc., it's just
the roles-without-aspnet-config-tool I seem to be having trouble
with.

Thanks for your time,
AC

Mar 19 '07 #2

P: n/a
AC
OK so you're saying I can do it by writing something using the
Membership classes - thanks for your help. Shame I can't
circumnavigate the first stage and simply stick something straight
into the database to allow ME access but - I wouldn't want it to be
easy !!! ...

Thanks again,
AC

On Mar 19, 9:33 pm, "sloan" <s...@ipass.netwrote:
You can't use that tool remotely as you've found out.

Here is the big 3 methods. I've wrappped them up for you.

// private List<string_toDoUserAndRoleValues = new List<string>();
ex john,admin
private void MapUsersToRoles()
{

char char1 = Convert.ToChar(",");

char[] splitter = { char1 };

foreach (string userAndRole in this._toDoUserAndRoleValues)
{

string[] splitUserNameAndRole = userAndRole.Split(splitter);

string userName = splitUserNameAndRole[0];
string roleName = splitUserNameAndRole[1];

if (!Roles.IsUserInRole (userName, roleName ))
{

Roles.AddUsersToRole(new string[] { userName } ,
roleName );
_actuallyImportedList.Add(string.Format("{0} / {1}",
userName, roleName));
}

}

}

private void CreateRole(string roleName)
{

if(!Roles.RoleExists (roleName ))
{
Roles.CreateRole(roleName);
this._allImportedRoles.Add(roleName);
}

}

private void CreateUser(string userName, string password)
{

if (null==System.Web.Security.Membership.GetUser (userName))
{
System.Web.Security.Membership.CreateUser(userName ,
password);
this._allImportedUser.Add(userName);
}

}

"AC" <andy.coll...@gmail.comwrote in message

news:11**********************@p15g2000hsd.googlegr oups.com...
Hi there. My asp.net 2.0 development website uses roles to control
access to sections of my site, configured using the asp.net
configuration tool, which is great. Except that isn't available once
published on the public, third-party hosted site. So I need to be
able to configure access through a new section of the site, available
only to admins/developers, which I'm about to write.
First off, I want to create a role in the database to represent users
who can access this area of the site - and I need to be able to do it
manually so I can test as I'm developing. I've created my new role in
my membership database by adding a row to the aspnet_Roles table and
added a user to that role by adding a row to the aspnet_UsersInRoles
table. However, Roles.GetRolesForUser() returns a zero-dimensioned
string array so I must have missed a trick somewhere ... can anyone
point me in the right direction please?
The membership database works fine in that any old user can come
along, register, gain access to members-only sections, etc., it's just
the roles-without-aspnet-config-tool I seem to be having trouble
with.
Thanks for your time,
AC

Mar 19 '07 #3

P: n/a

I actually wrote a .txt file reader, which reads the lines, and calls the
code below.

One file has users
One file has roles
One file has user-roles relationships.

With the code below, the other code would be simple.

Then I can deploy a new website or update one, with the creation of a few
..txt files.

roles.txt
SuperAdmin
Admin
RegularUser
Guest
users.txt
John,jpassword123
Mary,mpassword234
user_role.txt
John,SuperAdmin
Mary,Admin
John,RegularUser
Mary,RegularUser

"AC" <an**********@gmail.comwrote in message
news:11**********************@p15g2000hsd.googlegr oups.com...
OK so you're saying I can do it by writing something using the
Membership classes - thanks for your help. Shame I can't
circumnavigate the first stage and simply stick something straight
into the database to allow ME access but - I wouldn't want it to be
easy !!! ...

Thanks again,
AC

On Mar 19, 9:33 pm, "sloan" <s...@ipass.netwrote:
You can't use that tool remotely as you've found out.

Here is the big 3 methods. I've wrappped them up for you.

// private List<string_toDoUserAndRoleValues = new
List<string>();
ex john,admin
private void MapUsersToRoles()
{

char char1 = Convert.ToChar(",");

char[] splitter = { char1 };

foreach (string userAndRole in this._toDoUserAndRoleValues)
{

string[] splitUserNameAndRole =
userAndRole.Split(splitter);

string userName = splitUserNameAndRole[0];
string roleName = splitUserNameAndRole[1];

if (!Roles.IsUserInRole (userName, roleName ))
{

Roles.AddUsersToRole(new string[] { userName } ,
roleName );
_actuallyImportedList.Add(string.Format("{0} / {1}",
userName, roleName));
}

}

}

private void CreateRole(string roleName)
{

if(!Roles.RoleExists (roleName ))
{
Roles.CreateRole(roleName);
this._allImportedRoles.Add(roleName);
}

}

private void CreateUser(string userName, string password)
{

if (null==System.Web.Security.Membership.GetUser (userName))
{
System.Web.Security.Membership.CreateUser(userName ,
password);
this._allImportedUser.Add(userName);
}

}

"AC" <andy.coll...@gmail.comwrote in message

news:11**********************@p15g2000hsd.googlegr oups.com...
Hi there. My asp.net 2.0 development website uses roles to control
access to sections of my site, configured using the asp.net
configuration tool, which is great. Except that isn't available once
published on the public, third-party hosted site. So I need to be
able to configure access through a new section of the site, available
only to admins/developers, which I'm about to write.
First off, I want to create a role in the database to represent users
who can access this area of the site - and I need to be able to do it
manually so I can test as I'm developing. I've created my new role in
my membership database by adding a row to the aspnet_Roles table and
added a user to that role by adding a row to the aspnet_UsersInRoles
table. However, Roles.GetRolesForUser() returns a zero-dimensioned
string array so I must have missed a trick somewhere ... can anyone
point me in the right direction please?
The membership database works fine in that any old user can come
along, register, gain access to members-only sections, etc., it's just
the roles-without-aspnet-config-tool I seem to be having trouble
with.
Thanks for your time,
AC


Mar 20 '07 #4

P: n/a
On 19 Mar 2007 13:56:34 -0700, AC wrote:
Hi there. My asp.net 2.0 development website uses roles to control
access to sections of my site, configured using the asp.net
configuration tool, which is great. Except that isn't available once
published on the public, third-party hosted site.
You might be interested in this:

http://msdn2.microsoft.com/en-us/library/aa478947.aspx
Mar 26 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.