【问题标题】:How do I add windows authentification to my web service如何将 Windows 身份验证添加到我的 Web 服务
【发布时间】:2015-12-08 19:14:29
【问题描述】:

我有 WCF 服务,我的目标是能够使用 GET 和 POST 从我的服务器检索信息。我几乎让它工作了。我的问题是,为了能够访问服务器上的信息,我需要输入我的凭据,否则我会收到错误 401 未授权

我尝试了一些不同的方法,但我在使用 XML 方面还是新手,当我尝试添加一些代码时,我总是会出错。

这是我的代码:

C#

[ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "xml/{id}")]
        string XMLData(string id);

        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/{id}")]
        string JSONData(string id);
    }

XML

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
        </endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

我仍然不知道如何解决这个问题。

【问题讨论】:

  • @MurrayFoxcroft 谢谢你的回答。你能帮我让 XML 工作吗?如果我复制它,我会遇到 30 多个错误。需要结束标记,缺少属性名称,标记未关闭。
  • xml配置中缺少som空格。
  • 我让它运行但我得到一个错误。查看更新的问题

标签: c# .net xml wcf rest


【解决方案1】:

试试这个(下面的代码下载链接):

// IRestService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace RestService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "xml/{id}")]
        string XMLData(string id);

        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/{id}")]
        string JSONData(string id);
    }
}

// RestService.cs
namespace RestService
{
    public class RestServiceImpl : IRestServiceImpl
    {
        public string JSONData(string id)
        {
            return id;
        }

        public string XMLData(string id)
        {
            return id;
        }
    }
}


<!-- RestService.svc (Source) -->
<%@ ServiceHost Language="C#" Debug="true" Service="RestService.RestServiceImpl" CodeBehind="RestService.svc.cs"
    Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

<!--  Web.config -->
<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding"  bindingConfiguration="webBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
        </endpoint>
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="webBinding">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" proxyCredentialType="Windows">
            </transport>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceCredentials>
            <windowsAuthentication allowAnonymousLogons="false" includeWindowsGroups="true" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

以下结果(您的基本 URL 可能会有所不同)。忽略您看到的“找不到端点”消息。休息服务不应该暴露元数据(除非你认为 HATEOAS 是元数据 :)

http://localhost:62001/RestService.svc/xml/absee

<XMLDataResponse xmlns="http://tempuri.org/">
<XMLDataResult>absee</XMLDataResult>
</XMLDataResponse>

http://localhost:62001/RestService.svc/json/absee

{"JSONDataResult":"absee"}

Download the code here

您可能希望在 Web 选项卡下的项目设置中将其设置为在 IIS Express 下运行。

【讨论】:

  • 感谢您的帮助!我不明白最后一部分。这是什么:&lt;XMLDataResponse xmlns="http://tempuri.org/"&gt; &lt;XMLDataResult&gt;absee&lt;/XMLDataResult&gt; &lt;/XMLDataResponse&gt;?我尝试复制代码,但似乎仍然收到错误 401。我无法下载您的项目,因为我使用的是 Visual Studio 2010
  • 您好 Phil,XML 是您提供的代码的示例输出。请参阅“这是我的代码”下的原始帖子。就代码而言,它应该可以在 2010 年使用,并进行一些反向移植。尝试创建一个新的空白 WCF 服务和同名解决方案并将代码复制进去。
  • 对于 401,这表明身份验证正在工作。 Http 401:Web 服务器(运行网站)认为客户端(例如您的 Web 浏览器或我们的 CheckUpDown 机器人)发送的 HTTP 数据流是正确的,但访问 URL 资源需要用户身份验证。如果您在 IIS 下运行,请确保连接服务具有正确的凭据。使用 chrome 调试工具或提琴手telerik.com/fiddler
  • 是否可以要求提供凭据然后启动 Web 服务并使用这些凭据?
  • 您可能希望在调用 Web 服务之前设置凭据,而不是像网站那样进行质询-响应。这就是我认为你所追求的:stackoverflow.com/questions/15434779/…
猜你喜欢
  • 2012-04-18
  • 1970-01-01
  • 2016-06-10
  • 1970-01-01
  • 2012-02-28
  • 1970-01-01
  • 1970-01-01
  • 2016-07-17
  • 1970-01-01
相关资源
最近更新 更多