Roles in membership database

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
 
S

sloan

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);
}

}
 
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

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);
}

}


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
 
S

sloan

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

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 said:
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);
}

}


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top