您的位置:首页 > 其它

WCF 第八章 安全 因特网上的安全服务(下) 其他认证模式

2011-02-01 13:48 447 查看
使用角色提供方进行基于角色的授权

ASP.NET 基于角色认证允许开发人员执行基于角色的授权检查。它也使用一个提供者模型,它将用于角色存储的细节从应用程序代码中抽象出来。ASP.NET 中有很多角色提供者模型,包括SqlRoleProvider, WindowsTokenRoleProvider和AuthorizationStoreRoleProvider.因为我们在使用一个面向互联网的应用程序,我们将检查如何使用SqlRoleProvider来实现授权检查。使用一个ASP.NET 角色提供者有好几个步骤。第一个步骤是开启角色用户功能。这是使用roleManager元素在app.config或者web.config中配置完成的。

<roleManager enabled="true" defaultProvider="AspNetSqlRoleProvider">


这允许应用程序使用角色,但是它不确定使用哪个角色提供者。下一步是配置一个确定使用哪种角色提供者的服务行为。列表8.34显示了在serviceAuthorization 配置元素确定principalPermissionMode 和 roleProviderName属性的一个服务行为。principalPermissionMode用来确定授权检查过程是如何执行的。在当前情况中我们使用”UseAspNetRoles”,这意味着使用ASP.NET角色来进行授权检查。我们也确定了提供者的名字。

列表8.34 使用ASP.NET角色的服务授权

<serviceAuthorization principalPermissionMode="UseAspNetRoles"
roleProviderName="AspNetSqlRoleProvider" />


列表8.34为SQL Server使用默认的ASP.NET角色提供者。列表8.35显示了machine.config中ASP.NET角色提供者的默认配置。

列表8.35 角色提供者(在machine.config 中)

<roleManager enabled="true" defaultProvider="AspNetSqlRoleProvider">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer"
type="System.Web.Security.SqlRoleProvider,
System.Web, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"/>
<add name="AspNetWindowsTokenRoleProvider" applicationName="/"
type="System.Web.Security.WindowsTokenRoleProvider,
System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</roleManager>


对ASP.NET 网页应用程序来说,执行访问检查典型的方案是调用User.IsInRole方法。这个方法对基于授权检查的经常需要显示或隐藏访问特性的网页来说很好用,但是对WCF服务来说不是很好用。WCF使用PrincipalPermissionAttribute属性在服务层执行授权检查。列表8.36显示了一个确定权限检查的服务例子。这个属性检查用户是否在管理员角色中。如果用户不属于角色,用户就会被拒绝访问服务。

列表8.36 主体权限

[ServiceContract]
public interface IService
{
[OperationContract]
[PrincipalPermission(SecurityAction.Demand, Role="Administrator")]
string GetData(int value);

[OperationContract]
[PrincipalPermission(SecurityAction.Demand, Role = "Administrator")]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}


使用窗体(Forms)认证

到目前为止所有的方法都是如何从一个基于Windows应用程序通过互联网访问服务。图片8.8显示了从浏览器通过互联网访问服务的网页应用程序。我们现在将考虑网页应用程序如何使用一个网络中心方法来安全访问WCF服务。这意味着我们想使用标准HTTP方法来安全访问我们的服务。这包括使用HTTP cookies 来认证,使用SSL加密。SSL加密在这章的开始部分已经介绍,所以我们将重点介绍使用HTTP cookies进行认证。



图片8.8 网页应用程序的因特网服务

ASP.NET提供了一个很有名的窗体认证特性,它使用HTTP cookies 来认证。窗体认证允许一个开发人员创建一个使用一个为用户登录使用的HTML窗体的网页应用程序。在用户输入用户名和密码以后,窗体被提交给网页服务器来验证权限。在用户被认证以后,一个HTTP cookies将会下发给浏览器并使用这个令牌来认证用户。默认情况下,窗体认证直接与ASP.NET Membership一起工作来执行认证检查。使用窗体认证和Membership,开发人员可以写很少代码或者不写代码来保护他们的网页应用程序。这对网页应用程序来说非常好,但是它并不会为我们提供任何WCF服务的帮助。

不幸的是,目前在WCF和窗体认证直接并没有直接的集成。幸运的是,一个简单的解决方案解决了这个问题。列表8.37显示了一个允许WCF服务使用窗体认证的自定义属性。这个属性将当前线程的主体设置到当前HttpContext确定的主体中取。这个检查的属性允许使用PricipalPermissionAttribute与窗体认证一起使用。

列表8.37 UseFromsAuthentication 属性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using System.Security.Principal;
using System.Web.Security;
using System.Threading;
using System.ServiceModel.Description;
using System.Collections.ObjectModel;

/// <summary>
/// Summary description for UseFormsAuthentication
/// </summary>
public class UseFormsAuthentication : IDispatchMessageInspector
{
public UseFormsAuthentication()
{
//
// TODO: Add constructor logic here
//
}

#region IDispatchMessageInspector Members

public object AfterReceiveRequest(ref Message request,
IClientChannel channel,
InstanceContext instanceContext)
{
IPrincipal currentUser = HttpContext.Current.User;
if ((currentUser is RolePrincipal)
&& (currentUser != Thread.CurrentPrincipal))
{
Thread.CurrentPrincipal = currentUser;
}
return null;
}

public void BeforeSendReply(ref Message reply,
object correlationState)
{
}

#endregion
}

[AttributeUsage(AttributeTargets.Class)]
public class UseFormsAuthenticationBehaviorAttribute : Attribute, IServiceBehavior
{
#region IServiceBehavior Members

public void AddBindingParameters(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase,
Collection<ServiceEndpoint> endpoints,
BindingParameterCollection bindingParameters)
{
}

public void ApplyDispatchBehavior(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
{
foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
{
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new UseFormsAuthentication());
}
}
}

public void Validate(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase)
{
}

#endregion
}


列表8.38 显示了一个使用窗体认证属性的服务。这里需要提一下,属性应该与ASP.NET兼容模式一起使用。列表8.38中显示的GameReviewService 服务使用新的webHttpBinding绑定。也允许所有用户从浏览器收集游戏视图,但是只有授权用户可以添加视图。这个绑定用来使用一个REST/POX类型终结点暴露WCF服务。它也与ASP.NET扩展功能很好的集成在一起。关于这些特性的更多细节,请参考第十三章。

列表8.38 使用UseFormsAuthentication属性的服务

[UseFormsAuthenticationBehaviorAttribute]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class myService : IService
{
public string GetData(int value)
{
WebOperationContext wctx = WebOperationContext.Current;
wctx.OutgoingResponse.Headers.Add(
HttpResponseHeader.CacheControl, "no-cache");
return string.Format("You entered: {0}", value);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: