代码之家  ›  专栏  ›  技术社区  ›  Tr1stan

使用具有WPF Windows应用程序集成身份验证的Web服务

  •  2
  • Tr1stan  · 技术社区  · 16 年前

    我已经编写了一个使用.NET 3.5 Web服务的WPF 4.0 Windows应用程序。当托管中的Web服务允许匿名连接时,这很好,但是当我们上线时我需要使用的Web服务将保存在启用了集成身份验证的网站中。

    运行WPF应用程序的人将登录到与Web服务器位于同一域内的计算机上,并且如果使用启用了NTLM身份验证的Web浏览器浏览该Web服务,则有权查看该Web服务(不输入任何身份验证信息)。

    是否可以将运行应用程序的已登录用户的详细信息传递给WebService?

    以下是我当前使用的代码:

    MyWebService.SearchSoapClient client = new SearchSoapClient();
    //From the research I've done I think I need to something with these:
    //UserName.PreAuthenticate = true;
    //System.Net.CredentialCache.DefaultCredentials;
    List<Person> result = client.FuzzySearch("This is my search string").ToList();
    

    任何提示都非常感谢。

    以下是当前拨打电话时收到的错误消息:

    HTTP请求未经授权 客户端身份验证方案 “匿名”。身份验证头 从服务器收到的是 '协商,NTLM,摘要 qop=“auth”,algorithm=md5 sess,nonce=“+upgraded+v17 hashremoved”,charset=utf-8,realm=“digest”'。

    1 回复  |  直到 16 年前
        1
  •  5
  •   Tr1stan    16 年前

    所以在我的例子中,这个问题的解决方案非常简单。

    在app.config文件中WebService的绑定配置中,我更改了以下内容:

    <security mode="None">
      <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
     <message clientCredentialType="UserName" algorithmSuite="Default" />
    </security>
    

    对此:

    <security mode="TransportCredentialOnly">
     <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
     <message clientCredentialType="UserName" algorithmSuite="Default" />
    </security>
    

    注意,我更改了模式和clientCredentialType属性。

    在后面的代码中,我在对WebService调用方法之前添加了这一行:

    client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;