使用域凭据连接到Windows Phone 8(C#)中的WCF服务

时间:2013-06-18 13:01:36

标签: c# wcf windows-phone-8

我有一个带有服务引用的Windows Phone 8项目(在C#中)。是否可以将http客户端凭据类型设置为NTLM?我需要提供域名,用户和密码。

我正在使用BasicHttpBinding和TransportCredentialOnly选项。这是完成的 * .ClientConfig文件:

<configuration>
 <system.serviceModel>
  <bindings>
    <basicHttpBinding>
     <binding name="WSSoap" maxBufferSize="XXX" maxReceivedMessageSize="XXX">
       <security mode="TransportCredentialOnly" />
      </binding>
        </basicHttpBinding>
      </bindings>
      <client>
        <endpoint address="http://<address>" binding="basicHttpBinding"
            bindingConfiguration="WSSoap" contract="WorkListService.WSSoap"
            name="WSSoap" />
      </client>
  </system.serviceModel>
 </configuration>

但我还需要将ClientCredentialType设置为NTLM并提供ServiceCredentials:Domain,UserName和Password。

通过实现生成的服务类的部分ConfigureEndpoint方法,可以在Windows应用商店应用中轻松完成此操作。但我不知道如何在Windows Phone 8中这样做。

我注意到只有UserName和Password可用(基本身份验证):

*.ClientCredentials.UserName.UserName
*.ClientCredentials.UserName.Password

1 个答案:

答案 0 :(得分:0)

我没有任何手机开发经验。我在C#服务级别做了什么。

Uri uri = new Uri("http://whatever.com");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

NetworkCredential networkCredentials = new NetworkCredential();
networkCredentials.Domain = "Domain";
networkCredentials.UserName = "User";
networkCredentials.Password = "Pass";

CredentialCache cc = new CredentialCache();
cc.Add(uri, "Basic", networkCredentials); //NTLM can be used here, but Basic usually works.

request.Credentials = cc;
request.Method = WebRequestMethods.Http.Get;

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    ...
}