2012-06-01 47 views
0

我認爲我的配置有問題。我試圖獲得一個包含在IIS中的密碼保護的安全性(尚未實現),並且我收到各種異常。這一次我得到一個安全標記無效或元素異常。這裏是我的CONFIGS:安全WCF配置無法正常工作:拋出安全令牌請求包含無效或格式錯誤的元素

主持人:

<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
    <system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
     <binding name ="WSHttpBinding_ISVC1"> 
     <security mode="Message"> 
      <message clientCredentialType="UserName" establishSecurityContext="false"/> 
     </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="SVCBehaviors"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
      <serviceCredentials> 
      <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="blahblah.SVCValidator, TrademarkGlobal.Web"/> 
      </serviceCredentials> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <services> 
     <service name="blahblah.SVC" 
     behaviorConfiguration="SVCBehaviors"> 
     <endpoint address="" binding="wsHttpBinding" 
    contract="blahblah.ISVC" name="WSHttpBinding_ISVC1"/> 
     <endpoint contract="IMetadataExchange" 
      binding="mexHttpBinding" address="mex"/> 
     </service> 
    </services> 
    </system.serviceModel> 
</configuration> 

CLIENT: 
    <system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="WSHttpBinding_ISVC1"> 
      <security mode="Message"> 
      <message clientCredentialType="UserName" establishSecurityContext="false"/> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost:64609/SVC.svc" binding="wsHttpBinding" 
      bindingConfiguration="WSHttpBinding_ISVC1" contract="ISVC"> 
     </endpoint> 
    </client> 
    </system.serviceModel> 

SVC文件(svc.svc):

<%@ServiceHost Service="blahblah.SVC" %> 

的服務(SVC & ISVC)只是返回一個基本的〜應變g下了。

SVCVALIDATOR.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ServiceModel; 
using System.IdentityModel.Selectors; 
public class SVCValidator : UserNamePasswordValidator 
{ 
    public override void Validate(string userName, string password) 
    { 
     if (userName == null || password == null) 
     { 
      throw new ArgumentNullException(); 
     } 
     if (userName != "gooduser" && password != "goodpass") 
     { 
      throw new Exception("Incorrect Username or Password"); 
     } 
    } 
} 

控制器操作用於運行:

public class SVCController : Controller 
{ 
    public ActionResult Index(string user = "bad", string pass = "bad") 
    { 
     using (SVCClient client = new SVCClient()) 
     { 
     //SVCClient client = new SVCClient("WSHttpBinding_ISVC"); 
     client.ClientCredentials.UserName.UserName = user; 
     client.ClientCredentials.UserName.Password = pass; 
     ViewBag.Message = client.Test(); 
     return View("RunningSVC"); 
     } 
    } 
} 
+0

我認爲這個錯誤基本上意味着服務器無法編譯。要查看真實的錯誤消息,請轉到服務* blahblah.SVC *的URL。你在那看到什麼? – McGarnagle

回答

3

你不會在服務器上使用綁定配置。

你的配置有一個名字:

<bindings> 
     <wsHttpBinding> 
     <binding name ="WSHttpBinding_ISVC1"> 
     <security mode="Message"> 
      <message clientCredentialType="UserName" establishSecurityContext="false"/> 
     </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 

但端點並非指的是配置名稱。 正確的終點應該是:

<endpoint address="" binding="wsHttpBinding" 
    contract="blahblah.ISVC" name="WSHttpBinding_ISVC1" bindingConfiguration="WSHttpBinding_ISVC1" /> 

請讓我知道這是否解決了問題。

+1

新的異常:{「所請求的服務'http:// localhost:64609/SVC.svc'無法激活。請參閱服務器的診斷跟蹤日誌以獲取更多信息。」}。我認爲這是一個進步。 未來規定:沒有提供服務證書。在ServiceCredentials中指定一個服務證書。我不想要證書。 – tehdoommarine

+1

您必須使用證書來保護您的密碼。它應該是消息級別(您的配置)或傳輸 - http://msdn.microsoft.com/en-us/library/ms733775.aspx –

+1

另請參閱此 - http://blogs.msdn.com/b/pedram /archive/2007/10/05/wcf-authentication-custom-username-and-password-validator.aspx它包含的工作代碼 –

相關問題