WCF安全之ASP.NET兼容模式

本文是利用ASP.NET兼容模式实现WCF安全的一个完整示例,其中用到了ASP.NET的Forms身份验证及Membership,并启用了角色管理。

由于整套安全方案完全利用ASP.NET相关功能实现,而未用到WCF安全策略相关的包括WCF身份验证、WCF授权及WCF传输安全等元素,所以严格的说,这种模式不能算是WCF的安全模式,但该方案确实实现了特定应用场景下的WCF安全。

相比而言,该方案提供的安全程度比WCF的安全策要低一些(例如,未提供全过程的数据传输安全),因此,本方案适应对安全性要求不高的,以IIS为宿主的WCF应用。

本方案中的WCF服务需要以IIS为宿主,可以通过添加“启用Silverlight功能的WCF服务”的方式建立WCF服务。客户端为Silverlight,并在访问WCF服务时使用了Visual Studio 2008自动生成的代理类。

1、建立项目

通过创建“Silverlight应用程序”建立新的项目WcfSecSample,并建立承载该Silverlight的网站WcfSecSample.Web。

2、在Web项目中建立Service目录,并在该目录下添加WCF服务WeatherService,服务类的完整代码如下:

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Principal;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;namespace WcfSecSample.Web
{[ServiceContract(Namespace = "")][AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]public class WeatherService{private static string s_weather = "Sunny";[OperationContract]public void SetWeather(string weather){if (!HttpContext.Current.User.IsInRole("Admin")) throw new ApplicationException("无权限。");s_weather = weather;}[OperationContract]public string GetWeather(){if (!HttpContext.Current.User.IsInRole("Guest")) throw new ApplicationException("无权限。");return s_weather;}}
}

3、在Web项目中添加用于登录的WCF服务LoginService,该服务类的完整代码如下:

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;namespace WcfSecSample.Web
{[ServiceContract(Namespace = "")][AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]public class LoginService{[OperationContract]public bool Login(string userName, string password){bool isValid = Membership.ValidateUser(userName, password);if (isValid) FormsAuthentication.SetAuthCookie(userName, false);return isValid;}[OperationContract]public void SignOut(){FormsAuthentication.SignOut();}}
}

4、在Web项目中添加继承自MembershipProvider类的CustomerMembershipProvider类,暂时只实现了本方案所需要的ValidateUser方法。ValidateUser方法的代码如下:

        public override bool ValidateUser(string username, string password){return username == "admin" && password == "123456" || username == "guest";}

5、在Web项目中添加继承自RoleProvider类的CustomRoleProvider类,暂时只实现了本方案所需要的GetRolesForUser方法。GetRolesForUser方法的代码如下:

        public override string[] GetRolesForUser(string username){if (username == "admin") return new []{"Admin", "Guest"};return new[] { "Guest" };}

需要注意的是,虽然凭感觉HttpContext.Current.User.IsInRole方法应该最终调用RoleProvider类的IsUserInRole方法,但事实却是最终调用了RoleProvider类的GetRolesForUser方法完成的。if (!HttpContext.Current.User.IsInRole("Admin"))还可以换成if (Roles.IsUserInRole(HttpContext.Current.User.Identity.Name, "Admin")),同样是最终调用了RoleProvider类的GetRolesForUser方法。

6、配置Web.config文件

在system.web节内添加如下内容:

    <authentication mode="Forms" ><forms name=".sec" loginUrl="LoginService.svc"></forms></authentication><membership defaultProvider="default"><providers><add name="default" type="WcfSecSample.Web.CustomerMembershipProvider, WcfSecSample.Web"/></providers></membership><roleManager defaultProvider="default" enabled="true"><providers><add name="default" type="WcfSecSample.Web.CustomRoleProvider, WcfSecSample.Web"/></providers></roleManager>

以上三节内容分别配置了身份验证模式、MembershipProvider及RoleProvider。

然后在示例服务所在的Service目录下添加Web.config文件,禁止对Service目录的匿名访问。该文件的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration><system.web><authorization><deny users="?"/></authorization></system.web>
</configuration>

至此,服务端的工作就完成了,接下来建立客户端测试示例。

7、添加服务引用。

在Silverlight项目中添加对WeatherService的服务引用WeatherServiceRef。需注意的是进行该操作时需要暂时允许对Service目录的匿名访问。

8、在MainPage中添加测试代码。完成之后的代码如下:

MainPage.xaml文件:

<UserControl x:Class="WcfSecSample.MainPage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"><Grid x:Name="LayoutRoot" Width="200" Margin="100" ><Grid.RowDefinitions><RowDefinition Height="Auto"></RowDefinition><RowDefinition Height="Auto"></RowDefinition><RowDefinition Height="Auto"></RowDefinition><RowDefinition Height="Auto"></RowDefinition><RowDefinition Height="Auto"></RowDefinition><RowDefinition Height="Auto"></RowDefinition><RowDefinition Height="*"></RowDefinition></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="Auto" ></ColumnDefinition><ColumnDefinition Width="*" ></ColumnDefinition></Grid.ColumnDefinitions><TextBlock Grid.Row="0" Grid.Column="0" Margin="5">UserName:</TextBlock><TextBox Grid.Row="0" Grid.Column="1" Margin="5" Name="txtUserName" Text="admin" ></TextBox><TextBlock Grid.Row="1" Grid.Column="0" Margin="5">Password:</TextBlock><TextBox  Grid.Row="1" Grid.Column="1" Margin="5" Name="txtPassword" Text="123456"></TextBox><Button Name="btnLogin"  Grid.Row="2" Grid.ColumnSpan="2" Margin="5" Content="Login" Click="Login"></Button><Button Name="btnSignOut" Grid.Row="3" Grid.ColumnSpan="2" Margin="5" Content="SignOut" Click="SignOut"></Button><Button Name="btnSetWeather" Grid.Row="4" Grid.ColumnSpan="2" Margin="5" Content="SetWeather" Click="SetWeather"></Button><Button Name="btnGetWeather" Grid.Row="5" Grid.ColumnSpan="2" Margin="5" Content="GetWeather" Click="GetWeather"></Button></Grid>
</UserControl>

MainPage.xaml.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WcfSecSample.LoginServiceRef;
using WcfSecSample.WeatherServiceRef;namespace WcfSecSample
{public partial class MainPage : UserControl{public MainPage(){InitializeComponent();this.IsLogin = false;this.client.SetWeatherCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_SetWeatherCompleted);this.client.GetWeatherCompleted += new EventHandler<GetWeatherCompletedEventArgs>(client_GetWeatherCompleted);this.loginClient.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(loginClient_LoginCompleted);this.loginClient.SignOutCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(loginClient_SignOutCompleted);}private bool isLogin;private WeatherServiceClient client = new WeatherServiceClient();private LoginServiceClient loginClient = new LoginServiceClient();private bool IsLogin{get { return isLogin; }set{isLogin = value;this.btnLogin.IsEnabled = !isLogin;this.btnSignOut.IsEnabled = isLogin;}}private void Login(object sender, RoutedEventArgs e){this.loginClient.LoginAsync(this.txtUserName.Text, this.txtPassword.Text);}private void SignOut(object sender, RoutedEventArgs e){this.loginClient.SignOutAsync();}private void GetWeather(object sender, RoutedEventArgs e){this.client.GetWeatherAsync();}private void SetWeather(object sender, RoutedEventArgs e){this.client.SetWeatherAsync("Cloudy");}void loginClient_LoginCompleted(object sender, LoginCompletedEventArgs e){if (e.Error == null){MessageBox.Show(e.Result ? "Login succeed." : "Login faild.");this.IsLogin = e.Result;}else{MessageBox.Show(e.Error.Message);}}void loginClient_SignOutCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e){if (e.Error == null){MessageBox.Show("SignOut.");this.IsLogin = false;}else{MessageBox.Show(e.Error.Message);}}void client_GetWeatherCompleted(object sender, GetWeatherCompletedEventArgs e){if (e.Error == null) MessageBox.Show(e.Result);else MessageBox.Show(e.Error.Message);}void client_SetWeatherCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e){if (e.Error == null){MessageBox.Show("Set weather succeed.");}else{MessageBox.Show(e.Error.Message);}}}
}

为了测试登录对访问服务的影响,以上代码并未根据登录状态对SetWeather、GetWeather按钮的可用性进行控制。

运行示例,可以看到在登录之前访问WeatherService是不成功的,如果用Admin角色的账号登录之后可以SetWeather或GetWeather,如果用Guest角色的账号登录则只能GetWeather。登录并调用GetWeather的效果图如下:

image

示例测试环境:

操作系统:Windows7

开发环境:Visual Studio 2008 + Silverlight 3

IIS:7.5

浏览器:IE8

转载于:https://www.cnblogs.com/chinadhf/archive/2010/04/29/1724388.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/406217.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

javase基础复习攻略《七》

容器是什么&#xff1f;通俗的讲容器指可以装其它东西的器皿&#xff0c;前面我们提到的数组便是容器的一种&#xff0c;容器的概念在JAVA中便可以理解为用来存储其它对象的器皿。本篇就让我们一起来认识一下JAVA为我们提供的容器类。 1、容器API&#xff1a; J2SDK提供的容器A…

mysql2000数据库四合一_MSSQL2000四合一

MSSQL2000四合一是包含了MS Sql Server 2000 DEVELOPER、ENTERPRISE、PERSONAL、STANDARD 四个版本。在解压目录下有 SQL2000-KB884525-SP4-x86-CHS.EXE 这是SQL 2000 的 sp4 补丁。记录安装完后一定要安装上。个人版、企业版、专业版、开发版SQL Server 2000 是 Microsoft .NE…

数据库的种类

From:http://www.job168.com/info/read_87955.html 数据库的种类 大型数据库有&#xff1a;Oracle、Sybase、DB2、SQL server 小型数据库有&#xff1a;Access、MySQL、BD2等。 2007年4月29日消息&#xff0c;国外媒体报道&#xff0c;据权威调研机构IDC初步数据显示&#x…

Visual Studio Code超级装逼编辑自带火花插件Power Mode

Visual Studio Code超级装逼编辑自带火花插件Power Mode

IE6不支持PNG图片透明效果的完美解决方案(完善版)

可怜的IE6&#xff0c;不支持PNG图片透明&#xff0c;这已经是其众多“BUG”中&#xff0c;不是那么显眼的一个&#xff0c;但也是让部分人头痛的一个了。&#xff08;今天在机房那破机器上IE6忘了抓图了&#xff0c;现在想给大家看效果也难啊。&#xff09;发个PNG大家看看。 …

更改VS的运行主窗体

Program.cs中Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Form1());把new Form1()改成你需要启动的窗体转载于:https://www.cnblogs.com/moyuling/p/4337593.html

jms mysql_JMS学习九(ActiveMQ的消息持久化到Mysql数据库)

1、将连接Mysql数据库的jar文件&#xff0c;放到ActiveMQ的lib目录下2、修改ActiveMQ的conf目录下的active.xml文件&#xff0c;修改数据持久化的方式2.1 修改原来的kshadb的持久化数据的方式2.2 连接Mysql的配置(注意配置文件放置的位置)3、将数据持久化Mysql的运行截图3.1 …

Dynamic Performance Tables not accessible,Automatic Statistics...

From: http://igogo007.iteye.com/blog/443609 使用的PLSQL Developer 工具在对oracle 10g插入数据或查询数据的时候都会报以下错误&#xff1a;Dynamic Performance Tables not accessible, Automatic Statistics Disabled for this session. You can disable statistics…

看完这篇不要告诉我不会封装ant design弹框组件了

我是歌谣 放弃很容易 但是坚持一定很酷 微信公众号关注前端小歌谣 获取前端学习知识 1设计需求 封装一个弹框组件 直接调用接口 2技术栈 ant designreact 设计第一步 绘制样式 <ModalmaskClosable{false}visible{visible}title{签收协议}onOk{this.handleSignFor}onCancel…

vue+element+el-table表格简单封装成组件

vueelementel-table表格简单封装成组件

[文档].Altera - Avalon接口规范

Ch 1 导言 1.1 Avalon属性及参数 1.2 信号类型 1.3 接口时序 1.4 相关文档 Ch 2 时钟接口 2.1 时钟输入&#xff08;Sink&#xff09; 2.1.1 属性 2.1.2 信号类型 2.1.3 associatedClock接口 2.2 时钟输出&#xff08;Source&#xff09; 2.2.1 属性 2.2.2 信号类型 Ch 3 Avalo…

C++类库:OTL通用的数据库连接类库

From: http://www.cppblog.com/lovefeel2004/archive/2010/06/02/116946.aspx OTL是一个纯C的通用数据库连接模板库&#xff0c;可以支持各种当下流行的数据库&#xff0c;如Oracle&#xff0c;Sybase, MySQL, PostgreSQL, EnterpriseDB, SQLite, MS ACCESS, Firebird等等.它是…

mysql 报错3534_win7下安装MYSQL报错:MYSQL 服务无法启动的3534问题

win7下安装MYSQL,只到“net start mysql”这一步报错:3534的错误&#xff1a;是直接官网下载的压缩文件.不是安装文件.解决方法:1:环境变量PATH添加完成(例如&#xff1a;h:\mysql\\bin2:在mysql目录下&#xff0c;新建data目录。3:在mysql目录下 新建新建一个默认配置文件my.i…

Linux下Socket网络编程

什么是Socket Socket接口是TCP/IP网络的API&#xff0c;Socket接口定义了许多函数或例程&#xff0c;程序员可以用它们来开发TCP/IP网络上的应用程序。要学Internet上的TCP/IP网络编程&#xff0c;必须理解Socket接口。 Socket接口设计者最先是将接口放在Unix操作系统里面的。如…

软件测试课初感

在上课以前&#xff0c;说起软件测试&#xff0c;就连我们这些学了两年多的大学生&#xff0c;也会觉得“测试&#xff0c;不就是用一用&#xff0c;找找编写的时候没有发现的bug&#xff0c;然后就完事了”。 然而呢&#xff0c;改观就在第一节课上&#xff0c;直到老师老师举…

npm-shrinkwrap锁定依赖

npm-shrinkwrap锁定依赖

windows和linux下,查看oracle SID的方法

From: http://www.linuxdiyf.com/viewarticle.php?id78524 Windows 平台下&#xff1a;你可以到注册表去&#xff0c;看看 HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE 下面&#xff0c;有一个 ORACLE_SID。linux平台下&#xff1a;如果你的server是linux的话1.以安装oracle的账号…