471,316 Members | 1,336 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,316 software developers and data experts.

Roles question

I am writing an application using the ASP.NET Configuration Roles and
Users.

The problem I have, in my C# I need to work out which type of user just
logged in. I am currently using:

string[] role = Roles.GetRolesForUser();

This give me a string "Administrator" etc.

My C# code looks like this:

switch(userType)
{
case "Administrator":
//Do soemthing

//and so on...
}

Has anyone come up with a better way for doing this?

The problems I see with this, if the Role is renamed (would be deleted
and recreated to be called "Admin" for example or a new Role is added.

Any help / advice on this would be appreciated.

Regards,

Steven
*** Sent via Developersdex http://www.developersdex.com ***
Jan 4 '06 #1
5 1318
Steven,

Do you allow more than one role per person? If you do, then you need to
account for that, and a switch statement won't handle that.

If the role is renamed, then you have to change your code. I mean,
there has to be some level of consistency somewhere.

If you want, you should define aliases for your roles that you will
always use, that are linked to whatever descriptive names you give them.
However, if you use code based security, it doesn't really help, since the
name of your role has to match with the role that you specify in the
attribute.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steven Blair" <st**********@btinternet.com> wrote in message
news:eX**************@TK2MSFTNGP10.phx.gbl...
I am writing an application using the ASP.NET Configuration Roles and
Users.

The problem I have, in my C# I need to work out which type of user just
logged in. I am currently using:

string[] role = Roles.GetRolesForUser();

This give me a string "Administrator" etc.

My C# code looks like this:

switch(userType)
{
case "Administrator":
//Do soemthing

//and so on...
}

Has anyone come up with a better way for doing this?

The problems I see with this, if the Role is renamed (would be deleted
and recreated to be called "Admin" for example or a new Role is added.

Any help / advice on this would be appreciated.

Regards,

Steven
*** Sent via Developersdex http://www.developersdex.com ***

Jan 4 '06 #2
Yes a user can belong to more than one group.

Is there an alternative to using code based security, or am I gonna have
to accept a trade off?

I do take your point on board regarding users belonging to more than one
group. I might revisit this and simply setup one user with one role.

Regards,

Steven

*** Sent via Developersdex http://www.developersdex.com ***
Jan 4 '06 #3
Steven,

There are alternatives, but honestly, why use them when it is baked into
the framework? It's kind of foolish to try and write your own code which
you have to test, debug, maintain, blah, blah, blah.

And even if you don't use role-based security, the issue still remains,
your code isn't psychic. It can't tell if you add new roles which have
specific meanings unless you tell it so.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steven Blair" <st**********@btinternet.com> wrote in message
news:ux**************@TK2MSFTNGP12.phx.gbl...
Yes a user can belong to more than one group.

Is there an alternative to using code based security, or am I gonna have
to accept a trade off?

I do take your point on board regarding users belonging to more than one
group. I might revisit this and simply setup one user with one role.

Regards,

Steven

*** Sent via Developersdex http://www.developersdex.com ***

Jan 4 '06 #4
Maybe I am missing soemthing here.

I want to avoid re-inventing the wheel. Ideally, I want to use the
inbuilt security features completely.

My application allows different users varied access on my app, which is
great. But, the problem is, if "Role1" logs in, I need to filter some
data being returned. If "Role" logs in, same again, some filter on the
data.

Is this possible using only the inbuilt security features, or do I have
to add some C# code to cater for problems like this?

My feeling is that code is required on top of the Roles for my
particular problem, and if thats the case, was looking for suggestions
on how to make this as easy as possible.

Thanks again for the help.

Regards,

Steven

*** Sent via Developersdex http://www.developersdex.com ***
Jan 4 '06 #5
Steven,
Yes, you will need to use some code, but not that much.

You basically do what you have to do. However, your original question
was about what to do when you change the names of the roles, which is what
most of my responses have been referring to.

Basically, you get the roles, and can check what to return based on
those roles. However, the framework will handle the assigning of the roles
for you (in ASP.NET configuration roles), so you shouldn't have to worry
about that part.

What you ^could^ do is have your functions that return data marked with
the PrincipalPermission attribute. You can specify the roles that are
allowed to access the function, and if someone tries to access the function
and is not in the role, then a SecurityException will be thrown.

This would require you to split your functions out into more intricate
groups, but would make security easy.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steven Blair" <st**********@btinternet.com> wrote in message
news:OW*************@TK2MSFTNGP09.phx.gbl...
Maybe I am missing soemthing here.

I want to avoid re-inventing the wheel. Ideally, I want to use the
inbuilt security features completely.

My application allows different users varied access on my app, which is
great. But, the problem is, if "Role1" logs in, I need to filter some
data being returned. If "Role" logs in, same again, some filter on the
data.

Is this possible using only the inbuilt security features, or do I have
to add some C# code to cater for problems like this?

My feeling is that code is required on top of the Roles for my
particular problem, and if thats the case, was looking for suggestions
on how to make this as easy as possible.

Thanks again for the help.

Regards,

Steven

*** Sent via Developersdex http://www.developersdex.com ***

Jan 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Marty Underwood | last post: by
1 post views Thread by Charles | last post: by
5 posts views Thread by hansiman | last post: by
5 posts views Thread by Jonathan Allen | last post: by
2 posts views Thread by Francis Reed | last post: by
9 posts views Thread by Mike Hofer | last post: by
3 posts views Thread by rudolf.ball | last post: by
7 posts views Thread by MyndPhlyp | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.