使用wsHttpBinding构建Message安全模式和UserName授权
http://www.cnblogs.com/artech/archive/2011/05/22/authentication_01.html
https://www.cnblogs.com/Frank-yafeya/p/3283699.html
https://www.cnblogs.com/jfzhu/p/4067873.html
https://www.cnblogs.com/niaowo/p/4727378.html
1. server
a. implement UserNaePasswordValidator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IdentityModel.Selectors;
using System.ServiceModel; namespace WcfService1.Common
{
public class CustomUserNameValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (userName != "admin" || password != "abc123")
{
throw new FaultException("UserName or Password is incorrect!");
}
}
}
}
b. generate certificate
makecert.exe -sr LocalMachine -ss My -a sha1 -n CN=WcfServerCert -sky exchange –pe
c. config
<?xml version="1.0"?>
<configuration> <appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.7" />
<httpRuntime targetFramework="4.7"/>
</system.web>
<system.serviceModel>
<services>
<service name="WcfService1.Service1" behaviorConfiguration="securityBehaviorConfig">
<endpoint address="" binding="wsHttpBinding" contract="WcfService1.Contract.IService1"
bindingConfiguration="wsBindingConfig" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/wcf/Service1" />
</baseAddresses>
</host>
</service>
<service name="WcfService1.UserService">
<endpoint address="" behaviorConfiguration="WcfService1.UserServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="WcfService1.UserService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WcfService1.UserServiceAspNetAjaxBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="securityBehaviorConfig">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceCredentials>
<serviceCertificate findValue="WcfServerCert" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My"/>
<userNameAuthentication
userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="WcfService1.Common.CustomUserNameValidator, WcfService1"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="wsBindingConfig">
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true" >
<listeners>
<add name="xml"/>
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="xml"/>
</listeners>
</source>
<source name="myUserTraceSource"
switchValue="Information, ActivityTracing">
<listeners>
<add name="xml"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add name="xml"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="Error.svclog" />
</sharedListeners>
</system.diagnostics>
</configuration>
certlm.msc 添加 IIS AppPool\AppPoolName帐号,替换AppPoolName为应用池的名称,这里为WCFDemo。
2. client
var wsBinding = new WSHttpBinding();
wsBinding.Security.Mode = SecurityMode.Message;
wsBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
// for exception: Identity check failed for outgoing message. The expected DNS identity of the remote endpoint was 'localhost' but the remote endpoint provided DNS
EndpointIdentity identity = EndpointIdentity.CreateDnsIdentity("WcfServerCert");
EndpointAddress endAddress = new EndpointAddress(new Uri("http://localhost/wcf/Service1.svc"), identity); using (var factory = new ChannelFactory<IService1>(wsBinding, endAddress))
{
factory.Credentials.UserName.UserName = "admin";
factory.Credentials.UserName.Password = "abc123";
factory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode =
X509CertificateValidationMode.None; var proxy = factory.CreateChannel(); string result = proxy.GetData();
Console.WriteLine(result);
}
使用wsHttpBinding构建Message安全模式和UserName授权的更多相关文章
- [WCF安全3]使用wsHttpBinding构建基于SSL与UserName授权的WCF应用程序
上一篇文章中介绍了如何使用wsHttpBinding构建UserName授权的WCF应用程序,本文将为您介绍如何使用wsHttpBinding构建基于SSL的UserName安全授权的WCF应用程序. ...
- [WCF安全2]使用wsHttpBinding构建UserName授权的WCF应用程序,非SSL
上一篇文章中介绍了如何使用basicHttpBinding构建UserName授权的WCF应用程序,本文将为您介绍如何使用wsHttpBinding构建非SSL的UserName安全授权的WCF应用程 ...
- [WCF安全1]使用basicHttpBinding构建UserName授权的WCF应用程序
最近到了新公司,leader让我研究一下WCF的传输安全机制.以前也做过WCF的应用,但是很少涉及安全方面的东西.所以,花了三天的时间研究了一下如何在WCF的应用程序中配置安全.在这个系列文章中,我会 ...
- WCF安全3-Transport与Message安全模式
概述: WCF的安全传输主要涉及认证.消息一致性和机密性三个主题.WCF采用两种不同的机制来解决这三个涉及传输安全的问题,一般将它们成为不同的安全模式,即Transport安全模式和Message安全 ...
- WCF 内置绑定在不同的传输安全模式下的信道层
basicHttpBinding Transport安全模式信道层 Message安全模式信道层 TransportWithMessageCredential安全模式信道层 TransportCred ...
- 学会WCF之试错法——安全配置报错分析
安全配置报错分析 服务端配置 <system.serviceModel> <bindings> <wsHttpBinding> <binding name = ...
- 快速入门系列--WCF--07传输安全、授权与审核
这部分主要涉及企业级应用的安全问题,一般来说安全框架主要提供3个典型的安全行为:认证.授权和审核.除了典型的安全问题,对于一个以消息作为通信手段的分布式应用,还需要考虑消息保护(Message Pro ...
- 【WCF安全】WCF 自定义授权[用户名+密码+x509证书]
1.x509证书制作(略) 2.直接贴代码 ----------------------------------------------------------------------服务端----- ...
- WCF身份验证一:消息安全模式之<Certificate>身份验证
消息安全模式的证书身份验证方式,基于WSHttpBinding绑定协议的实现过程.主要内容:基本概念,然后是制作证书.服务端配置.客户端配置.总结.这里应该和Transport传输安全模式之证书身份验 ...
随机推荐
- [py]django url 参数/reverse和HttpResponseRedirect
参考 需要完成以下任务 - 访问http://127.0.0.1:8000/ 返回"hello maotai"或home.html - 访问http://127.0.0.1:800 ...
- Selenium+Java元素定位之三
首先自己先准备一个表格代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- iOS 设计模式-委托模式
委托是指给一个对象提供机会对另一对象中的变化做出反应或者相应另一个对象的行为.其基本思想是协同解决问题. Delegate的使用场合 对象A内部发生了一些事情,想通知对象B 对象B想监听对象A内部发生 ...
- C#可扩展数组转变为String[]数组
简单备忘: 由于需要将数据最终以逗号隔开来拼接,因而写了下面的处理方法. public void GetJoinString() { ArrayList arr = new ArrayList(); ...
- cmd 笔记(随时补充)
被一篇破解WIFI的标题文骗到了,所以学习一下CMD的命令 1 查看已经连接的wifi和密码 netsh wlan show profiles 回车 netsh wlan show profiles ...
- VS2010/MFC编程入门之五十(图形图像:GDI对象之画笔CPen)
上一节中鸡啄米讲了CDC类及其屏幕绘图函数,本节的主要内容是GDI对象之画笔CPen. GDI对象 在MFC中,CGdiObject类是GDI对象的基类,通过查阅MSDN我们可以看到,CGdiObje ...
- jquery ajax基本用法
<script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script> <s ...
- Java设计模式应用——桥接模式
性能管理系统中,数据产生后需要经过采集,汇聚,入库三个流程,用户才能查询使用. 采集可以是snmp采集,也可以是ems采集:汇聚可以使storm汇聚,也可以是spark汇聚:入库可以是hdfs入库,也 ...
- 删除github上个人的repositories的操作步骤
- ELK+Kafka学习笔记之FileBeat日志合并配置输出到kafka集群
filebeat.prospectors: - type: log #日志输出类型 enabled: true ...