【问题标题】:How to call a web service using stored credentials?如何使用存储的凭据调用 Web 服务?
【发布时间】:2009-06-15 01:35:25
【问题描述】:

我需要调用在不同于(Windows 窗体)客户端运行的 Windows 域中运行的 Web 服务。Web 服务使用 Windows 身份验证进行保护。

Web 服务域的域凭据保存在客户端的用户配置文件中(XP 中保存的用户名和密码),但我无法弄清楚在调用 Web 服务时如何使用这些保存的凭据。我发现了很多使用

的例子
WebService1.Credentials = System.Net.CredentialCache.DefaultCredentials 

(这不起作用,因为它是本地域的凭据)

WebService1.Credentials = new NetworkCredentials(username, pwd, domain) 

(用户名、密码、域是硬编码的)。

我已阅读使用 Windows API 使用 CredEnumerateCredRead,但不知道如何(或是否)我可以将 PCREDENTIAL 转换为托管 NetworkCredentialReadCred 赢了'不返回存储域凭据的密码)

这里有人知道怎么做吗?

谢谢!

【问题讨论】:

    标签: .net web-services security authentication


    【解决方案1】:

    我不相信你可以直接使用它们。我相信您必须提示用户,然后一旦用户提供它们,您就可以再次提示。 Here is an article on how to do that。毕竟,如果您只能从 DPAPI 中获取凭据,那将无法达到目的。 :)

    这里有一些信息。万一有帮助...

    在 DPAPI 中保存数据的管理器称为Key Manager。您可以通过执行 start->run ...

    从 UI 访问密钥管理器
    rundll32.exe keymgr.dll, KRShowKeyMgr
    

    一般来说,存储认证函数的地方的API是ADVAPI32。你也许能在里面找到一些东西来帮助你。这是该 API 上的decent article

    当您将凭据添加到 CredentialCache 时,您还可以尝试将 authType "Negotiate" 设置为 described here

    对不起,这只是一个研究转储 - 我没有找到答案,但希望其中一些可能对您有所帮助。总账。

    【讨论】:

      【解决方案2】:

      好的,我找到了解决方案,所以我将回答我自己的问题:

      首先,我发现通过使用新的 WCF 样式代理类“服务引用”,您可以在 app.config 文件中设置安全设置。关键(在我的场景中)是仅设置传输凭据的模式,将 Windows 指定为类型,并在领域属性中标识域:

      <system.serviceModel>
              <bindings>
                  <basicHttpBinding>
                      <binding name="Service1Soap" closeTimeout="00:01:00" openTimeout="00:01:00"
                          receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                          bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                          maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                          useDefaultWebProxy="true">
                          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                          <security mode="TransportCredentialOnly">
                              <transport clientCredentialType="Windows" realm="mydomain.com" />
                          </security>
                      </binding>
                  </basicHttpBinding>
              </bindings>
              <client>
                  <endpoint address="http://server.mydomain.com/HelloWorldSvc/Service1.asmx"
                      binding="basicHttpBinding" bindingConfiguration="Service1Soap"
                      contract="ServiceReference1.Service1Soap" name="Service1Soap" />
              </client>
          </system.serviceModel>
      

      接下来,我使用 Hernan de Lahitte's 出色的 wrapper 作为 CredUIPromptForCredentials API 函数,以在引发安全异常时提示用户输入凭据并将其存储在他们的配置文件中:

      ServiceReference1.Service1SoapClient c = new Service1SoapClient();
      retry:
      try
      {
          MessageBox.Show(this, c.HelloWorld());
      }
      catch (System.ServiceModel.Security.MessageSecurityException securityException)
      {
          UserCredentialsDialog creds = new UserCredentialsDialog("*.mydomain.com", "My App",
                                                                              "Enter your enterprise credentials.  Enter your user name as \"MyDomain\\username\"");
          creds.Flags = UserCredentialsDialogFlags.Persist;
          if (creds.ShowDialog() == DialogResult.OK)
          {
              goto retry;
          }
      }
      
      c.Close();
      

      是的,这是一个“goto”。 ****喘气**** :)

      【讨论】:

        【解决方案3】:

        我认为你不能在代码中使用它们,afaik 你唯一的选择是:

        1. 硬编码您所写的用户名/密码(在大多数情况下是一个很大的禁忌)。
        2. 在域和计算机之间设置具有信任的 Kerberos,以便用户域帐户可以调用该服务。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-09
          • 1970-01-01
          • 2019-12-06
          相关资源
          最近更新 更多