【问题标题】:WCF Security exception when calling from Windows Service从 Windows 服务调用时出现 WCF 安全异常
【发布时间】:2011-11-03 08:00:19
【问题描述】:

我有一些使用 WCF 服务的代码。该服务受基本身份验证保护,因此在创建客户端时,我使用以下代码:

    BasicHttpBinding httpBinding = new BasicHttpBinding();
    httpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
    httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
    httpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
    httpBinding.Security.Transport.Realm = service_realm;

    EndpointAddress address = new EndpointAddress(service_address);

    Service.ServiceClient client = new Service.ServiceClient(httpBinding, address);

    client.ClientCredentials.UserName.UserName = service_username;
    client.ClientCredentials.UserName.Password = service_password;

当我从控制台应用程序运行代码时工作正常。但是当我从 Windows 服务运行相同的代码时,会抛出 MessageSecurityException 告诉我我的请求未经授权。出于某种原因,它似乎正在使用当前的 Windows 帐户进行身份验证,因为我自己的帐户确实可以访问该服务。但我不希望它,我希望它使用存储的凭据。我在这里想念什么?

【问题讨论】:

    标签: wcf windows-services


    【解决方案1】:

    WCF basicHttpBinding 不支持纯文本 凭据;原因是当您希望在传输绑定上传递凭据时,WCF 要求底层传输是 安全 传输,例如 SSL。

    为了让您的代码正常工作,您需要通过https 或使用证书或加密来使用服务。

    【讨论】:

    • 可能是这样,但是当我有一个正常的 app.config 时,这曾经可以工作。改变的是我现在以编程方式创建绑定,这似乎是导致问题的原因。我认为您的说法适用于 wsHttpBinding,但不适用于 basicHttpBinding。
    【解决方案2】:

    似乎使用此配置修复:

    _httpBinding = new BasicHttpBinding();

    _httpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; _httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; _httpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;

    _httpBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName; _httpBinding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;

    _httpBinding.AllowCookies = false; _httpBinding.BypassProxyOnLocal = false; _httpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; _httpBinding.MessageEncoding = WSMessageEncoding.Text; _httpBinding.TextEncoding = 编码.UTF8; _httpBinding.TransferMode = TransferMode.Buffered; _httpBinding.UseDefaultWebProxy = false; Service.ServiceClient 客户端 = new Service.ServiceClient(_httpBinding, _address);

    client.ClientCredentials.UserName.UserName = service_username; client.ClientCredentials.UserName.Password = service_password;

    【讨论】:

      猜你喜欢
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2011-04-01
      • 2013-07-09
      • 2017-09-01
      • 2019-12-10
      • 1970-01-01
      相关资源
      最近更新 更多