2012-06-01 42 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它包含的工作代码 –

相关问题