[WCF权限控制]从两个重要的概念谈起:Identity与Principal[上篇]
2011-06-29 22:56
881 查看
在安全领域,认证和授权是两个重要的主题。认证是安全体系的第一道屏障,守护着整个应用或者服务的第一道大门。当访问者叩门请求进入的时候,认证体系通过验证对方提供凭证确定其真实身份。作为看门人的认证体系,只有在证实了访问者的真实身份的情况下才会为其打开城门,否则将之举之门外。
当访问者入门之后,并不意味着它可以为所欲为。为了让适合的人干适合的事,就需要授权机制为具体的人设置具体的权限,并根据这些权限设置决定试图调用的操作或者访问的资源对该访问者是否是安全的。对于一个安全保障体系来说,授权是目的。但是授权的执行是假定已经通过认证体现确定了访问者真实身份,因为用于进行授权采用的权限集是基于真个确定的身份的。在真正进入对WCF授权的具体介绍之前,我们有必要来了解一下这个“身份”的问题。
Name:身份所代表的用户的名称;
IsAuthenticated:身份所代表的用户是否经过认证;
AuthenticationType:身份认证所采用的类型。
[/code]
通过IIdentity表示的身份是基于某种认证类型的,不同类型的认证往往对应于不同的身份类型。以ASP.NET认证为例,如果我们采用Forms认证,那么认证后的身份通过一个FormsIdentity对象表示。而Windows Live Passport认证对应的具体身份类型则是PassportIdentity。在这里我们着重介绍一下如下三种身份类型:WindowsIdentity、GenericIdentity和X509Identity。
[/code]
对于用于表示认证类型的AuthenticationType属性来说,在工作组模式下返回NTLM。对于域模式,如果操作系统是Vista或者以后的版本,该属性返回Negotiate,表示采用SPNEGO认证协议。而对于之前的Windows版本,则该属性值为Kerberos。
Groups属性返回WindowsIdentity对应的Windows帐号所在的用户组(User Group),而IsGuest则用于判断Windows帐号是否存在于Guest用户组中。IsSystem属性则表示Windows帐号是否是一个系统帐号。
如果你对ASP.NET的安全有一定的了解,应该知道我们可以对IIS进行相应的配置是ASP.NET应用支持匿名用户。也就是说,用户无需提供具体的用户凭证,而是以匿名的方式登录到ASP.NET站点中。对于匿名登录,IIS实际上会采用一个预先指定的Windows帐号进行登录。而在这里,IsAnonymous属性就表示该WindowsIdentity对应的Windows帐号是否是匿名帐号。
对于匿名身份的问题,在这里还有一点值得补充一下。WindowsIdentity定义了如下一个静态的GetAnonymous方法用于返回一个表示匿名身份的WindowsIdentity对象。但是这仅仅是一个空的WindowsIdentity对象而以,并不对应着某个确定的Windows帐号。
[/code]
任何一个具体的Windows进程总是运行在一个确定的安全身份下。如果你手工启动一个.exe文件,被开启的进程会运行在基于当前登录帐号的身份下。如有你同时拥有多个Windows帐号,你可以通过“Run As”的方式选择一个不同于当前登录帐号的身份去运行某个.exe文件。而对于很多的Windows服务,它们大多运行在某个系统帐号下。比如我们熟悉的IIS(IIS6或者之后的版本)在默认的情况下就运行在Network Service这个系统帐号下面。当一个线程在这个进程中被创建并启动的时候,进程的安全身份会自动附加到线程上。WindowsIdentity为我们提供了如下一个GetCurrent静态方法返回基于当前线程/进程的WindowsIdentity。
[/code]
正如名称所体现的一样,GenericIdentity为我们定义了一个一般性的安全身份。GenericIdentity的定义非常简单,仅仅实现了定义在IIdentity接口的三个只读属性而以。我们可以通过指定用户名或者用户名与认证类型来创建一个GenericIdentity对象。下面的代码片断体现了GenericIdentity的整个定义。
[/code]
由于GenericIdentity的IsAuthenticated属性是只读,也不同通过存储过程对其进行初始化,那么如何确定一个通过GenericIdentity对象表示的安全身份是否已经通过认证了呢?实际上,GenericIdentity采用很简单的逻辑来判断其自身是否经过认证:如果用户名不为空,IsAuthenticated返回True,否则返回False。下面给出的代码可以验证这一点。
[/code]
X509Identity定义在程序集System.IdentityModel中,对应的命名空间是System.IdentityModel.Claims。从下面给出的定义我们可以看出X509Identity仅仅是一个内部(Internal)类型。
[/code]
X509Identity直接继承自GenericIdentity。我们可以通过传入一个X509Certificate2对象或者以X500DistinguishedName对象表示的证书的标识名称来创建X509Identity。X509Identity重写了GenericIdentity的Name属性,最终作为名称的返回的是证书的主题名称和指纹的组合,<<主题名称>>; <<指纹>>(分号之后具有一个空格,比如:CN=Foo; 12BA3675C89BD7FE00E3F7E92A620749FB9E6D89)。X509Identity对象的AuthenticationType属性为“X509”。
[/code]
你可以通过两种方式获取当前的ServiceSecurityContext,一种是通过ServiceSecurityContext的静态只读属性Current,另一种则是通过当前OperationContext的ServiceSecurityContext属性。实际上通过这两种方式得到的是同一个ServiceSecurityContext。ServiceSecurityContext对象的同一性可以通过下面的代码来验证。
[/code]
ServiceSecurityContext具有两个表示安全身份的属性PrimaryIdentity和WindowsIdentity,它们都代表当前客户端的身份。对于Windows认证,这两个属性返回同一个WindowsIdentity对象。不过需要注意的是,这是所说的Windows认证实际上包括如下三种情况:
客户端凭证为Windows凭证;
客户端凭证为用户名/密码凭证,并采用Windows认证模式;
客户端凭证为X.509证书凭证,并允许与Windows帐号进行映射。
而对于不属于上述三种情况下的非Windows凭证,当前ServiceSecurityContext的WindowsIdentity属性返回Null,而PrimaryIdentity属性则因客户端凭证类型和认证方式有所区别。具体来说,如果客户端凭证为用户名/密码凭证,并采用Membership和Custom认证模式,则在成功认证的情况下PrimaryIdentity的属性返回一个以用户名作为名称的GenericIdentity。如果客户端凭证为X.509证书凭证,但不采用Windows帐号映射机制,则PrimaryIdentity的属性返回的是一个X509Identity。
对于匿名客户端(客户端凭证类型为None),PrimaryIdentity返回的是一个空的GenericIdentity,IsAnonymous返回True。你通过静态属性Anonymous可以返回一个匿名ServiceSecurityContext。下满的表格体现了成功认证后当前ServiceSecurityContext的PrimaryIdentity与客户端凭证类型以及认证模式之间的关系。
从两个重要的概念谈起:Identity与Principal[上篇]
从两个重要的概念谈起:Identity与Principal[下篇]
当访问者入门之后,并不意味着它可以为所欲为。为了让适合的人干适合的事,就需要授权机制为具体的人设置具体的权限,并根据这些权限设置决定试图调用的操作或者访问的资源对该访问者是否是安全的。对于一个安全保障体系来说,授权是目的。但是授权的执行是假定已经通过认证体现确定了访问者真实身份,因为用于进行授权采用的权限集是基于真个确定的身份的。在真正进入对WCF授权的具体介绍之前,我们有必要来了解一下这个“身份”的问题。
目录
一、IIdentity
二、WinodwsIdentity
三、GenericIdentity
四、X509Identity
五、服务安全上下文中的身份
一、IIdentity
二、WinodwsIdentity
三、GenericIdentity
四、X509Identity
五、服务安全上下文中的身份
一、IIdentity
在.NET的安全应用编程接口中,身份通过System.Security.Principal.IIdentity接口表示。从下面表示IIdentity接口定义的代码片断中,我们可以看到这个接口定义其实很简单,它具有如下三个只读属性:Name:身份所代表的用户的名称;
IsAuthenticated:身份所代表的用户是否经过认证;
AuthenticationType:身份认证所采用的类型。
[code] public interface IIdentity { string AuthenticationType { get; } bool IsAuthenticated { get; } string Name { get; } }
[/code]
通过IIdentity表示的身份是基于某种认证类型的,不同类型的认证往往对应于不同的身份类型。以ASP.NET认证为例,如果我们采用Forms认证,那么认证后的身份通过一个FormsIdentity对象表示。而Windows Live Passport认证对应的具体身份类型则是PassportIdentity。在这里我们着重介绍一下如下三种身份类型:WindowsIdentity、GenericIdentity和X509Identity。
二、WindowsIdentity
WindowsIdentity定义在System.Security.Principal命名空间下。顾名思义,WindowsIdentity用于表示一个基于Windows认证的身份。一个采用WindowsIdentity定义的Windos身份具有一系列的属性,它们主要定义在如下的代码片断中。[code] public class WindowsIdentity : IIdentity, ... { //其他成员 public virtual string Name { get; } public string AuthenticationType { get; } public virtual bool IsAuthenticated { get; } public IdentityReferenceCollection Groups { get; } public virtual bool IsAnonymous { get; } public virtual bool IsGuest { get; } public virtual bool IsSystem { get; } }
[/code]
对于用于表示认证类型的AuthenticationType属性来说,在工作组模式下返回NTLM。对于域模式,如果操作系统是Vista或者以后的版本,该属性返回Negotiate,表示采用SPNEGO认证协议。而对于之前的Windows版本,则该属性值为Kerberos。
Groups属性返回WindowsIdentity对应的Windows帐号所在的用户组(User Group),而IsGuest则用于判断Windows帐号是否存在于Guest用户组中。IsSystem属性则表示Windows帐号是否是一个系统帐号。
如果你对ASP.NET的安全有一定的了解,应该知道我们可以对IIS进行相应的配置是ASP.NET应用支持匿名用户。也就是说,用户无需提供具体的用户凭证,而是以匿名的方式登录到ASP.NET站点中。对于匿名登录,IIS实际上会采用一个预先指定的Windows帐号进行登录。而在这里,IsAnonymous属性就表示该WindowsIdentity对应的Windows帐号是否是匿名帐号。
对于匿名身份的问题,在这里还有一点值得补充一下。WindowsIdentity定义了如下一个静态的GetAnonymous方法用于返回一个表示匿名身份的WindowsIdentity对象。但是这仅仅是一个空的WindowsIdentity对象而以,并不对应着某个确定的Windows帐号。
[code] public class WindowsIdentity : IIdentity, ... { //其他成员 public static WindowsIdentity GetAnonymous() }
[/code]
任何一个具体的Windows进程总是运行在一个确定的安全身份下。如果你手工启动一个.exe文件,被开启的进程会运行在基于当前登录帐号的身份下。如有你同时拥有多个Windows帐号,你可以通过“Run As”的方式选择一个不同于当前登录帐号的身份去运行某个.exe文件。而对于很多的Windows服务,它们大多运行在某个系统帐号下。比如我们熟悉的IIS(IIS6或者之后的版本)在默认的情况下就运行在Network Service这个系统帐号下面。当一个线程在这个进程中被创建并启动的时候,进程的安全身份会自动附加到线程上。WindowsIdentity为我们提供了如下一个GetCurrent静态方法返回基于当前线程/进程的WindowsIdentity。
[code] public class WindowsIdentity : IIdentity, ... { //其他成员 public static WindowsIdentity GetCurrent(); }
[/code]
三、 GenericIdentity
虽然对于这些我们常用的认证类型,比如Windows认证、Forms认证和Windows Live Passport认证,都具有对应的安全身份类型。如果我们采用自定义的认证方式,是否意味着我们也需要定义一个实现了IIdentity接口的类型呢?实际上是不需要的,我们可以直接使用GenericIdentity这个类型。正如名称所体现的一样,GenericIdentity为我们定义了一个一般性的安全身份。GenericIdentity的定义非常简单,仅仅实现了定义在IIdentity接口的三个只读属性而以。我们可以通过指定用户名或者用户名与认证类型来创建一个GenericIdentity对象。下面的代码片断体现了GenericIdentity的整个定义。
[code] public class GenericIdentity : IIdentity { public GenericIdentity(string name); public GenericIdentity(string name, string type); public virtual string AuthenticationType { get; } public virtual bool IsAuthenticated { get; } public virtual string Name { get; } }
[/code]
由于GenericIdentity的IsAuthenticated属性是只读,也不同通过存储过程对其进行初始化,那么如何确定一个通过GenericIdentity对象表示的安全身份是否已经通过认证了呢?实际上,GenericIdentity采用很简单的逻辑来判断其自身是否经过认证:如果用户名不为空,IsAuthenticated返回True,否则返回False。下面给出的代码可以验证这一点。
[code] var anonymousIdentity = new GenericIdentity(""); var authenticatedIdentity = new GenericIdentity("Foo"); Debug.Assert(anonymousIdentity.IsAuthenticated == false); Debug.Assert(authenticatedIdentity.IsAuthenticated == true);
[/code]
四、X509Identity
通过前面一章的介绍,我们知道了WCF具有三种典型的认证方式:Windows认证、用户名/密码认证和证书认证。认证的方式决定了安全身份的类型,对于Windows认证和用户名/密码认证,认证后的安全身份分别由一个WindowsIdentity和GenericIdentity表示。但是对于证书认证,则对应着另一种安全身份类型:X509Identity。X509Identity定义在程序集System.IdentityModel中,对应的命名空间是System.IdentityModel.Claims。从下面给出的定义我们可以看出X509Identity仅仅是一个内部(Internal)类型。
[code] internal class X509Identity : GenericIdentity, IDisposable { //其他成员 public X509Identity(X500DistinguishedName x500DistinguishedName); public X509Identity(X509Certificate2 certificate); public X509Identity Clone(); public void Dispose(); public override string Name { get; } }
[/code]
X509Identity直接继承自GenericIdentity。我们可以通过传入一个X509Certificate2对象或者以X500DistinguishedName对象表示的证书的标识名称来创建X509Identity。X509Identity重写了GenericIdentity的Name属性,最终作为名称的返回的是证书的主题名称和指纹的组合,<<主题名称>>; <<指纹>>(分号之后具有一个空格,比如:CN=Foo; 12BA3675C89BD7FE00E3F7E92A620749FB9E6D89)。X509Identity对象的AuthenticationType属性为“X509”。
五、服务安全上下文中的身份
当服务安全开始的情况,服务端在经过认证之后会创建一个上下文用以存储基于当前服务调用相关的安全相关的信息,其中就包含了代表被认证客户端的安全身份。这个上下文被称为服务安全上下文,通过类型ServiceSecurityContext表示。[code] public class ServiceSecurityContext { //其他成员 public static ServiceSecurityContext Current { get; } public IIdentity PrimaryIdentity { get; } public WindowsIdentity WindowsIdentity { get; } public bool IsAnonymous { get; } public static ServiceSecurityContext Anonymous { get; } }
[/code]
你可以通过两种方式获取当前的ServiceSecurityContext,一种是通过ServiceSecurityContext的静态只读属性Current,另一种则是通过当前OperationContext的ServiceSecurityContext属性。实际上通过这两种方式得到的是同一个ServiceSecurityContext。ServiceSecurityContext对象的同一性可以通过下面的代码来验证。
[code] var securityContext1 = OperationContext.Current.ServiceSecurityContext; var securityContext2 = ServiceSecurityContext.Current; Debug.Assert(object.ReferenceEquals(securityContext1, securityContext2));
[/code]
ServiceSecurityContext具有两个表示安全身份的属性PrimaryIdentity和WindowsIdentity,它们都代表当前客户端的身份。对于Windows认证,这两个属性返回同一个WindowsIdentity对象。不过需要注意的是,这是所说的Windows认证实际上包括如下三种情况:
客户端凭证为Windows凭证;
客户端凭证为用户名/密码凭证,并采用Windows认证模式;
客户端凭证为X.509证书凭证,并允许与Windows帐号进行映射。
而对于不属于上述三种情况下的非Windows凭证,当前ServiceSecurityContext的WindowsIdentity属性返回Null,而PrimaryIdentity属性则因客户端凭证类型和认证方式有所区别。具体来说,如果客户端凭证为用户名/密码凭证,并采用Membership和Custom认证模式,则在成功认证的情况下PrimaryIdentity的属性返回一个以用户名作为名称的GenericIdentity。如果客户端凭证为X.509证书凭证,但不采用Windows帐号映射机制,则PrimaryIdentity的属性返回的是一个X509Identity。
对于匿名客户端(客户端凭证类型为None),PrimaryIdentity返回的是一个空的GenericIdentity,IsAnonymous返回True。你通过静态属性Anonymous可以返回一个匿名ServiceSecurityContext。下满的表格体现了成功认证后当前ServiceSecurityContext的PrimaryIdentity与客户端凭证类型以及认证模式之间的关系。
从两个重要的概念谈起:Identity与Principal[上篇]
从两个重要的概念谈起:Identity与Principal[下篇]
相关文章推荐
- [WCF权限控制]从两个重要的概念谈起:Identity与Principal
- [WCF权限控制]从两个重要的概念谈起:Identity与Principal[下篇]
- [WCF权限控制]利用WCF自定义授权模式提供当前Principal[实例篇]
- [WCF权限控制]ASP.NET Roles授权[上篇]
- [WCF权限控制]ASP.NET Roles授权[上篇]
- [WCF权限控制]模拟(Impersonation)与委托(Delegation)[上篇]
- [WCF权限控制]基于Windows用户组的授权方式[上篇]
- [WCF权限控制]利用WCF自定义授权模式提供当前Principal[原理篇]
- 在OSPF中,有两个相当重要的概念:DR和BDR
- 【Java】Java语言重点概念#1——访问权限控制
- 小白学《神经网络与深度学习》笔记之六-两个重要概念及其他杂项
- Delphi线程中的两个重要概念
- 对象编程两个重要的相等概念
- 你或许理解错了Android系统权限管理的这两个概念
- 零基础写python爬虫之urllib2中的两个重要概念:Openers和Handlers
- Css中的两个重要概念:块状元素和内联元素
- 零基础写python爬虫之urllib2中的两个重要概念:Openers和Handlers
- 控制并发访问的三道屏障: WCF限流(Throttling)体系探秘[上篇]
- (转)关于spring的两个概念:IoC(控制反转) ,DI(依赖注入)
- urllib2中的两个重要概念 Openers和Handlers