Can someone help me with this code, at the line concerning the condition if I'm and Administrator it returns false
despite logged in as Administrator.
The C# Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Principal;
namespace WinFormsAppDummies
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Added code from the book C# 5.0 Dummies
WindowsIdentity myIdentity = WindowsIdentity.GetCurrent();
WindowsPrincipal myPrincipal = new WindowsPrincipal(myIdentity);
if (myPrincipal.IsInRole("Accounting"))
{
AccountingButton.Visible = true;
}
else if(myPrincipal.IsInRole("Sales"))
{
SalesButton.Visible = true;
}
else if (myPrincipal.IsInRole("Management"))
{
ManagerButton.Visible = true;
}
else if (myPrincipal.IsInRole(WindowsBuiltInRole.Administrator)) // <--- RETURNS FALSE!
{
ManagerButton.Visible = true;
}
}
private void SalesButton_Click(object sender, EventArgs e)
{
}
private void AccountingButton_Click(object sender, EventArgs e)
{
}
private void ManagerButton_Click(object sender, EventArgs e)
{
}
}
}
Can someone help me adjusting the previous code so that the Rule is seen as Administrator?