您的位置:首页 > 其它

WCF之添加自定义用户名密码认证

2016-08-16 17:08 411 查看
1.创建WCF服务应用以及调用客户端(请自行google)。

2.创建X509证书

cmd 进入 C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\

  makecert.exe -sr LocalMachine -ss My -a sha1 -n CN=JJWcfService -sky exchange -pe

  



下面是各种参数的介绍

属性

解析

-sr

指定的证书存储区中的注册表位置。
currentUser
指定注册版存储位置为 HKEY_CURRENT_USER.
localMachine
指定注册版存储位置为 HKEY_LOCAL_MACHINE.

-ss

指定证书存储的位置。

-a

指定相关的算法,可以选择 MD5 算法或者 SHA1算法

-n

指定证书的名称。该名称遵循X.500命名标准。简单例子如 "CN=MyName" 格式,如果没有指定/n开关,证书默认的名称是"Joe's Software Emporium"。

-sky

证书键类型。可以设置为 exchange 或者 signature。

-pe

证书可导出

出现succeeded说明证书创建成功。

查看证书:

1.win+r==>mmc==>确定 打开控制台
2.文件==>添加/删除管理单元==>证书==>添加==>我的用户账户==>完成==>添加==>计算机账户==>下一步==>完成==>确定



导出证书:





此处务必选择是,导出私钥,设置密码导出即可,

证书(本地计算机)==>受信任的根证书颁发机构==>证书==>所有任务==>导入==>下一步==>选择证书==>输入密码==>下一步==>完成

3.为WCF服务添加自定义验证 (添加一个继承UserNamePasswordValidator的类并重写Validate)

// 自定义的用户名/密码验证类
/// </summary>
public class CustomNamePasswordValidator : System.IdentityModel.Selectors.UserNamePasswordValidator
{
/// <summary>
/// 验证指定的用户名和密码
/// </summary>
/// <param name="userName">要验证的用户名</param>
/// <param name="password">要验证的密码</param>
public override void Validate(string userName, string password)
{
if (!(userName == "xxx" && password == "xxx"))
{
throw new FaultException("用户名或密码不正确");
}
}
}


4.为WCF宿主添加如下Config配置

<system.serviceModel>
<services>
<!--name - 提供服务的类名-->
<!--behaviorConfiguration - 指定相关的行为配置-->
<service name="ZhiHeng.JinJiBox.Service.Api" behaviorConfiguration="SecurityBehavior">
<!--address - 服务地址-->
<!--binding - 通信方式-->
<!--contract - 服务契约-->
<endpoint address="" binding="wsHttpBinding" contract="ZhiHeng.JinJiBox.Service.IApi" bindingConfiguration="SecurityBindingConfiguration"  />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/CardOper" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="SecurityBehavior">
<!--httpGetEnabled - 指示是否发布服务元数据以便使用 HTTP/GET 请求进行检索,如果发布 WSDL,则为 true,否则为 false,默认值为 false-->
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<!--userNamePasswordValidationMode - 以用户名/密码模式来进行验证的方法-->
<!--UserNamePasswordValidationMode.Windows - 用户名映射到 Windows 用户-->
<!--UserNamePasswordValidationMode.MembershipProvider - 提供基于已配置的 MembershipProvider 的密码验证-->
<!--UserNamePasswordValidationMode.Custom - 基于已配置的自定义 UsernamePasswordValidator 的自定义身份验证-->
<!--customUserNamePasswordValidatorType - 所使用的自定义用户名密码验证程序的类型-->
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="ZhiHeng.JinJiBox.Service.CustomNamePasswordValidator, ZhiHeng.JinJiBox.Service" />
<!--findValue - 指定要在 X.509 证书存储区中搜索的值-->
<!--storeLocation - 指定客户端可用于验证服务器证书的证书存储区位置(LocalMachine - 分配给本地计算机的 X.509 证书存储区;CurrentUser - 当前用户使用的 X.509 证书存储区)-->
<!--storeName - 要打开的 X.509 证书存储区的名称(参看:StoreName枚举。AddressBook, AuthRoot, CertificateAuthority, Disallowed, My, Root, TrustedPeople, TrustedPublisher)-->
<!--x509FindType - 要执行的 X.509 搜索的类型(参看:X509FindType枚举)-->
<serviceCertificate findValue="JJWcfService" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="SecurityBindingConfiguration">
<security>
<!--clientCredentialType - 客户端用以进行身份验证的凭据的类型,默认值 UserName -->
<!--BasicHttpMessageCredentialType.UserName - 使用用户名凭据对客户端进行身份验证-->
<!--BasicHttpMessageCredentialType.Certificate - 使用证书对客户端进行身份验证-->
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>


启动WCF服务端。

5.生成WCF客户端Config配置

  vs==>工具==>外部工具==>添加==>标题SvcUtil,命令C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\SvcUtil.exe,初始目录$(SolutionDir)==>应用,确定==>工具==>SvcUtil==>参数输入WCF的服务地址,不出意外会在项目跟目录生成2个文件,将代理文件拷贝到WCF客户端,将配置文件拷贝到客户端配置文件中

<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IApi" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false"
transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8000/CardOper" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IApi" contract="IApi"
name="WSHttpBinding_IApi">
<identity>
<certificate encodedValue="AwAAAAEAAAAUAAAAvDJSUOU7j3OZw58oadeaOQ2hFwwgAAAAAQAAAAMCAAAwggH/MIIBaKADAgECAhAsiRVKHUl5gk8EfX3h8Nl8MA0GCSqGSIb3DQEBBAUAMBcxFTATBgNVBAMTDEpKV2NmU2VydmljZTAeFw0xNjA4MTYwNjQ3MDdaFw0zOTEyMzEyMzU5NTlaMBcxFTATBgNVBAMTDEpKV2NmU2VydmljZTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAudUXIgHlGJi7R+DJBxqfB9EldfxTk2uhRlc/miG+UEG2049rZzyg8C4buGq6czimSWw+3qdRC5ssBCFvutgsspGD4Yfnthycc9FJJrnJ7XMEi+YuvBy6dhx3VvnqYD2V8lYARbzFcjBJZPJCTfVibR9Q2ztR3fCUOaCax8JqA08CAwEAAaNMMEowSAYDVR0BBEEwP4AQbxiagPbpXbaqlELymH5aDKEZMBcxFTATBgNVBAMTDEpKV2NmU2VydmljZYIQLIkVSh1JeYJPBH194fDZfDANBgkqhkiG9w0BAQQFAAOBgQAnouchd9o4q3fR212HD4aiD5iXXQv6o7VMY4TZPn/+UDaLXEZVB5mbuCh0vnlU2zIaJCN9LuQiQ9/MyjuoTKA2lAMup+hMw5F0bnjeWOdwD6shKBqnLlY44hufbVx0U2rN8n9Yay9oCYSzBnM3WKNXIWc1TeGBJvnpDBnMqqvNeA==" />
</identity>

</endpoint>
</client>
</system.serviceModel>


6 客户端调用

using (var proxy = new Api())
{
//此出进行验证
proxy.ClientCredentials.UserName.UserName = “xxx”;
proxy.ClientCredentials.UserName.Password = "xxx";

//在下面调用你的WCF方法
proxy.SayHello("Vis");
}


参考:

  http://www.cnblogs.com/leeolevis/archive/2011/05/17/2048710.html

http://www.cnblogs.com/wuhuacong/archive/2011/09/29/2195528.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: