.接口
namespace Aretch.WcfService.Services.Interface
{
[ServiceContract]
public interface ICalculator
{
[OperationContract]
void test();
}
}
.服务类
public class CalculatorService : ICalculator
{
public void test()
{
Console.WriteLine("test");
}
}
.Aretch.WcfService.Services类库的配置文件app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
<connectionStrings>
<add name="hra" connectionString="Server=.\SQLEXPRESS;DataBase=hra;uid=sa;pwd=kxbkxqkxm123" providerName="System.Data.SqlClient"/>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApplication3-20180424021512.mdf;Initial Catalog=aspnet-WebApplication3-20180424021512;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<membership defaultProvider="myProvider">
<providers>
<!--System.Web.Security.SqlMembershipProvider,System.Web-->
<add name="myProvider" type="System.Web.Providers.DefaultMembershipProvider,System.Web.Providers"
connectionStringName="hra"
applicationName="MembershipAuthenticationDemo"
requiresQuestionAndAnswer="false"/>
</providers>
</membership>
<roleManager defaultProvider="defaultRoleProvider">
<providers>
<add name="defaultRoleProvider"
type="System.Web.Providers.DefaultRoleProvider,System.Web.Providers"
connectionStringName="hra"
applicationName="MembershipAuthenticationDemo"/>
</providers>
</roleManager>
</system.web>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
<system.serviceModel>
<bindings>
<ws2007HttpBinding>
<binding name="userNameCredentialBinding">
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</ws2007HttpBinding>
</bindings>
<services>
<service name="Aretch.WcfService.Services.CalculatorService" behaviorConfiguration="membershipAuthentication" >
<endpoint address="http://127.0.0.1/calculatorService" binding="ws2007HttpBinding" bindingConfiguration="userNameCredentialBinding"
contract="Aretch.WcfService.Services.Interface.ICalculator"> </endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="membershipAuthentication">
<serviceCredentials>
<serviceCertificate storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" findValue="DESKTOP-SH76H7U"> </serviceCertificate>
<userNameAuthentication
userNamePasswordValidationMode="MembershipProvider"
membershipProviderName="myProvider"> </userNameAuthentication>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel> </configuration>
.这里我使用本机名称DESKTOP-SH76H7U做了个证书,将其利用mmc控制台的导入导出功能让其在个人区域和受信任区域都存在
.WebApplication3
5.1 服务调用
protected void Button1_Click(object sender, EventArgs e)
{
using (ChannelFactory<ICalculator> channelFactor = new ChannelFactory<ICalculator>("calculationService"))
{
UserNamePasswordClientCredential credential = channelFactor.Credentials.UserName;
credential.UserName = "zhansan";
credential.Password = "pass@word";
ICalculator calculator = channelFactor.CreateChannel();
try
{
calculator.test();
Response.Write("zhansan服务调用成功<br>");
}
catch (Exception ex)
{
channelFactor.Abort();
Response.Write("服务调用失败"); } } using (ChannelFactory<ICalculator> channelFactor = new ChannelFactory<ICalculator>("calculationService"))
{
UserNamePasswordClientCredential credential = channelFactor.Credentials.UserName;
credential.UserName = "lisi";
credential.Password = "pass@word";
ICalculator calculator = channelFactor.CreateChannel();
try
{
calculator.test();
Response.Write("成功");
}
catch (Exception ex)
{
channelFactor.Abort();
Response.Write("lisi服务调用失败"); } }
}
.2配置文件web.config
<?xml version="1.0"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
<connectionStrings>
<add name="hra" connectionString="Server=.;DataBase=hra;uid=sa;pwd=kxbkxqkxm123"/>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApplication3-20180424021512.mdf;Initial Catalog=aspnet-WebApplication3-20180424021512;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<!--
有关 web.config 更改的说明,请参见 http://go.microsoft.com/fwlink/?LinkId=235367。 可在 <httpRuntime> 标记上设置以下特性。
<system.Web>
<httpRuntime targetFramework="4.5" />
</system.Web>
-->
<system.web>
<membership defaultProvider="myProvider">
<providers>
<add name="myProvider" type="System.Web.Security.SqlMembershipProvider,System.Web" connectionStringName="hra" applicationName="MembershipAuthenticationDemo" requiresQuestionAndAnswer="false"/>
</providers>
</membership>
<authentication mode="None"/>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime/>
<pages controlRenderingCompatibilityVersion="4.0">
<namespaces>
<add namespace="System.Web.Optimization"/>
<add namespace="Microsoft.AspNet.Identity"/>
</namespaces>
<controls>
<add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/>
</controls>
</pages>
<!--<membership>
<providers>
-->
<!--
已在此模板中禁用 ASP.NET 成员身份。请访问以下链接 http://go.microsoft.com/fwlink/?LinkId=301889,以了解此模板中的 ASP.NET 成员身份支持
-->
<!--
<clear/>
</providers>
</membership>-->
<profile>
<providers>
<!--
已在此模板中禁用 ASP.NET 成员身份配置文件。请访问以下链接 http://go.microsoft.com/fwlink/?LinkId=301889,以了解此模板中的 ASP.NET 成员身份支持
-->
<clear/>
</providers>
</profile>
<!--<roleManager>
--><!--
已在此模板中禁用 ASP.NET 成员身份角色。请访问以下链接 http://go.microsoft.com/fwlink/?LinkId=301889,以了解此模板中的 ASP.NET 成员身份支持
--><!--
<providers>
<clear/>
</providers>
</roleManager>-->
<!--
如果要部署到具有多个 Web 服务器实例的云环境,
则应将会话状态模式从 "InProc" 更改为“自定义”。此外,
还应将名为 "DefaultConnection" 的连接字符串更改为连接到
SQL Server (包括 SQL Azure 和 SQL Compact)实例,而不是连接到 SQL Server Express 实例。
-->
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection"/>
</providers>
</sessionState>
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
</httpModules>
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthentication"/>
<remove name="ApplicationInsightsWebTracking"/>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler"/>
</modules>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" culture="neutral" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.OAuth" culture="neutral" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.Cookies" culture="neutral" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" culture="neutral" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
<system.serviceModel>
<bindings>
<ws2007HttpBinding>
<binding name="userNameCredentialBinding">
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</ws2007HttpBinding>
</bindings>
<client>
<endpoint name="calculationService" behaviorConfiguration="peerTrustSvcCertValidation" address="http://127.0.0.1/calculatorService" binding="ws2007HttpBinding" bindingConfiguration="userNameCredentialBinding" contract="Aretch.WcfService.Services.Interface.ICalculator">
<identity>
<certificateReference storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" findValue="DESKTOP-SH76H7U"/>
</identity>
</endpoint>
</client>
<behaviors>
<endpointBehaviors>
<behavior name="peerTrustSvcCertValidation">
<clientCredentials>
<serviceCertificate>
<authentication certificateValidationMode="PeerTrust"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
wcf 使用sqlMembership证书认证的更多相关文章
-
WCF X.509证书双向认证小结
最近在学习WCF X.509证书验证,想实现使用证书实现服务端和客户端的双向认证,实现原理是利用了数字证书包含的一对非对称密钥来实现数字签名及加密.所谓非对称密钥是采用两个密钥将加密和解密能力分开:一 ...
-
WCF的用户名+密码认证方式(转)
概述 今天在做Master Data Service(后面简称MDS)项目时需要通过WCF来使用MDS的API,从而对MDS的数据进行操作.在这个过程中,遇到了一个棘手的问题,就是在客户端调用Web ...
-
XP机器上WCF采用X509证书加密时IIS读取证书的授权
XP机器上WCF采用X509证书加密时IIS读取证书的授权 XP下的授权命令为:winhttpcertcfg -g -c LOCAL_MACHINE\My -s 证书名称 -a "ASPNE ...
-
tomcat 配置客户端证书认证
在完成配置客户端证书认证后,浏览器以https访问服务器的时候,会提示选择证书,之后,服务器端会验证证书.也就意味着只有拥有有效证书的客户端才能打开该网站. 以下是具体的配置过程. 1. 在服务器端生 ...
-
不同服务器之间使用svn钩子post-commit同步代码遇到的证书认证问题.md
遇到的问题,以下其他问题都是因解决这个问题引申出来的问题 VisualSVN hooks自动同步更新到web服务器 错误信息如下: Error validating server certificat ...
-
QT https post请求(QNetworkRequest要设置SSL证书,而SSL证书认证有三种,实测成功)
以VS开发为例.因为https访问需要用到SSL认证,而QT默认是不支持SSL认证,所以在使用之前必须先做一些准备工作: 需要安装OpenSSL库: 1.首先打开http://slproweb.com ...
-
APK安装时的过滤方式:包名白名单、证书认证
1.定义一些全局变量,文件位置: Build.java (frameworks\base\core\java\android\os) /** * 包管理方式名称<br> * whiteli ...
-
JAVA中SSL证书认证通讯
JAVA中SSL证书认证通讯 SSL通讯服务端 /******************************************************************** * 项目名称 ...
-
Https握手协议以及证书认证
1. 什么是https Https = http + 加密 + 认证 https是对http的安全强化,在http的基础上引入了加密和认证过程.通过加密和认证构建一条安全的传输通道.所以https可以 ...
随机推荐
-
从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://w ...
-
Ubuntu 下配置Ganglia监控
Ganglia是比较知名的开源监控系统, 运维上需要关注的一些通用的状态都有所涉及.其组成主要是gmond(监控程序),gmetad(信息收集程序),web(监控数据展现app).ubuntu的apt ...
-
线程池的使用ExecutorService
private ExecutorService executorService = Executors.newFixedThreadPool(5); // 引入线程池来管理多线程 private vo ...
-
如何让SQLPLUS实现带日期的时间戳
from http://www.itpub.net/thread-1876506-4-1.html SQL> defineDEFINE _DATE = "20-7月 ...
-
A手机等的网络udp广播,收到广播以后回复udp消息
B手机:向A手机发送一条消息,等待A回复 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: ...
-
【转】Android自定义控件
原文网址:http://blog.163.com/ppy2790@126/blog/static/103242241201382210910473/ 开发自定义控件的步骤: 1.了解View的工作原理 ...
-
如何成为一名优秀的web前端工程师(前端攻城师)?
程序设计之道无远弗届,御晨风而返.———— 杰佛瑞 · 詹姆士 我所遇到的前端程序员分两种:第一种一直在问:如何学习前端?第二种总说:前端很简单,就那么一点东西. 我从没有听到有人问:如何做一名优秀. ...
-
JDBC插入百万数据,不到5秒!
java自带的批量操作,就可以很好的支持大量数据的处理.相比c#,简单很多.c#要使用oracle提供的ODP.NET,效率才很高,但是代码却很复杂.总之,在这方面,c#没得比.当然,这里的表是没加索 ...
-
SICP-2锻炼.34
[锻炼2.34] 为x给定值,找到一个多项式x的值,它也可以被形式化为累积. 下多项式的值: an*x^n + an-1*x^n-1 + .... + a1*x + a0 採用著名的Horner规则, ...
-
C# RabbitMQ延迟队列功能实战项目演练
一.需求背景 当用户在商城上进行下单支付,我们假设如果8小时没有进行支付,那么就后台自动对该笔交易的状态修改为订单关闭取消,同时给用户发送一份邮件提醒.那么我们应用程序如何实现这样的需求场景呢?在之前 ...