[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 800
  • Last Modified:

Show or Hide a field based on Role status.

ASP.net 2.0
I'm using Roles in my web site.
How do I create and If statment to say:
IF (User Role = Administrator) then
<asp:CommandField ShowEditButton="True" />
ELSE
<asp:CommandField ShowEditButton="False" />
END IF

Then were do I put this code (in the code behide? or on the page it's self)?


<asp:GridView ID="GridView2" runat="server" DataSourceID="XmlDataSource1">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
            </Columns>
        </asp:GridView>
0
quest_capital
Asked:
quest_capital
1 Solution
 
RedKelvinCommented:
Ok here goes,

This code for example will give you all the role info for the current user

        private void Form1_Load(object sender, EventArgs e)
        {
            AppDomain myDomain = Thread.GetDomain();

            myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
            WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;

            Console.WriteLine("{0} belongs to: ", myPrincipal.Identity.Name.ToString());

            Array wbirFields = Enum.GetValues(typeof(WindowsBuiltInRole));

            foreach (object roleName in wbirFields)
            {
                try
                {
                    Console.WriteLine("{0}? {1}.", roleName,
                        myPrincipal.IsInRole((WindowsBuiltInRole)roleName));
                }
                catch (Exception)
                {
                    Console.WriteLine("{0}: Could not obtain role for this RID.",
                            roleName);
                }
            }
        }

http://msdn2.microsoft.com/en-us/library/system.security.principal.windowsprincipal.isinrole(VS.71).aspx


So your answer really is

            AppDomain myDomain = Thread.GetDomain();

            myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
            WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;

            if (myPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                  MyButton.Visible = true
            }
             else
             {
                  MyButton.Visible = false
             }

you will also need to add these to the top of your class
using System.Threading;
using System.Security.Principal;


Note also that you can check for other roles using WindowsBuiltInRole, administrator is defined in the example

RedK
0
 
RedKelvinCommented:
That is all C# code for the codebehind class

You could also structure it like this, create a function like so in your codebehind

public bool AmIAdmin()
{
            AppDomain myDomain = Thread.GetDomain();

            myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
            WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;

            if (myPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                  return = true;
            }
             else
             {
                  return = false;
             }
}

then anywhere in the codebehind you can go

if (AmIAdmin)
{
       MyButton.Visible = true;
}
else
{
       MyButton.Visible = false;
}
0
 
telefoniaCommented:
may this help:
1- add:
<asp:CommandField id="button1" runat="server"  ShowEditButton="True" />
2. OnDataBind() event use it.
IF (User Role = Administrator) then
ctype(e.item.findcontrol("button1"), CommandField).ShowEditButton="True"
ELSE
ctype(e.item.findcontrol("button1"), CommandField).ShowEditButton="False"
END IF
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
quest_capitalAuthor Commented:
RedKelvin:

That seem much to complecated to just get the users role.
this does not work be wouldn't it be something simple like
if(Roles.Equals("Admin"))
        {
}
This should be stored in a session right?
0
 
RedKelvinCommented:
Something else, any control in your markup, that you want to expose to the codebehind will need an ID, such as

ID="MyButton"

and you will need to set the runat property to server

runat="server"
0
 
RedKelvinCommented:
Ah yes you are right

It is User.IsinRole

What I posted before gets the machine role, User.IsinRole gets the asp role
0
 
RedKelvinCommented:
0
 
quest_capitalAuthor Commented:
RedKelvin:

User.IsInRole
tells you what role the user is in

I just need to do something like
if(Roles.Equals("Admin"))
        {
            Label1.Text = "Admin";
        }
but this gives you an error
0
 
RedKelvinCommented:
Yeah, I reckon you need to do it with IsInRole, like this

if(User.IsInRole("Admin"))
        {
            Label1.Text = "Admin";
        }
0
 
quest_capitalAuthor Commented:
RedKelvin:
if(User.IsInRole("Admin"))
        {
            Label1.Text = "Admin";
        }
and
if(Roles.Equals("Admin"))
        {
            Label1.Text = "Admin";
        }
but this gives you an error

do not work
0
 
quest_capitalAuthor Commented:
if(User.IsInRole("Admin"))
        {
            Label1.Text = "Admin";
        }
Do I need a namespce for this?
Is this just a VB thing?
do I need to ad like:
this.user.IsInRole
or
bah.user.IsInRole
0
 
ventaurCommented:
Use the Page property if your code is in a user control. It'll work even if you're on a page:

if (Page.User.IsInRole("Admin")) {
  Label1.Text = "Admin";
}


To answer your original question though, setup your grid view like this:

<asp:GridView ID="GridView2" runat="server" DataSourceID="XmlDataSource1" OnPreRender="GridView2_PreRender">
  <Columns>
    <asp:CommandField ShowEditButton="True" />
  </Columns>
</asp:GridView>

Notice the OnPreRender event setup. Next, either in code-behind or in a server-side script tag, add the attached code to show/hide that command column entirely, based on the user's role just before the grid renders.
protected void GridView2_PreRender(object sender, EventArgs e) {
  if (!Page.User.IsInRole("Admin")) {
    // Get the first column by index.
    // * If you change the position of the CommandField, use the appropriate index here.
    int ColIndex = 0;
    GridView2.Columns[ColIndex].Visible = false;
  }
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now