2010-12-01 73 views
0

我正在嘗試使WP7應用程序必須與WCF服務進行交互。
我想要的是傳遞用戶名/密碼,並在WCF服務中看到它。WCF - 無法獲取Identity.UserName服務內

 Service1Client proxy = new Service1Client(); 
     proxy.ClientCredentials.UserName.UserName= "[email protected]"; 
     proxy.ClientCredentials.UserName.Password = "abc"; 

代碼,我們試圖在服務獲得認證的名字(但我們得到的Windows標識代替 - MYPC \ MyLogin)

HttpContext.Current.User.Identity.Name 

下面是客戶端配置

<configuration> 
<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
      <binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647" 
       maxReceivedMessageSize="2147483647"> 
       <!--<security mode="None" />--> 
       <security mode="TransportCredentialOnly"> 
       <!--<transport clientCredentialType="Basic" />-->     
       </security> 
      </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost:45148/Service1.svc" binding="basicHttpBinding" 
      bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1" 
      name="BasicHttpBinding_IService1" /> 
    </client> 
</system.serviceModel> 

這裏是服務器配置

 <?xml version="1.0"?> 
     <configuration> 
      <system.web> 
      <compilation debug="true" targetFramework="4.0" /> 
      </system.web> 
      <system.serviceModel> 
      <behaviors> 
       <serviceBehaviors > 
       <behavior> 
        <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
        <serviceMetadata httpGetEnabled="true"/> 
        <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
        <serviceDebug includeExceptionDetailInFaults="true"/> 
        <serviceCredentials> 
        <userNameAuthentication userNamePasswordValidationMode="Custom" 
        customUserNamePasswordValidatorType="WP7Service.WpfCustomValidator, WP7Service" /> 
        </serviceCredentials> 
       </behavior> 
       </serviceBehaviors> 
      </behaviors> 
      <bindings> 
       <basicHttpBinding> 
       <binding name="BasicHttpBinding_IService1"> 
        <security mode="TransportCredentialOnly"> 
        <transport clientCredentialType="None" /> 
        </security> 
       </binding> 
       </basicHttpBinding> 
      </bindings> 
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" /> 
      </system.serviceModel> 
      <system.webServer> 
      <modules runAllManagedModulesForAllRequests="true"/> 
      </system.webServer> 
     </configuration> 

我也有對自定義的用戶名驗證,但它從未被稱爲

public class WpfCustomValidator : 
     System.IdentityModel.Selectors.UserNamePasswordValidator 
{ 
    public override void Validate(string userName, string password) 
    { 
     if (userName == null || password == null) 
     { 
      throw new ArgumentNullException(); 
     } 
     var rf = WindsorAccessor.InitializeRepositoryFactory(userName); 

     if (!ValidateLogOn(rf, userName, password)) 
     { 
      throw new FaultException("Incorrect Username or Password"); 
     } 
    } 
} 

我想要的答案來說明如何通過HttpContext.Current.User.Identity.Name或通過當前線程配置客戶端和服務能夠看到在服務ClientCredentials.UserName.UserName身份(或任何其他方式)。

回答

1

Dominick Baier shows you how do to do this在他的博客上。

您的自定義驗證程序代碼不會被調用,因爲服務已配置爲clientCredentialType =「None」 - 即沒有身份驗證。

+0

尚未測試,但似乎它是我們需要的,tnx。 – 2010-12-13 13:36:08