【问题标题】:Web Service - Windows AuthenticationWeb 服务 - Windows 身份验证
【发布时间】:2012-04-18 09:30:48
【问题描述】:

我已将 Web 服务添加到现有的 asp.net 内部网应用程序。目的是向同一域上的其他 Intranet 应用程序公开功能。

Intranet 应用程序使用 Windows 身份验证。如何设置 Web 服务以使用 Windows 身份验证?

【问题讨论】:

  • 另一种看待它的方式 - 如果您使用的是静态 IP,请保留一张表格,说明哪些用户在哪个 IP 上。
  • 有趣的是,这样的问题仍然存在。如果您已经自行解决了,请发布答案并接受它,或者干脆删除此问题。

标签: asp.net web-services


【解决方案1】:
Client.localhost.Service1 service = new Client.localhost.Service1();
   service.Credentials = new System.Net.NetworkCredential("username", "pass", "");

【讨论】:

    【解决方案2】:

    设置 Web 服务以使用 Windows 身份验证很容易。你只需在 IIS 中更改身份验证模式!

    与该服务通信是另一回事。首先,您需要在使用应用程序的 Web 配置中正确设置服务引用。下面的安全部分是让它发挥作用的最关键部分。

    <system.serviceModel>
    <bindings>
    <basicHttpBinding>
        <binding name="ServiceSoap" 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="Transport">
            <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://yourservice.com/Service.asmx"
        binding="basicHttpBinding" bindingConfiguration="ServiceSoap"
        contract="ServiceClient.IServiceSoap" name="ServiceSoap" />
    </client>
    

    然后,您需要在开始使用之前设置客户端对象的 Windows 凭据。

    var credentials = ServiceSoapClient.ClientCredentials;
    credentials.Windows.ClientCredential.Domain = "domain";
    credentials.Windows.ClientCredential.UserName = "user";
    credentials.Windows.ClientCredential.Password = "pwd";
    credentials.Windows.AllowNtlm = true;
    

    【讨论】:

    • 您是否认为使用 Web 服务公开 ASP.NET 应用程序的一小部分是不好的做法?它是一个内部应用程序,将链接到另一个内部应用程序。
    • 我不认为创建一个小型 Web 服务来允许两个应用程序通信是一种不好的做法,但我欢迎任何其他意见。
    • 谢谢。您在答案中引用了 web.config 中的代码。如果您为 Web 服务创建服务引用,此代码是否会自动生成?
    • 通常,web config 中的代码至少是部分配置的。但是,根据我的经验,它并不总是针对 Windows Auth 进行正确设置。这就是我发布它的原因。您可能需要更正安全部分。
    猜你喜欢
    • 2016-06-10
    • 2012-02-28
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多