WCF身份验证问题

时间:2011-01-10 20:56:12

标签: .net wcf wcf-security

我在WCF中使用自定义用户名身份验证存在很大问题。我做了一个简单的WCF服务,需要通过CustomUserValidator进行用户验证。当我从控制台应用程序登录时,如下所示:

using (var svc = new ServiceReference2.DBServiceClient())
{
    Console.WriteLine("Username: ");
    string user = Console.ReadLine();
    Console.WriteLine("Pass: ");
    string pass = Console.ReadLine();
    svc.ClientCredentials.UserName.UserName=user;
    svc.ClientCredentials.UserName.Password=pass;
    Console.WriteLine(svc.GetAllDepartmentList().First().DepartmentID);
    Console.Read();
}

一切都很好,但是当我想使用我的GUI客户端时,我必须做同样的错误: 外部例外:

  

由于与远程端点的安全协商失败,无法打开安全通道。这可能是由于在用于创建通道的EndpointAddress中缺少或错误指定了EndpointIdentity。请验证EndpointAddress指定或暗示的EndpointIdentity是否正确标识了远程端点。

内部例外:

  

安全令牌请求包含无效或格式错误的元素。

两者的app.config条目完全相同:

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="WSHttpBinding_IDBService" 
        closeTimeout="00:01:00" openTimeout="00:01:00" 
        receiveTimeout="00:10:00" sendTimeout="00:01:00"
        bypassProxyOnLocal="false" transactionFlow="false"
        hostNameComparisonMode="StrongWildcard"
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
        allowCookies="false">
        <readerQuotas maxDepth="32" maxStringContentLength="8192"
          maxArrayLength="16384" maxBytesPerRead="4096"
          maxNameTableCharCount="16384" />
        <reliableSession ordered="true" 
          inactivityTimeout="00:10:00" enabled="false" />
        <security mode="Message">
          <transport clientCredentialType="Windows" 
            proxyCredentialType="None" realm="" />
          <message clientCredentialType="UserName" 
            negotiateServiceCredential="true" algorithmSuite="Default" />
        </security>
      </binding>
    </wsHttpBinding>
  </bindings>
  <client>
    <endpoint name="WSHttpBinding_IDBService"
      address="http://localhost:8732/Design_Time_Addresses/DBService/Service1/"
      binding="wsHttpBinding" 
      bindingConfiguration="WSHttpBinding_IDBService"
      contract="ServiceReference2.IDBService" >
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
  </client>
</system.serviceModel>

感谢您的帮助!

编辑:发布我捕获异常的GUI代码:

            using (var svc = new DBServiceHost.DBServiceClient())
        {
            svc.ClientCredentials.UserName.UserName = view.userName;
            svc.ClientCredentials.UserName.Password = view.userPass;
            int asd = svc.GetCompanies().First().CompanyID;                
        }

这个对int的赋值只是为了在这个地方得到这个例外。

1 个答案:

答案 0 :(得分:0)

右键单击Run As Admin尝试运行Gui应用程序。还要确保修剪view.userName字段。尾随空格可能会产生问题。