我有一個簡單的服務,我嘗試設置身份驗證。在客戶端上,我希望用戶輸入他們的Windows用戶帳戶。 WCF將使用客戶端提供的用戶名/密碼並對Windows身份驗證進行身份驗證。WCF安全認證
這裏是我的服務器的app.config
<system.serviceModel>
<services>
<service name="WcfService.Service1" behaviorConfiguration="WcfService.Service1Behavior">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8731/Design_Time_Addresses/WcfService/Service1/" />
</baseAddresses>
</host>
<endpoint address ="" binding="wsHttpBinding" contract="WcfService.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfService.Service1Behavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True" />
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode = "Windows"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
這裏是我的客戶的app.config
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IService1">
<security mode = "Message">
<message clientCredentialType = "UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8731/Design_Time_Addresses/WcfService/Service1/"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
contract="ServiceReference1.IService1" name="WSHttpBinding_IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
這裏是我的客戶
ServiceReference1.Service1Client client = new WcfAuthentication.ServiceReference1.Service1Client();
client.ClientCredentials.UserName.UserName = "mywindowsusername";
client.ClientCredentials.UserName.Password = "mywindowsuserpassword";
Console.WriteLine(client.GetData(5));
但我對代碼總是得到這個例外:
{「由於與遠程端點的安全協商失敗,無法打開安全通道。這可能是由於EndpointAddress中用於創建頻道的EndpointIdentity缺失或不正確。請驗證由EndpointAddress指定或暗示的EndpointIdentity正確標識遠程端點。 「} {」安全令牌請求無效或格式錯誤元素「}
有趣的是,它非常有意義,它必須使用安全的連接。這只是瞭解wcf提供的不同認證可能性的練習。在現實世界。它適用於我的移動應用程序,用戶需要在其移動設備上提供Windows帳戶憑據。所以它使用basichttpbinding並保證我將要使用SSL(https)進行的通信。因此,例如上面使用nettcpbinding應該是正確的,因爲我認爲默認情況下它使用Transport應用程序已經用tcp加密。 – pdiddy 2010-09-01 19:59:36