安全探索——.NET 中的角色安全(3)
2009-02-22 20:45 G yc {Son of VB.NET} 阅读(560) 评论(0) 编辑 收藏 举报(写在之前, 恩。。这回的发布有点慢了,主要也是最近 风波四起啊~~~)
或许大家应该主要到,在使用VS2008 的时候,打开项目属性后,会看到一个“服务”标签。这个东西是所作什么的?
答案就是,使用 ASP.NET 的Membership 功能 + 网站 数据库 来认证 客户端程序(即Windows程序和WPF等)。
而证人通过后, 获得 Identity 和Principal对象就是 ClientFormsIdentity 和 ClientRolePrincipal。
ClientFormsIdentity 和 ClientRolePrincipal
ClientFormsIdentity和ClientRolePrincipal 是.NET 3.5 中新增的成员,在system.web.clientservices 命名空间下。
关于 ClientFormsIdentity ,这里说一点。其AuthenticationCookies属性, 保存了认证的Cookies,在使用WebServices时,可以附加此Cookies,即可通过认证。至于WCF,我还没有看到怎么加入Cookies。唉~
客户端应用程序服务
客户端应用程序服务使您可以轻松创建基于 Windows 的应用程序,这些应用程序使用 Microsoft ASP.NET 2.0 AJAX Extensions 中包含的 ASP.NET AJAX 登录、角色和配置应用程序服务。这些服务可让多个基于 Web 和 Windows 的应用程序共享来自单个服务器的用户信息和用户管理功能。例如,可以使用这些服务器执行下列任务:
- 对用户进行身份验证。可以使用身份验证服务来验证用户的身份。
- 确定已验证身份的用户的角色。可以使用角色服务来根据用户的角色更改应用程序的用户界面。例如,可以为属于管理员角色的用户提供附加功能。
- 存储和访问位于服务器上的每个用户的应用程序设置。可以使用 Web 设置服务(也称为配置文件服务)跨多个应用程序和位置共享设置。
客户端应用程序服务通过客户端服务提供程序利用 Web 服务扩展性模型,可以在应用程序配置文件中指定这些客户端服务提供程序。这些服务提供程序包括脱机功能,当网络连接不可用时,脱机功能对身份验证、角色和设置数据使用本地缓存。
下面是程序的制作过程,另外,本程序基于上篇文章的网站而做。
1、首先,先修改网站的Web.config文件。
a.修改 <profile> 节点,增加Profile内容
<providers>
<clear/>
<add name="AspNetSqlProfileProvider"
connectionStringName="LocalSqlServer"
applicationName="DemoIdentity"
type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
<properties>
<add name="MyString" type="string"/>
</properties>
</profile>
b.开启AJAX服务扩展
<scripting>
<webServices>
<authenticationService enabled="true" />
<roleService enabled="true"/>
<profileService enabled="true" readAccessProperties ="MyString" writeAccessProperties ="MyString" />
</webServices>
</scripting>
</system.web.extensions>
<system.serviceModel>
<services>
<service name="System.Web.ApplicationServices.AuthenticationService" behaviorConfiguration="AuthenticationServiceTypeBehaviors">
<endpoint contract="System.Web.ApplicationServices.AuthenticationService"
binding="basicHttpBinding"
bindingConfiguration="userHttps"
bindingNamespace="http://asp.net/ApplicationServices/v200"/>
</service>
<service name="System.Web.ApplicationServices.RoleService"
behaviorConfiguration="ApplicationServiceTypeBehaviors">
<endpoint contract="System.Web.ApplicationServices.RoleService"
binding="basicHttpBinding"
bindingConfiguration="userHttps"
bindingNamespace="http://asp.net/ApplicationServices/v200"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="userHttps">
<!--<security mode="Transport" />-->
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ApplicationServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
<behavior name="AuthenticationServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
2、打开网站的属性,将网站启动位置设置成特定端口,例如 55555
现在网站这边完成了, 接下来是客户端。
1、首先,新建一个项目,这里选的是WPF程序,名字是WPFApplicationServices。
2、打开项目属性,转到服务标签。
a.选择 “启动客户端应用程序服务”
b.选择“使用 Forms 身份认证”
c.分别在 “身份验证服务器位置”、“角色服务器位置”、“Web 设置服务器位置” 上填入服务器地址:http://localhost:55555
d.填写 “凭据提供程序 ” : WPFApplicationServices.Login, WPFApplicationServices
3、转到 “设置” 选项卡,单击“加载Web设置”
到这里,基本配置的地方就算完成了。
接下来实现凭据提供程序
1、添加一个窗体, 名为Login。
2、更改界面XAML如下:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Login" Height="300" Width="500" WindowStartupLocation="CenterOwner" WindowStyle="SingleBorderWindow" ResizeMode="NoResize" ShowInTaskbar="False">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200*" />
<ColumnDefinition Width="300*" />
</Grid.ColumnDefinitions>
<Canvas Name="Canvas">
<Ellipse Canvas.Left="85" Canvas.Top="187" Height="23" Name="Ellipse1" Stroke="AliceBlue" Width="25" Fill="AliceBlue" />
<Rectangle Canvas.Left="85" Canvas.Top="46" Height="135" Name="Rectangle1" Stroke="Black" Width="25" Fill="AliceBlue" RadiusX="10" RadiusY="10" />
</Canvas>
<StackPanel Grid.Column="1">
<Rectangle Height="80"></Rectangle>
<TextBlock>UserName:</TextBlock>
<TextBox x:Name="txtUserName" ></TextBox>
<TextBlock>Password:</TextBlock>
<PasswordBox x:Name="psdPassword" ></PasswordBox>
<CheckBox x:Name="chkrememberMe" Content="Remember me"></CheckBox>
<Rectangle Height="15">
</Rectangle>
<Grid Height="30">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150*" />
<ColumnDefinition Width="150*" />
</Grid.ColumnDefinitions>
<Button x:Name="btnLogin" Content="Login" Grid.Column="0" IsDefault="True"></Button>
<Button x:Name="btnCancel" Content="Cancel" Grid.Column="1" IsCancel="True"></Button>
</Grid>
</StackPanel>
</Grid>
</Window>
3、修改代码文件,实现IClientFormsAuthenticationCredentialsProvider接口
Partial Public Class Login
Implements IClientFormsAuthenticationCredentialsProvider
Public Function GetCredentials() As ClientFormsAuthenticationCredentials Implements _
IClientFormsAuthenticationCredentialsProvider.GetCredentials
If Me.ShowDialog Then
Return New ClientFormsAuthenticationCredentials( _
txtUserName.Text, Me.psdPassword.Password, _
chkrememberMe.IsChecked)
Else
Return Nothing
End If
End Function
Private Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnLogin.Click
Me.DialogResult = True
End Sub
Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
' Me.DialogResult = False
End Sub
End Class
现在登录窗口也制作好了。
接下来,只要在 程序中调用 Membership.ValidateUser(String.Empty, String.Empty) 方法,既可显示登录窗口。
至于注销,需要先将 System.Web.Security.Membership.Provider(当前的Provider) 转换成 ClientFormsAuthenticationMembershipProvider, 然后在调用其Logout() 方法。
有关 客户端应用程序服务 可以参考这里 客户端应用程序服务
另外还有官方的使用实例教程,是Windows客户端的 演练:使用客户端应用程序服务
关于客户端应用服务的本质, 就是通过重写了Membership Provider来调用网站的 AJAX扩展服务。 这些Provider 在System.Web.ClientServices.Providers 命名空间下。