惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
T
Threatpost
G
Google Developers Blog
T
Threat Research - Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Exploit Database - CXSecurity.com
H
Heimdal Security Blog
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
Hacker News: Ask HN
Hacker News: Ask HN
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Schneier on Security
B
Blog
V2EX - 技术
V2EX - 技术
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
W
WeLiveSecurity
C
Cybersecurity and Infrastructure Security Agency CISA
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Y
Y Combinator Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Spread Privacy
Spread Privacy
The Last Watchdog
The Last Watchdog
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
Schneier on Security
Schneier on Security
F
Fortinet All Blogs
N
News | PayPal Newsroom
Attack and Defense Labs
Attack and Defense Labs
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
S
Security @ Cisco Blogs
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
Project Zero
Project Zero
I
Intezer
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
SecWiki News
SecWiki News
Martin Fowler
Martin Fowler

博客园 - G yc {Son of VB.NET}

解决安装Visual Studio 2010 SP1 Tools for SQL Server Compact 4.0 错误 25541 解决“计划任务不存在的问题”方法 开启 Win 2008 R2 可靠性历史记录 解决 Hyper-V R2 虚拟网卡影响网速变慢问题 Win7/R2 原生VHD 启动 沈阳 .NET 俱乐部 10月25日 Windows 7社区发布活动 俱乐部活动讲师 & 场地招募中。。 WCF 部署问题 小总结 (HTTP 不能注册的解决方法 和 部署Bat脚本) Win7 支付宝证书 安装记 (翻译)《介绍 GENEVA Beta 1 白皮书》(3) (翻译)《介绍 GENEVA Beta 1 白皮书》(2) (翻译)《介绍 GENEVA Beta 1 白皮书》(1) 翻译预告 《介绍 GENEVA Beta 1 白皮书》 安全探索——.NET 3.0 中的标识模型(1) 安全探索——.NET 中的角色安全(4) 安全探索——.NET 中的角色安全(2) 安全探索——.NET 中的角色安全(1) 发布 事例 Comics Service [转载] 让VS2010 CTP 运行在Hyper-V 上
安全探索——.NET 中的角色安全(3)
G yc {Son of VB.NET} · 2009-02-22 · via 博客园 - G yc {Son of VB.NET}

(写在之前, 恩。。这回的发布有点慢了,主要也是最近 风波四起啊~~~)

或许大家应该主要到,在使用VS2008 的时候,打开项目属性后,会看到一个“服务”标签。这个东西是所作什么的?

答案就是,使用 ASP.NET 的Membership 功能 + 网站 数据库 来认证 客户端程序(即Windows程序和WPF等)。

而证人通过后, 获得 Identity 和Principal对象就是 ClientFormsIdentity 和 ClientRolePrincipal。

ClientFormsIdentity 和 ClientRolePrincipal

ClientFormsIdentityClientRolePrincipal 是.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内容     

<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服务扩展  

<system.web.extensions>
    
<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

clip_image001

现在网站这边完成了, 接下来是客户端。

1、首先,新建一个项目,这里选的是WPF程序,名字是WPFApplicationServices。

2、打开项目属性,转到服务标签。

    a.选择 “启动客户端应用程序服务”

    b.选择“使用 Forms 身份认证”

    c.分别在 “身份验证服务器位置”、“角色服务器位置”、“Web 设置服务器位置” 上填入服务器地址:http://localhost:55555

    d.填写 “凭据提供程序 ” : WPFApplicationServices.Login, WPFApplicationServices

clip_image002

3、转到 “设置” 选项卡,单击“加载Web设置”

到这里,基本配置的地方就算完成了。

接下来实现凭据提供程序

1、添加一个窗体, 名为Login。

2、更改界面XAML如下:

        <Window x:Class="Login"
            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接口

        Imports System.Web.ClientServices.Providers
        
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 ObjectByVal e As System.Windows.RoutedEventArgs) Handles btnLogin.Click
                
Me.DialogResult = True
            
End Sub
        
            
Private Sub btnCancel_Click(ByVal sender As ObjectByVal 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() 方法。

clip_image006clip_image005

clip_image003clip_image004

有关 客户端应用程序服务 可以参考这里 客户端应用程序服务

另外还有官方的使用实例教程,是Windows客户端的 演练:使用客户端应用程序服务

关于客户端应用服务的本质, 就是通过重写了Membership Provider来调用网站的 AJAX扩展服务。 这些Provider 在System.Web.ClientServices.Providers 命名空间下。