469,950 Members | 2,029 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Roles in membership database

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
4 3065

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
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

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
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.

Similar topics

1 post views Thread by ijoxley | last post: by
5 posts views Thread by JP.Gantlin | last post: by
1 post views Thread by =?Utf-8?B?TWFya0F1cml0?= | last post: by
2 posts views Thread by StinkyDuck | last post: by
1 post views Thread by =?Utf-8?B?ZVByaW50?= | last post: by
2 posts views Thread by Vincent | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.