WCF身份验证问题

发布于 2024-10-11 04:24:23 字数 2923 浏览 3 评论 0原文

我在 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 或指定的 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 的分配只是为了在这个地方获取此异常。

I have a big problem with custom user name authentication in WCF. I've made a simple WCF Service which needs users validation via CustomUserValidator. When I log-in from Console Application like this:

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();
}

Everything is fine, but when I want to use my GUI Client which essentialy do the same I've got an error:
Outer Exception:

Secure channel cannot be opened because security negotiation with the remote endpoint has failed. This may be due to absent or incorrectly specified EndpointIdentity in the EndpointAddress used to create the channel. Please verify the EndpointIdentity specified or implied by the EndpointAddress correctly identifies the remote endpoint.

Inner Exception:

The request for security token has invalid or malformed elements.

The app.config entry for both of them is identical:

<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>

Thanks for all your help!

Edit: posting GUI code in which I catch an exception:

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

This assigment to int is just to get this exception in this place.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

触ぅ动初心 2024-10-18 04:24:23

尝试右键单击“以管理员身份运行”来运行 Gui 应用程序。还要确保您正在修剪 view.userName 字段。尾随空格可能会产生问题。

Try running Gui app with right click Run As Admin. Also make sure that you are trimming the view.userName fields. The trailing whitespace can create problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文