La mejor solución para personalizar los controles de página en función de algunos roles y configuraciones

I have several pages in asp.net each with lots of controls. I Also have some roles in my application that each has some setting options. Now I want to prepare my page based on these settings. Maybe it’s not too clear, so please take a look at my example.

Example: There are some buttons, some textboxes, some datetime picker, and a chart in a page, now what I want is when a user sees this page, the controls appear and disappear based on the users role. An important thing is that I don’t want to have only visible and invisible controls, in some scenarios I need to show controls with some customizations. For example change chart data source, limit selecting date time and so on.

The first solution that I can think of, is saving the settings in database and after visiting the page by user, the settings fetch from database and based on those, I can customize the controls with conditional phrases (if and else). But I suppose it is not a good approach and my page will get very messy.

Please help me with any better solutions and if you know good references about it, please let me know.

preguntado el 31 de julio de 12 a las 13:07

Take a look at the webpart and webpartmanager classes. -

2 Respuestas

Please see this link...use of ControlAdapters te puede ayudar...

Habilitación / deshabilitación de controles basada en roles en asp.net

contestado el 23 de mayo de 17 a las 13:05

You must use Thread.CurrentPrincipal.

A. When user login to your application, you attach his identity to thread, for example

string[] rolesArray = .....; //Get roles from dataBase by identity.
Thread.CurrentPrincipal = new YourCustomPrincipal(new YourCustomIdentity("YouName", "..."), rolesArray);

B. And when you navige about your application you test Thread.CurrentPrincipal

IPrincipal threadPrincipal = Thread.CurrentPrincipal;
if(threadPrincipal.Roles.Contains("roleTest"))
{
  //Adjust your control
}

Respondido 31 Jul 12, 13:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.