.接口
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=\&quot;Web\&quot; /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证书认证的更多相关文章

  1. WCF X.509证书双向认证小结

    最近在学习WCF X.509证书验证,想实现使用证书实现服务端和客户端的双向认证,实现原理是利用了数字证书包含的一对非对称密钥来实现数字签名及加密.所谓非对称密钥是采用两个密钥将加密和解密能力分开:一 ...

  2. WCF的用户名+密码认证方式(转)

    概述 今天在做Master Data Service(后面简称MDS)项目时需要通过WCF来使用MDS的API,从而对MDS的数据进行操作.在这个过程中,遇到了一个棘手的问题,就是在客户端调用Web ...

  3. XP机器上WCF采用X509证书加密时IIS读取证书的授权

    XP机器上WCF采用X509证书加密时IIS读取证书的授权 XP下的授权命令为:winhttpcertcfg -g -c LOCAL_MACHINE\My -s 证书名称 -a "ASPNE ...

  4. tomcat 配置客户端证书认证

    在完成配置客户端证书认证后,浏览器以https访问服务器的时候,会提示选择证书,之后,服务器端会验证证书.也就意味着只有拥有有效证书的客户端才能打开该网站. 以下是具体的配置过程. 1. 在服务器端生 ...

  5. 不同服务器之间使用svn钩子post-commit同步代码遇到的证书认证问题.md

    遇到的问题,以下其他问题都是因解决这个问题引申出来的问题 VisualSVN hooks自动同步更新到web服务器 错误信息如下: Error validating server certificat ...

  6. QT https post请求(QNetworkRequest要设置SSL证书,而SSL证书认证有三种,实测成功)

    以VS开发为例.因为https访问需要用到SSL认证,而QT默认是不支持SSL认证,所以在使用之前必须先做一些准备工作: 需要安装OpenSSL库: 1.首先打开http://slproweb.com ...

  7. APK安装时的过滤方式:包名白名单、证书认证

    1.定义一些全局变量,文件位置: Build.java (frameworks\base\core\java\android\os) /** * 包管理方式名称<br> * whiteli ...

  8. JAVA中SSL证书认证通讯

    JAVA中SSL证书认证通讯 SSL通讯服务端 /******************************************************************** * 项目名称 ...

  9. Https握手协议以及证书认证

    1. 什么是https Https = http + 加密 + 认证 https是对http的安全强化,在http的基础上引入了加密和认证过程.通过加密和认证构建一条安全的传输通道.所以https可以 ...

随机推荐

  1. 无线渗透开启WPS功能的路由器

    首先关闭网络服务 service network-manager stop wps一般可在10-20小时可以爆破开,攻击难度较低,有一些厂家的无线路由甚至无法关闭WPS功能. 开始侦听开启wps功能的 ...

  2. hibench 对CDH5.13.1进行基准测试(测试项目hadoop\spark\)HDFS作HA高可靠性

    使用CDH 5.13.1部署了HADOOP集群之后,需要进行基准性能测试. 一.hibench 安装 1.安装位置要求. 因为是全量安装,其中有SPARK的测试(SPARK2.0). 安装位置在SPA ...

  3. A class of finite groups with abelian 2-Sylow subgroups By CHIH-HAN SAH

    Remark: 1.An element of a group which conjugate to its inverse is called a real element. If $G$ has ...

  4. git config --global user.email

    加上这个就ok

  5. Example of Formalising a Grammar for use with Lex & Yacc

    Here is a sample of a data-file that we want to try and recognise. It is a list of students and info ...

  6. FileReader读取文件详解

    FileReader是一种异步文件读取机制,结合input:file可以很方便的读取本地文件. input:file 在介绍FileReader之前,先简单介绍input的file类型. <in ...

  7. 【centos】centos安装g++

    gcc在Centos下的安装:使用的是以下语句:yum install gcc 以为安装g++,类似的应该使用:yum install g++ 可是提示:command is not found 查询 ...

  8. 在Eclipse中安装python插件的方法

    一个博士给了我一堆代码,原本以为是C++或者java写的,结果是python,我压根没学过呀,不过本着语言都是相通的原则,我硬着头皮开始学习Python,当然先学习安装IDE(以前学习一门新语言,我会 ...

  9. 线特征---EDLines原理(六)

    参考文献:EDLines: A real-time line segment detector with a false detection control ----Cuneyt Akinlar  , ...

  10. sqlserver使用SQL语句创建数据库登录对象、数据库用户以及对为该用户赋予操作权限

    --创建登录名USE masterGO--PbMaster,密码123456CREATE LOGIN PbMaster with PASSWORD='1234GO --创建数据库用户USE E_Mar ...