xinxuaw231 发表于 2015-8-12 12:18:01

WCF BasicHttpBinding 安全解析(4)windows验证(IIS宿主)

  现在我们讨论TransportCredentialOnly安全模式下的安全配置,首先在配置文件中添加如代码清单11-84所示的配置节,配置windows验证。Windows凭据认证是基于Windows组账户或者域账户进行认证的方式。在这种认证方式下,客户端进程运行的Window帐号对应的Windows凭证被自动作为调用服务的客户端凭证,所以无需显示指定具体的Windiws凭证。如果需要另一个Windows帐号的名义调用服务,客户端就需要通知指定Windows帐号和密码的方式显式地进行客户端Windows凭证的设置。Windows凭证在WCF通过类型WindowsClientCredential表示。真正的凭证最终保存在类型为NetworkCredential的ClientCredential属性中。通过该属性,你可以指定Windows凭证的域名、用户名和密码。
  注:
  TransportCredentialOnly安全模式是明文传输,这里是为了查看通信细节才采用这种方式,实际场景不推荐使用。
  代码清单11-84 配置windows验证





   1: <bindings>
   2:
   3:   <basicHttpBinding>
   4:
   5:       <binding name="basicBindingConf">
   6:
   7:         <security mode=" TransportCredentialOnly" >
   8:
   9:         <transport clientCredentialType="Windows">
10:
11:         </transport>
12:
13:         </security>
14:
15:       </binding>
16:
17:   </basicHttpBinding>
18:
19:   </bindings>
  看清单11-84,通过<ecurity mode=" TransportCredentialOnly ">”设置安全模式为TransportCredentialOnly,然后通过设置<transport clientCredentialType="Windows">设置客户端凭据类型为Windows。此外我们需要设置站点的认证模式为Windows,设置方式如代码清单11-85。
  代码清单11-85 设置站点的认证模式为Windows





   1: <system.web>
   2:
   3:   <authentication mode="Windows"></authentication>
   4:
   5: </system.web>
  服务端更改完毕之后,还有确保IIS开启了Windows认证。最后我们更新客户端的服务引用,右键服务引用,然后单击“更新服务引用”,我们会看到如图11-36所示的弹出框。

图11-36 更新服用引用需要验证凭据
  输入验证信息之后,客户端的安全配置更新为代码清单11-86所示的内容。
  代码清单11-86 windows认证时客户端安全配置





   1: <security mode="TransportCredentialOnly">
   2:
   3:         <transport clientCredentialType="Windows" proxyCredentialType="None"
   4:
   5: alm="" />
   6:
   7:         <message clientCredentialType="UserName" algorithmSuite="Default" />
   8:
   9:         </security>
  我们传递一个错误的用户账户信息,启动测试站点,会得到如图11-37所示的错误信息。

图11-37 windows身份验证未通过
  图11-37所示的错误信息是因为客户端未提供正确的身份验证信息导致的。实际上客户端和服务端经过了三次协商,最后一次的服务端响应信息如代码清单11-87。
  代码清单11-87 验证失败的服务端响应信息头





HTTP/1.1 401 Unauthorized

Cache-Control: private

Content-Type: text/html; charset=utf-8

Server: Microsoft-IIS/7.5

WWW-Authenticate: Negotiate

WWW-Authenticate: NTLM

X-Powered-By: ASP.NET

Date: Sat, 25 Jun 2011 10:44:49 GMT

Content-Length: 6331

Proxy-Support: Session-Based-Authentication
  返回401权限验证失败的信息,验证标头为Negotiate和NTLM。
  Negotiate 身份验证协议包是 Windows 中的一个安全支持提供程序 (SSP),它提供身份验证和加密。它的作用是基于客户端计算机和服务器上支持的协议协商要用于身份验证请求的身份验证协议。在 Windows 7 和 Windows Server 2008 R2 之前的 Windows 版本中,Negotiate 包支持 NTLM 和 Kerberos。对于 Windows 7 和 Windows Server 2008 R2,已经对 Negotiate 包进行了更新,以支持更多 SSP。
  出现上面的错误信息实际上是因为我的IIS的Windows验证默认提供成为Negotiate和NTLM,当然我们还可以添加Kerberos验证方式。
  那个如何在客户端附加Windows身份验证信息呢?代码清单11-88是修改后的客户端代码。
  代码清单11-88 设置windows账户信息



public ActionResult Index()
      {
            client.ClientCredentials.Windows.ClientCredential.Domain = "corp.beisen.com";
            client.ClientCredentials.Windows.ClientCredential.UserName = "yangwenhai";
            client.ClientCredentials.Windows.ClientCredential.Password = "xuan$%^hun456";
string helloString = client.GetHello();
            ViewData["Message"] = helloString;
return View();
}

  在代码清单11-88中,我们通过设置client.ClientCredentials.Windows.ClientCredential的Domain属性来设置域信息,通过UserName属性设置用户名,通过Password属性设置密码。
  说明:
  正常情况下,客户端和服务器在同一个域环境中,是不需要传递用户名和密码的。
  运行结果如图11-38。

图11-38 Windows验证
页: [1]
查看完整版本: WCF BasicHttpBinding 安全解析(4)windows验证(IIS宿主)