zoukankan      html  css  js  c++  java
  • WCF BasicHttpBinding 安全解析(6)Digest验证(IIS宿主)

    Digest验证方式在Basic验证方式的基础上增加了摘要信息,采用的是挑战-应答模式。Digest验证也是Http安全验证的标准(RFC 2617)。

    首先我们修改服务端配置文件启用Digest验证,如代码清单11-95。

    代码清单11-95 启用Digest验证


    <basicHttpBinding>
    <binding
    name="basicBindingConf">
    <security
    mode="TransportCredentialOnly">
    <transport
    clientCredentialType="Digest">
    </transport>
    </security>
    </binding>
    </basicHttpBinding>

    同样,我们需要配置IIS启用摘要式身份验证,然后更新测试站点的服务引用。

    默认情况下是不需要显示传递用户名和密码等信息给服务端的,如果想要传递该信息可以使用代码清单11-96的方式。

    代码清单11-96 摘要验证方式显示传递身份信息


    p.ClientCredentials.HttpDigest.AllowedImpersonationLevel =
    System.Security.Principal.TokenImpersonationLevel.Impersonation;
    p.ClientCredentials.HttpDigest.ClientCredential =
    new System.Net.NetworkCredential("v-samzha","domain_pwd");

    如代码清单11-96,摘要验证的身份信息保存在p.ClientCredentials.HttpDigest对象中。

    下面我们分析一下摘要验证的过程。

    先看代码清单11-97是第发起请求的信息。

    代码清单11-97 发起请求


    POST http://wcfservicewebsite.com/HelloService.svc HTTP/1.1
    Content-Type: text/xml; charset=utf-8
    VsDebuggerCausalityData:
    uIDPo9ymOexINPFJi+3tKDrHjuIAAAAAGYVyeTYe3UqAVZgCC45FRE5qbQJALydCjORFK5/dBBoACQAA
    SOAPAction: "http://tempuri.org/IHelloService/GetHello"
    Host: wcfservicewebsite.com
    Content-Length: 133
    Expect: 100-continue
    Accept-Encoding: gzip, deflate
    Connection: Keep-Alive
    <s:Envelope
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetHello
    xmlns="http://tempuri.org/"/></s:Body></s:Envelope>

    从代码清单11-97,我们可以看出第一次请求没有提供任何验证信息,肯定是不能通过验证的,那么服务端返回的信息是什么呢?看代码清单11-98。

    代码清单11-98 第一次请求失败的服务端信息


    HTTP/1.1 401 Unauthorized
    Cache-Control: private
    Content-Type: text/html; charset=utf-8
    Server: Microsoft-IIS/7.5
    WWW-Authenticate: Digest
    qop="auth",algorithm=MD5-sess,nonce="+Upgraded+v1b410f2cc9b3070af323f9066253e36c8cf176bcec933cc01c4ff7612615bd9d42b1607c3e122bcba7ca83f47d02c6f1169f78240641df79c",charset=utf-8,realm="
    bs--yangwenhai"
    X-Powered-By: ASP.NET
    Date: Sun, 26 Jun 2011 06:25:13 GMT
    Content-Length: 6079

    服务端的返回信息中,我们关注代码清单11-98中加粗的部分,它告诉客户端验证方式为Digest,采用的摘要的算法为MD5-sess。qop表示用于服务器回应的保护等级选项,"auth"值表示鉴别;而"auth-int"值则表示采用完整性保护的鉴别。nonce字段是服务器返回的标识,每次请求都不一样,使用如下生产规则:

    time-stamp H(time-stamp ":" ETag ":" private-key)

    H为采用的摘要算法。

    realm=" bs--yangwenhai"声明当前验证的域为bs--yangwenhai。

    客户端接收到这样的反馈之后,会发送Digest验证信息给服务端,如代码清单11-99。

    代码清单11-99 客户端发送Digest验证信息

    POST http://wcfservicewebsite.com/HelloService.svc HTTP/1.1

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

    VsDebuggerCausalityData: uIDPo9ymOexINPFJi+3tKDrHjuIAAAAAGYVyeTYe3UqAVZgCC45FRE5qbQJALydCjORFK5/dBBoACQAA

    SOAPAction: "http://tempuri.org/IHelloService/GetHello"

    Accept-Encoding: gzip, deflate,gzip, deflate

    Authorization: Digest username="administrator",realm="bs--yangwenhai",nonce="+Upgraded+v1b410f2cc9b3070af323f9066253e36c8cf176bcec933cc01c4ff7612615bd9d42b1607c3e122bcba7ca83f47d02c6f1169f78240641df79c",uri="http://wcfservicewebsite.com/HelloService.svc",cnonce="+Upgraded+v1462dbb5533a1c4c04d59d91a9b10460cfd38ba314128516a4b17391abdc5a273",nc=00000001,algorithm=MD5-sess,response="9aefaca8ec6d904bb0bc43f068e328e2",qop="auth",charset=utf-8,hashed-dirs="service-name,channel-binding",service-name="HTTP/wcfservicewebsite.com",channel-binding="00000000000000000000000000000000"

    Host: wcfservicewebsite.com

    Content-Length: 133

    Expect: 100-continue

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetHello xmlns="http://tempuri.org/"/></s:Body></s:Envelope>

    代码清单11-99是客户端发送的验证信息,其中nonce值与服务端返回的值相同,客户端将密码经过Md5加密放在头部传回服务端。服务端接受到该请求之后,从header中提取有效的client credential,并通过LDAP服务,连接至DC,查找相应用户名与摘要信息匹配的domain user。如果找到,说明该 credential有效,则开始处理请求,否则,返回401给客户端。


    作者:玄魂
    出处:http://www.cnblogs.com/xuanhun/
    原文链接:http://www.cnblogs.com/xuanhun/ 更多内容,请访问我的个人站点 对编程,安全感兴趣的,加qq群:hacking-1群:303242737,hacking-2群:147098303,nw.js,electron交流群 313717550。
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    关注我:关注玄魂的微信公众号

  • 相关阅读:
    JS 面向对象
    inline-block元素间隙处理
    MUI
    MUI
    chrome://inspect调试html页面空白,DOM无法加载的解决方案
    MUI
    MUI
    MUI
    MUI
    MUI
  • 原文地址:https://www.cnblogs.com/xuanhun/p/2095553.html
Copyright © 2011-2022 走看看