以win形式实施授权 [英] Implement authorization in win forms
本文介绍了以win形式实施授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在胜利形式中实施授权的最佳方式
申请。
我的意思是显示/隐藏或启用/禁用保存按钮,创建
上下文菜单等。
Whats the best way of implementing authorization in a win forms
application.
I mean things like show/hide or enable/disable Save button ,creating
context menus etc.
推荐答案
如果您遇到困难,请告诉我。这是
System.ComponentModel的一部分,这是我非常了解的一个领域。虽然我理解他们,但我以前没有亲自*创建*扩展属性
,但我想如果*你不是太棘手*了解
该区域其余部分的工作原理。实际上,我很乐意以此为借口
在该地区进行更多的挖掘... ;-p
Marc
If you get stuck, please let me know. This is part of
System.ComponentModel, an area that I know very well. Although I
understand them, I haven''t personally *created* an extension property
before, but I imagine that it isn''t too tricky *if* you understand how
the rest of that area works. Actually, I''d happily use it as an excuse
to do some more digging in that area... ;-p
Marc
4月11日凌晨4:25,Marc Gravell< marc.grav ... @ gmail.comwrote:
On Apr 11, 4:25 am, Marc Gravell <marc.grav...@gmail.comwrote:
有关信息,这里是组件外观的粗略草图
...这允许IDE和程序化使用;请注意,对于
基于角色的安全性,您还需要初始化主体 - 在
,最具弹性的可以简单如下:
Thread.CurrentPrincipal = new GenericPrincipal(
new GenericIdentity(" Marc"),//用户名
new string [] {" ; BASIC"} //
用户拥有的角色数组
);
显然,如果你的安全模型更多很复杂,你可能需要改变东西; $ p $>
[ProvideProperty(" Role",typeof(Control))]
[ToolboxItemFilter(" System.Windows.Forms")]
[描述("提供自动角色检查))
公共类RoleDisabler:Component ,IExtenderProvider
{
private Dictionary< Control,stringmap
= new Dictionary< Control,string>();
[DefaultValue("")]
公共字符串GetRole(控制控件)
{
if(control == null)return"";
string role;
map.TryGetValue(control,out role);
返回角色? ";
}
public void SetRole(控制控件,字符串角色)
{
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;
}
否则如果(删除)
{
control.ParentChanged - = control_ParentChanged;
}
}
}
private void SetEnabled(控制控件)
{
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(发件人为控件);
}
bool IExtenderProvider.CanExtend(object obj )
{
返回obj是对照;
}
}
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, stringmap
= 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;
}
}
谢谢..
我将如何使用RoleDisabler?我会从工具栏中拖出它吗
喜欢工具提示吗?
Thanks..
How will i use the RoleDisabler ? would i drag it from the toolbar
like tooltip?
4月11日下午1点49分,parez< psaw ... @ gmail.comwrote:
On Apr 11, 1:49 pm, parez <psaw...@gmail.comwrote:
4月11日凌晨4:25,Marc Gravell< marc.grav ... @ gmail.comwrote:
On Apr 11, 4:25 am, Marc Gravell <marc.grav...@gmail.comwrote:
有关信息,这里是组件外观的粗略草图
like ...这允许IDE和程序化使用;请注意,对于
基于角色的安全性,您还需要初始化主体 - 在
最具弹性,这可以简单到:
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"),//用户名
new string [] {" BASIC"} //
用户拥有的角色数组
);
Thread.CurrentPrincipal = new GenericPrincipal(
new GenericIdentity("Marc"), // name of user
new string[] { "BASIC" } // array of roles that the
user has
);
显然,如果您的安全模型更复杂,您可能需要更改b / b
; -p
Obviously if your security model is more complex, you may need to
change things ;-p
[ProvideProperty(" Role",typeof(Control))]
[ToolboxItemFilter(" System.Windows.Forms") ]
[描述(提供自动角色检查)]
公共类RoleDisabler:Component,IExtenderProvider
{
private Dictionary< Control,stringmap
= new Dictionary< Control,string>();
[DefaultValue("")]
public string GetRole(Control control)
{
if(control == null)return"" ;;
string角色;
map.TryGetValue(控制,退出角色);
返回角色? ";
}
public void SetRole(控制控件,字符串角色)
{
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;
[ProvideProperty("Role", typeof(Control))]
[ToolboxItemFilter("System.Windows.Forms")]
[Description("Provides automatic role-checking")]
public class RoleDisabler : Component, IExtenderProvider
{
private Dictionary<Control, stringmap
= 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(删除)
{
control.ParentChanged - = control_ParentChanged;
}
}
}
private void SetEnabled(控制控件)
{
如果(DesignMode || control == null)返回;
字符串角色;
if(b) map.TryGetValue(控制,退出角色))
{
IPrincipal principal = Thread.CurrentPrincipal;
control.Enabled = principal == null ? false:
principal.IsInRole(role);
}
}
void control_ParentChanged(object sender,EventArgs e)
{
SetEnabled(发件人为控件);
}
bool IExtenderProvider.CanExtend(object obj )
{
返回obj是控制;
}
}
}
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;
}
}
谢谢..
我将如何使用RoleDisabler?我会从工具栏中拖动它吗?/ b $ b喜欢工具提示吗?
Thanks..
How will i use the RoleDisabler ? would i drag it from the toolbar
like tooltip?
太棒了..谢谢......
我有一个问题..
什么时候会SetEnabled函数执行?
That was great.. thanks...
I have a question..
When will the SetEnabled function execute?
这篇关于以win形式实施授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文