Authorization Mechanisms(认证机制)

The first method is to use the <authorization> section of the Web.config file to control access to files
and folders:

<authorization>
<allow roles=”Members”/>
<deny users=”?”/>
</authorization>

 This configuration only allows the users in the Members role access the files and subfolders of the folder
where the Web.config file is located. The UrlAuthorizationModule automatically uses the contents of
the <authorization> section to authorize the user without any work on the part of page developers. Page
developers are only responsible for using the <allow> and <deny> elements to define the access rules.

The second method is to use the PrinciplePermissionAttribute to control access to classes and
methods:

[System.Security.Permissions.PrincipalPermission(System.Security.Permissions.
SecurityAction.Demand,Role
=”Members”)]
public static bool MyMethod()
{
...
}

The PrinciplePermission demand shown here will only allow members of the Members role to call
the MyMethod method. This means that the PrinciplePermissionAttribute attribute allows you to
create more fine-grained access rules than the <authorization> tag in the Web.config file. The
<authorization> tags can be used to control access to a file, not the classes and methods in the file.


The third method is to use the PrinciplePermission class programmatically to control access to a
block of code:

public static bool MyMethod()
{
System.Security.Permissions.PrincipalPermission perm 
=
new System.Security.Permissions.PrincipalPermission(null, “Members”);
perm.Demand();
...
}

Only members of the Members role will be allowed to run the code that comes after the call to the
Demand method.
The fourth method, using the IPrincipal.IsInRole method, allows you to set up even more finegrained
access rules:

public static bool MyMethod()
{
...
if (HttpContext.Current.User.IsInRole(“Members”))
{
//some code
}

}

Only the members of the Members role will be allowed to run the code inside the if block.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值