练习1:应用程序安全性
通过该练习将在一个已经存在的应用程序中添加认证和基于角色的授权。
第一步
打BugSmak.sln项目,默认的安装路径应该为C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Security\exercises\ex01\begin,并编译。
第二步 在应用程序中添加认证
1
.选择Debug | Start Without Debugging菜单命令运行应用程序。应用程序当前没有可以使用的认证用户。
2
.关闭应用程序。
3
.在解决方案管理器中选择Security \ SecurityHelper.cs文件,选择View | Code菜单命令,添加如下的命名空间。
using
System.Web.Security;
4 .在方法Authenticate中添加如下代码。
public static bool Authenticate( string username, string password)
{
bool authenticated = false;
// TODO: Authenticate Credentials
authenticated = Membership.ValidateUser(username, password);
// TODO: Get Roles
return authenticated;
}
4 .在方法Authenticate中添加如下代码。
public static bool Authenticate( string username, string password)
{
bool authenticated = false;
// TODO: Authenticate Credentials
authenticated = Membership.ValidateUser(username, password);
// TODO: Get Roles
return authenticated;
}
方法
Authenticate将会被窗体
LoginForm调用来验证用户身份,
Membership.ValidateUser方法实现了用户的验证。
Membership系统使用了
provider模型,所以应用程序不用去实现数据的存储,
ASP.NET ships提供了两种
Membership Provider,一是使用
Microsoft SQL Server作为数据源,另一个是使用
Windows Active Directory。也可以创建自己的
Membership Provider,我们已经实现了读取从
XML文件中读取
application members。
5
.在解决方案管理器中选择Security | Providers | ReadOnlyXmlMembershipProvider.cs,并选择View | Code菜单命令回顾一下代码。
ReadOnlyXmlMembershipProvider
(继承于MembershipProvider)是一个自定义Provider的示例,它实现从一个未加密的XML文件中读取,这并不是一个好的实践,但是在该练习中却非常的有用。
6
.打开App.config文件,查看membership provider的配置,认证数据的存储定义在一个Users.xml的文件中。
<?
xml version="1.0" encoding="utf-8"
?>
< configuration >
< system .web >
< membership defaultProvider ="ReadOnlyXmlMembershipProvider" >
< providers >
< add name ="ReadOnlyXmlMembershipProvider"
type ="BugSmak.Security.Providers.ReadOnlyXmlMembershipProvider, BugSmak"
description ="Read-only XML membership provider"
xmlFileName ="Users.xml" />
</ providers >
</ membership >
< roleManager enabled ="true"
defaultProvider ="ReadOnlyXmlRoleProvider" >
< providers >
< add name ="ReadOnlyXmlRoleProvider"
type ="BugSmak.Security.Providers.ReadOnlyXmlRoleProvider, BugSmak"
description ="Read-only XML role provider"
xmlFileName ="Users.xml" />
</ providers >
</ roleManager >
</ system.web >
</ configuration >
< configuration >
< system .web >
< membership defaultProvider ="ReadOnlyXmlMembershipProvider" >
< providers >
< add name ="ReadOnlyXmlMembershipProvider"
type ="BugSmak.Security.Providers.ReadOnlyXmlMembershipProvider, BugSmak"
description ="Read-only XML membership provider"
xmlFileName ="Users.xml" />
</ providers >
</ membership >
< roleManager enabled ="true"
defaultProvider ="ReadOnlyXmlRoleProvider" >
< providers >
< add name ="ReadOnlyXmlRoleProvider"
type ="BugSmak.Security.Providers.ReadOnlyXmlRoleProvider, BugSmak"
description ="Read-only XML role provider"
xmlFileName ="Users.xml" />
</ providers >
</ roleManager >
</ system.web >
</ configuration >
如果有一个自定义的
Provider,必须对应用程序进行配置。
7
.打开Users.xml文件,可以看到定义了如下用户。
Username
|
Password
|
Role(s)
|
Tom
|
P@ssw0rd
|
Employee
|
Dick
|
P@ssw0rd
|
Developer
|
Harry
|
P@ssw0rd
|
Manager
|
其中代码:
<?
xml version="1.0" encoding="utf-8"
?>
< Users >
< User >
< UserName > Tom </ UserName >
< Password > P@ssw0rd </ Password >
< EMail > [email]tom@contoso.com[/email] </ EMail >
< Roles > Employee </ Roles >
</ User >
< User >
< UserName > Dick </ UserName >
< Password > P@ssw0rd </ Password >
< EMail > [email]dick@contoso.com[/email] </ EMail >
< Roles > Developer </ Roles >
</ User >
< User >
< UserName > Harry </ UserName >
< Password > P@ssw0rd </ Password >
< EMail > [email]harry@contoso.com[/email] </ EMail >
< Roles > Manager </ Roles >
</ User >
</ Users >
< Users >
< User >
< UserName > Tom </ UserName >
< Password > P@ssw0rd </ Password >
< EMail > [email]tom@contoso.com[/email] </ EMail >
< Roles > Employee </ Roles >
</ User >
< User >
< UserName > Dick </ UserName >
< Password > P@ssw0rd </ Password >
< EMail > [email]dick@contoso.com[/email] </ EMail >
< Roles > Developer </ Roles >
</ User >
< User >
< UserName > Harry </ UserName >
< Password > P@ssw0rd </ Password >
< EMail > [email]harry@contoso.com[/email] </ EMail >
< Roles > Manager </ Roles >
</ User >
</ Users >
8
.选择Debug | Start Without Debugging运行应用程序,用Tom, Dick或者Harry正确的用户登录设置Membership Provider。选择File | Sign Out退出,以一个错误的用户名和密码登录。
9
.以用户名Tom登录,选择Tasks | Raise New Bug菜单命令,将会给出一个提示信息“Sorry, you aren't allowed to access that form”。类似的尝试一下Assign Bug和Resolve Bug。
10
.关闭应用程序。
第三步 添加基于角色的授权
1
.在解决方案管理器中选择TaskForms \ RaiseBug.cs文件,选择View | Code菜单命令,RaiseBug窗体不管用户是Employee,Developer还是Manager都允许访问,如果用户尝试访问没有权限的窗体,将会抛出一个SecurityException异常。角色与窗体之间的对应权限如下:
TaskForm
|
Role Required
|
RaiseBug
|
Employee, Developer, or Manager
|
AssignBug
|
Manager
|
ResolveBug
|
Developer or Manager
|
2
.在解决方案管理中选择Security \ SecurityHelper.cs,单击View | Code菜单命令,
修改方法Authenticate的代码如下。
public
static
bool
Authenticate(
string
username,
string
password)
{
bool authenticated = false;
// TODO: Authenticate Credentials
authenticated = Membership.ValidateUser(username, password);
// TODO: Get Roles
if (!authenticated)
return false;
IIdentity identity;
identity = new GenericIdentity(username, Membership.Provider.Name);
string[] roles = Roles.GetRolesForUser(identity.Name);
IPrincipal principal = new GenericPrincipal(identity, roles);
// Place user's principal on the thread
Thread.CurrentPrincipal = principal;
return authenticated;
}
{
bool authenticated = false;
// TODO: Authenticate Credentials
authenticated = Membership.ValidateUser(username, password);
// TODO: Get Roles
if (!authenticated)
return false;
IIdentity identity;
identity = new GenericIdentity(username, Membership.Provider.Name);
string[] roles = Roles.GetRolesForUser(identity.Name);
IPrincipal principal = new GenericPrincipal(identity, roles);
// Place user's principal on the thread
Thread.CurrentPrincipal = principal;
return authenticated;
}
3
.打开App.config文件,查看角色的配置,数据存储在一个Users.xml的文件中。
<?
xml version="1.0" encoding="utf-8"
?>
< configuration >
< system .web >
< membership defaultProvider ="ReadOnlyXmlMembershipProvider" >
< providers >
< add name ="ReadOnlyXmlMembershipProvider"
type ="BugSmak.Security.Providers.ReadOnlyXmlMembershipProvider, BugSmak"
description ="Read-only XML membership provider"
xmlFileName ="Users.xml" />
</ providers >
</ membership >
< roleManager enabled ="true"
defaultProvider ="ReadOnlyXmlRoleProvider" >
< providers >
< add name ="ReadOnlyXmlRoleProvider"
type ="BugSmak.Security.Providers.ReadOnlyXmlRoleProvider, BugSmak"
description ="Read-only XML role provider"
xmlFileName ="Users.xml" />
</ providers >
</ roleManager >
</ system.web >
</ configuration >
< configuration >
< system .web >
< membership defaultProvider ="ReadOnlyXmlMembershipProvider" >
< providers >
< add name ="ReadOnlyXmlMembershipProvider"
type ="BugSmak.Security.Providers.ReadOnlyXmlMembershipProvider, BugSmak"
description ="Read-only XML membership provider"
xmlFileName ="Users.xml" />
</ providers >
</ membership >
< roleManager enabled ="true"
defaultProvider ="ReadOnlyXmlRoleProvider" >
< providers >
< add name ="ReadOnlyXmlRoleProvider"
type ="BugSmak.Security.Providers.ReadOnlyXmlRoleProvider, BugSmak"
description ="Read-only XML role provider"
xmlFileName ="Users.xml" />
</ providers >
</ roleManager >
</ system.web >
</ configuration >
4
.选择Debug | Start Without Debugging运行应用程序,分别用Tom, Dick 和 Harry用户登录,他们的访问权限如下:
User
|
Task Access
|
Tom (Employee)
|
Raise New Bug
|
Dick (Developer)
|
Raise New Bug
Resolve Bug
|
Harry (Manager)
|
Raise New Bug
Resolve Bug
Assign Bug
|
5
.关闭应用程序。
更多Enterprise Library的文章请参考《Enterprise Library系列文章》
本文转自lihuijun51CTO博客,原文链接:
http://blog.51cto.com/terrylee/67643
,如需转载请自行联系原作者