Re: Implement authorization in win forms

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



For info, here is a rough sketch of what the component would look
like... this allows both IDE and programmatic usage; note that for
roles-based security you'd also need to initialize the principal - at
the most primative this can be as simple as:

Thread.CurrentPrincipal = new GenericPrincipal(
new GenericIdentity("Marc"), // name of user
new string[] { "BASIC" } // array of roles that the
user has
);

Obviously if your security model is more complex, you may need to
change things ;-p

[ProvideProperty("Role", typeof(Control))]
[ToolboxItemFilter("System.Windows.Forms")]
[Description("Provides automatic role-checking")]
public class RoleDisabler : Component, IExtenderProvider
{
private Dictionary<Control, string> map
= new Dictionary<Control, string>();
[DefaultValue("")]
public string GetRole(Control control)
{
if (control == null) return "";
string role;
map.TryGetValue(control, out role);
return role ?? "";
}
public void SetRole(Control control, string role)
{
if (control == null) return;
bool add = false, remove = false;
if (string.IsNullOrEmpty(role))
{
remove = map.Remove(control);
}
else
{
add = !map.ContainsKey(control);
map[control] = role;
}
if (!DesignMode)
{
SetEnabled(control);
if (add)
{
control.ParentChanged += control_ParentChanged;

}
else if (remove)
{
control.ParentChanged -= control_ParentChanged;
}
}
}
private void SetEnabled(Control control)
{
if (DesignMode || control == null) return;
string role;
if (map.TryGetValue(control, out role))
{
IPrincipal principal = Thread.CurrentPrincipal;
control.Enabled = principal == null ? false :
principal.IsInRole(role);
}
}
void control_ParentChanged(object sender, EventArgs e)
{
SetEnabled(sender as Control);
}
bool IExtenderProvider.CanExtend(object obj)
{
return obj is Control;
}
}
.



Relevant Pages

  • Re: Implement authorization in win forms
    ... roles-based security you'd also need to initialize the principal - at ... public void SetRole(Control control, string role) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Creating ToolStripMenu based on user rights
    ... public void SetRole(Control control, string role) ... bool IExtenderProvider.CanExtend(object obj) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Implement authorization in win forms
    ... public void SetRole(Control control, string role) ... bool IExtenderProvider.CanExtend(object obj) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Implement authorization in win forms
    ... public void SetRole(Control control, string role) ... bool IExtenderProvider.CanExtend(object obj) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Asp.Net AJAX dynamically added Tab Control not displaying
    ... TabPanel assumes there's no active tab and it hides all the tabs at client ... protected void Page_PreRender ... Microsoft is providing this information as a convenience to you. ... does not control these sites and has not tested any software or information ...
    (microsoft.public.dotnet.framework.aspnet)