练习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菜单命令,添加如下的命名空间。

Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)using System.Web.Security;
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
4.在方法Authenticate中添加如下代码。
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
public static bool Authenticate(string username, string password)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
}

方法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的文件中。

Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)<?xml version="1.0" encoding="utf-8"?>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
<configuration>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)  
<system.web>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<membership defaultProvider="ReadOnlyXmlMembershipProvider">
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)      
<providers>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)        
<add name="ReadOnlyXmlMembershipProvider"
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)             type
="BugSmak.Security.Providers.ReadOnlyXmlMembershipProvider, BugSmak"
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)             description
="Read-only XML membership provider"
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)             xmlFileName
="Users.xml" />
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)      
</providers>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
</membership>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<roleManager enabled="true"
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)                 defaultProvider
="ReadOnlyXmlRoleProvider">
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)      
<providers>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)        
<add name="ReadOnlyXmlRoleProvider"
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)             type
="BugSmak.Security.Providers.ReadOnlyXmlRoleProvider, BugSmak"
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)             description
="Read-only XML role provider"
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)             xmlFileName
="Users.xml" />
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)      
</providers>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
</roleManager>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)  
</system.web>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
</configuration>

如果有一个自定义的Provider,必须对应用程序进行配置。

7.打开Users.xml文件,可以看到定义了如下用户。

Username

Password

Role(s)

Tom

P@ssw0rd

Employee

Dick

P@ssw0rd

Developer

Harry

P@ssw0rd

Manager

其中代码:

Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)<?xml version="1.0" encoding="utf-8" ?>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
<Users>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)  
<User>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<UserName>Tom</UserName>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<Password>P@ssw0rd</Password>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<EMail>tom@contoso.com</EMail>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<Roles>Employee</Roles>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)  
</User>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)  
<User>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<UserName>Dick</UserName>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<Password>P@ssw0rd</Password>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<EMail>dick@contoso.com</EMail>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<Roles>Developer</Roles>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)  
</User>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)  
<User>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<UserName>Harry</UserName>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<Password>P@ssw0rd</Password>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<EMail>harry@contoso.com</EMail>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<Roles>Manager</Roles>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)  
</User>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
</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 BugResolve Bug

10.关闭应用程序。

 

第三步 添加基于角色的授权

1.在解决方案管理器中选择TaskForms \ RaiseBug.cs文件,选择View | Code菜单命令,RaiseBug窗体不管用户是EmployeeDeveloper还是Manager都允许访问,如果用户尝试访问没有权限的窗体,将会抛出一个SecurityException异常。角色与窗体之间的对应权限如下:

TaskForm

Role Required

RaiseBug

Employee, Developer, or Manager

AssignBug

Manager

ResolveBug

Developer or Manager

2.在解决方案管理中选择Security \ SecurityHelper.cs,单击View | Code菜单命令,

修改方法Authenticate的代码如下。

Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)public static bool Authenticate(string username, string password)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)

3.打开App.config文件,查看角色的配置,数据存储在一个Users.xml的文件中。

Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)<?xml version="1.0" encoding="utf-8"?>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
<configuration>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)  
<system.web>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<membership defaultProvider="ReadOnlyXmlMembershipProvider">
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)      
<providers>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)        
<add name="ReadOnlyXmlMembershipProvider"
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)             type
="BugSmak.Security.Providers.ReadOnlyXmlMembershipProvider, BugSmak"
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)             description
="Read-only XML membership provider"
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)             xmlFileName
="Users.xml" />
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)      
</providers>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
</membership>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
<roleManager enabled="true"
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)                 defaultProvider
="ReadOnlyXmlRoleProvider">
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)      
<providers>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)        
<add name="ReadOnlyXmlRoleProvider"
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)             type
="BugSmak.Security.Providers.ReadOnlyXmlRoleProvider, BugSmak"
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)             description
="Read-only XML role provider"
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)             xmlFileName
="Users.xml" />
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)      
</providers>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)    
</roleManager>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)  
</system.web>
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
</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系列文章

相关文章:

  • 2021-10-05
  • 2021-07-03
  • 2021-10-19
  • 2022-02-05
  • 2022-01-18
  • 2021-11-03
猜你喜欢
  • 2022-01-06
  • 2021-06-03
  • 2021-08-07
  • 2021-10-09
  • 2022-02-10
  • 2021-12-03
相关资源
相似解决方案