2015-10-07 71 views
0

我幾乎沒有嘗試爲我的WCF(發佈在IIS 8.5)上設置BasicAuthentification。但我總是得到這些錯誤之一:WCF與BasicAuthentification問題

http request was forbidden with client authentication scheme 'basic'. Got following authentificationheader "Digest qop="auth",algorithm=MD5-sess,nonce="someMD5stuff",charset=utf-8,realm="Digest",Negotiate,NTLM,Basic realm="localhost"" from the server.

The HTTP request was forbidden with client authentication scheme 'Basic'. Got authentificationheader "Basic realm="localhost"" from server.

我的web.config文件服務器端(在WCF):

<system.serviceModel> 
<services> 
    <service name="WCF_for_APP.Service1"> 
    <endpoint 
     address="" 
     binding="basicHttpBinding" 
     contract="WCF_for_APP.Service"/> 
    </service> 
</services> 
<bindings> 
    <basicHttpBinding> 
    <binding name="BasicHttpBinding_IPersonService" /> 
    <binding> 
     <security mode="TransportCredentialOnly"> 
     <transport clientCredentialType="Basic" proxyCredentialType="None" realm=""/> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 
<client> 
    <endpoint address="http://somewhere/customerService" 
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPersonService" 
    contract="PersonService.IPersonService" name="BasicHttpBinding_IPersonStateService" /> 
</client> 
<behaviors> 
    <serviceBehaviors> 
    <behavior>   
     <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/> 
     <serviceAuthenticationManager authenticationSchemes="Basic"></serviceAuthenticationManager> 
     <serviceCredentials> 
     <userNameAuthentication userNamePasswordValidationMode="Custom" 
      customUserNamePasswordValidatorType="CustomerValidator.SecureBindingUsernamePasswordValidator, CustomerValidator" /> 
     </serviceCredentials> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
</system.servicemodel> 

我試過它設置一個名稱綁定配置等,但它並沒有改變一件事情。

我試着通過的ChannelFactory客戶方訪問我的WCF的ASP應用:

EndpointAddress endpointAddress = new EndpointAddress(endpointadress); 

BasicHttpBinding basicHttpBinding = new BasicHttpBinding(); 
basicHttpBinding.ReaderQuotas.MaxBytesPerRead = Int16.MaxValue; 
basicHttpBinding.MaxReceivedMessageSize = int.MaxValue; 
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 
basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; 

ChannelFactory<Service> channelFactory = null; 
Service client = null;    

channelFactory = new ChannelFactory<Service>(basicHttpBinding, endpointAddress); 
channelFactory.Credentials.UserName.UserName = ConfigurationManager.AppSettings["wcfUser"].ToString(); 
channelFactory.Credentials.UserName.Password = ConfigurationManager.AppSettings["wcfPW"].ToString(); 
try 
{ 
    client = channelFactory.CreateChannel(); 
    string a = client.SendMail(); 
} 
catch(Exception e) 
{ 
    Response.Write(e.Message); 
} 

在IIS基本身份驗證被激活。我真的不想切換到HTTPS和證書,因爲我只需要這個basicauthent來實現內部安全。我甚至無法使用visual studio wcf testclient啓動WCF,但是沒有身份驗證設置,一切正常。 WCF和ASP都發布在我的本地IIS上。

有什麼建議是什麼問題?我是否必須使用相同憑據將用戶添加到本地系統?

** 編輯 **

我想我知道的問題!在我的WCF中,我打電話給另一個WCF(來自客戶),並且這個與我的安全設置相結合正在製造麻煩。那麼我如何解決這個問題?我的WCF服務器端配置和客戶端WCF客戶端配置在一個web.config? (客戶WCF也通過ChannelFactory調用)因爲如果我命名綁定配置它不會改變一件事(見上面的代碼)。

通過ChannelFactory進行客戶WCF調用的代碼看起來像上面的代碼。 這可能是問題嗎? 2 WCF - 一個服務器端和一個呼叫?

+0

不要在沒有https的情況下使用BASIC。使用嗅探器將密碼從電線上拉出是微不足道的。不要認爲內部意味着「安全」。 – Crowcoder

+0

@Crowcoder - 我真的不在乎外部人能否看到一些東西。沒有親密的轉移正在進行。只是爲了確保每位員工的內部安全,只要他們錯誤地擊中了WCF的網站。 –

+0

@DaveStockinger您是否嘗試在客戶端添加領域到證書? – mkysoft

回答

0

一切都很好我BasicAuthentification。

錯誤是我通過我的WCF調用的另一個WCF,並且因爲我的WCF中的安全設置,我無法調試(而不是掛起來處理或測試客戶端),並且因爲錯誤消息中沒有詳細信息我沒有找到這個錯誤的來源。這個其他WCF拋出錯誤與「基本」是不允許的,所以我的客戶做了他的IIS(也許)的一些變化。

而我不得不添加一個本地用戶使用相同的憑據,因爲我在我的WCF中使用。不知道,但耶 - 一切都很好,我必須等待客戶告訴我有關他們的設置。

0

我創建測試項目,它正在工作。代碼如下:

IService1接口:

namespace WcfTestService 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. 
    [ServiceContract] 
    public interface IService1 
    { 

     [OperationContract] 
     string GetData(int value); 

     [OperationContract] 
     CompositeType GetDataUsingDataContract(CompositeType composite); 

     // TODO: Add your service operations here 
    } 


    // Use a data contract as illustrated in the sample below to add composite types to service operations. 
    [DataContract] 
    public class CompositeType 
    { 
     bool boolValue = true; 
     string stringValue = "Hello "; 

     [DataMember] 
     public bool BoolValue 
     { 
      get { return boolValue; } 
      set { boolValue = value; } 
     } 

     [DataMember] 
     public string StringValue 
     { 
      get { return stringValue; } 
      set { stringValue = value; } 
     } 
    } 
} 

Service1類:

namespace WcfTestService 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. 
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging. 
    public class Service1 : IService1 
    { 
     public string GetData(int value) 
     { 
      return string.Format("You entered: {0}", value); 
     } 

     public CompositeType GetDataUsingDataContract(CompositeType composite) 
     { 
      if (composite == null) 
      { 
       throw new ArgumentNullException("composite"); 
      } 
      if (composite.BoolValue) 
      { 
       composite.StringValue += "Suffix"; 
      } 
      return composite; 
     } 
    } 
} 

web.config文件:

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="WcfTestService.Service1" behaviorConfiguration="HttpBehavior"> 
     <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding" contract="WcfTestService.IService1" /> 

     </service> 

    </services> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="BasicHttpBinding" > 
      <readerQuotas /> 
      <security mode="TransportCredentialOnly"> 

      <transport clientCredentialType="Basic" realm="" /> 
      </security> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="HttpBehavior"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 

    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 

</configuration> 

啓用IIS基本身份驗證,禁用匿名。

WCF客戶端控制檯應用程序:

namespace WcfTestClient 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      EndpointAddress endpointAddress = new EndpointAddress(@"http://localhost/Service1.svc"); 

      BasicHttpBinding basicHttpBinding = new BasicHttpBinding(); 
      basicHttpBinding.MaxReceivedMessageSize = int.MaxValue; 
      basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 
      basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; 

      var channelFactory = new ChannelFactory<WcfTestService.IService1>(basicHttpBinding, endpointAddress); 
      channelFactory.Credentials.UserName.UserName = @"server\someuser"; 
      channelFactory.Credentials.UserName.Password = @"somepass"; 
      try 
      { 
       var client = channelFactory.CreateChannel(); 
       string a = client.GetData(55); 
       Console.Write(e.Message); 
      } 
      catch (Exception e) 
      { 
       Console.Write(e.Message); 
      } 
     } 
    } 
} 

下載:http://sharesend.com/6gutdu4s

+0

如果我嘗試通過瀏覽器訪問我的wcf,一切正常。我被要求提供憑據,當我登錄時看到服務,但是當我嘗試以編程方式訪問它時,當涉及到'client.GetData()'時,我仍然得到相同的錯誤。 –

+0

你可以試用框架4.0的項目嗎? – mkysoft

+0

我想我知道這個問題!在我的WCF中,我打電話給另一個WCF(來自客戶),這個與我的安全設置結合在一起造成麻煩。那麼我如何解決這個問題?我的WCF服務器端配置和客戶端WCF客戶端配置在一個web.config? (客戶WCF也通過ChannelFactory調用) –