在wcf中使用自定义的用户名和密码验证方式

https://msdn.microsoft.com/en-us/library/aa702565.aspx

http://www.codeproject.com/Articles/96028/WCF-Service-with-custom-username-password-authenti

To configure a service to use a custom user name and password validator

  1. Configure a binding that uses message security over any transport or transport-level security over HTTP(S).

    When using message security, add one of the system-provided bindings, such as a <wsHttpBinding>, or a <customBinding> that supports message security and the UserName credential type.

    When using transport-level security over HTTP(S), add either the <wsHttpBinding> or <basicHttpBinding>, a <netTcpBinding> or a <customBinding> that uses HTTP(S) and the Basic authentication scheme.

    Note

    When .NET Framework version 3.5 or later is used, you can use a custom username and password validator with message and transport security. With WinFX, a custom username and password validator can only be used with message security.

    Tip

    For more information on using <netTcpBinding> in this context, see <security> of <netTcpBinding>

    1. In the configuration file, under the <system.serviceModel> element, add a <bindings> element.

    2. Add a <wsHttpBinding> or <basicHttpBinding> element to the bindings section. For more information about creating an WCF binding element, see How to: Specify a Service Binding in Configuration.

    3. Set the mode attribute of the <security> of <wsHttpBinding> or <security> of <basicHttpBinding> to Message, Transport, or TransportWithMessageCredential.

    4. Set the clientCredentialType attribute of the <message> of <wsHttpBinding> or <transport> of <wsHttpBinding>.

如果第3步使用的是Security是Message,那么就设置MessageSecurity的MessageClientCredentialType为UserName

When using message security, set the clientCredentialType attribute of the <message> of <wsHttpBinding> to UserName.

When using transport-level security over HTTP(S), set the clientCredentialType attribute of the <transport> of <wsHttpBinding> or <transport> of <basicHttpBinding> to Basic.

Note

When a WCF service is hosted in Internet Information Services (IIS) using transport-level security and the UserNamePasswordValidationMode property is set to Custom, the custom authentication scheme uses a subset of Windows authentication. That is because in this scenario, IIS performs Windows authentication prior to WCF invoking the custom authenticator.

For more information about creating an WCF binding element, see How to: Specify a Service Binding in Configuration.

The following example shows the configuration code for the binding.

<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="Binding1">
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>

netTcpBinding的security的Mode属性,可以设置为4种类型,选择Message

 
如果Mode设置为Message的话,那么就需要设置MessageSecurity的MessageClientCredentialType为UserName
如果Mode设置为Transport的话,那么就需要设置TransportSecurity的TransportClientCredentialType

2.Configure a behavior that specifies that a custom user name and password validator is used to validate user name and password pairs for incoming UserNameSecurityToken security tokens.

  1. As a child to the <system.serviceModel> element, add a <behaviors> element.

  2. Add a <serviceBehaviors> to the <behaviors> element.

  3. Add a <behavior> of <serviceBehaviors> element and set the name attribute to an appropriate value.

  4. Add a <serviceCredentials> to the <behavior> of <serviceBehaviors> element.

  5. Add a <userNameAuthentication> to the <serviceCredentials>.

  6. Set the userNamePasswordValidationMode to Custom.

    Important

    If the userNamePasswordValidationMode value is not set, WCF uses Windows authentication instead of the custom user name and password validator.

  7. Set the customUserNamePasswordValidatorType to the type that represents your custom user name and password validator.

The following example shows the <serviceCredentials> fragment to this point.

<serviceCredentials>

<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Microsoft.ServiceModel.Samples.CalculatorService.CustomUserNameValidator, service" />

</serviceCredentials>


最新文章

  1. Thinkphp控制器,1.多层控制器2.多级控制器
  2. ASP.NET中的XML和JSON
  3. jeasyUI的treegrid批量删除多行(转载)
  4. VS发布,应用程序验证未成功。无法继续。
  5. ListView实现原理
  6. 【Android开发坑系列】之经常被忽略的背景图片内存泄露
  7. easyui datagrid 表格组件列属性formatter和styler使用方法
  8. MySQL数据库最大连接数
  9. Codeforces Beta Round #2B(dp+数学)
  10. HDU 1175 连连看(BFS)
  11. OpenCV2马拉松第14圈——边缘检測(Sobel,prewitt,roberts)
  12. css学习笔记二
  13. CSS 实现图片灰度效果 兼容各种浏览器
  14. PAT (Advanced Level) 1078. Hashing (25)
  15. html5 storage事件
  16. GIS中的坐标系【Esri官方文档部分翻译】
  17. Redis数据结构之skiplist
  18. 初步:jenkins自动构建安卓Apk
  19. javascript取querystring,存储为hash
  20. [rejected] master -&gt; master (fetch first)

热门文章

  1. 关于C++ string 的神奇用法
  2. Optimal Subsequences(主席树)
  3. Go-Mutex互斥量
  4. 在LIPS表追加拣配数量PIKMG字段(转)
  5. 常用的AJAX弹出层代码
  6. [Codeforces600E] Lomsat gelral(树上启发式合并)
  7. python学习第八天二进制和字符编码有关联
  8. iOS crash log 解析
  9. Failed to load C:\ProgramFilesTwo\Android\sdk\build-tools\27.0.3\lib\dx.jar
  10. go语言从例子开始之Example37.Go 状态协程