wcf 使用sqlMembership证书认证
.接口
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可以 ...
随机推荐
- War3编辑器
[War3编辑器] 1.英雄在商店买东西叫“任意单位 出售物品”,英雄卖东西给商店叫“抵押物品”. 2.触发器中的一级窗口包含: 1)设置事件窗口. 2)设置条件窗口. 3)设置动作窗口. 3.二级设 ...
- IconFont使用指南
[IconFont使用指南] 为了使用IconFont,需要先建立自己的项目. 在IconFont.cn中寻找自己喜欢的图标,加入到这个新建的项目. IconFont有三种使用方式,其中FontCla ...
- Javascript Iterator
[Javascript Iterator] 1.@@iterator Whenever an object needs to be iterated (such as at the beginning ...
- 使用tor网络
在www.torproject.org/projects/torbrowser.html.en上找到合适的版本下载 下载好tor浏览器之后,解压双击Tor Browser,出现这个错误 这是因为kal ...
- sql根据最小值去重
CREATE TABLE temp2 AS SELECT MAX(id) id FROM sys_oper_procenter GROUP BY pro_title 创建一个temp2的表 根据标题分 ...
- vps vultr centos7 搭建 伟皮恩
vultr 配置 64 bit OS CentOS 7 ×64 20 GB SSD 1 CPU 512MB Memory 500GB Bandwidth √ Enable IPv6 √ ...
- STL::unordered_map/unordered_multimap
unordered_map: 和 unorder_set 相似,该容器内部同样根据 hash value 把键值对存放到相应的 bucket(slot)中,根据单个 key 来访问 value 的速度 ...
- LibreOJ 6280 . 数列分块入门 4
题目链接:https://loj.ac/problem/6280 加一个数组保存块的和. 代码: #include<iostream> #include<cstring> #i ...
- 17. Letter Combinations of a Phone Number (backtracking)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- Appium+python自动化4-等待函数
4.1 等待函数癈使用 4.1.1 为什么要使用等待函数 我们在做自动化的时候很多时候都不是很顺利,不是因为app的问题,我们的脚本也没问题,但是很多时候都会报错,比如一个页面本来就有id为1的这个元 ...