简体   繁体   English

如何将Windows身份验证添加到我的Web服务

[英]How do I add windows authentification to my web service

I have WCF Service, my goal is to be able to retrieve information from my server using GET and POST. 我有WCF服务,我的目标是能够使用GET和POST从服务器检索信息。 I almost got it to work. 我差一点就可以工作了。 My problem is that to be able to access the information on the server I need to enter my credentials, otherwise I get the error 401 unauthorized 我的问题是,要访问服务器上的信息,我需要输入我的凭据,否则我将得到未授权的错误401

I tried a few different things but I'm still new at using XML and when I try to add some code I always get errors. 我尝试了几种不同的方法,但是在使用XML方面我还是一个新手,当我尝试添加一些代码时,总是会出错。

This is my code: 这是我的代码:

C# 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格式

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

I still don't know how to fix this. 我仍然不知道该如何解决。

Try this (link to code download below listing): 试试这个(链接到下面列表中的代码下载):

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

Results below (your base URL will likely differ). 以下结果(您的基本URL可能会有所不同)。 Ignore the "Endpoint not found" message you see. 忽略您看到的“找不到端点”消息。 Rest services should not expose metadata (unless HATEOAS is considered metadata in your mind :) 休息服务不应公开元数据(除非您认为HATEOAS是元数据:)

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

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

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

{"JSONDataResult":"absee"}

Download the code here 在此处下载代码

You may want to set it to run under IIS Express in the project settings under the Web tab. 您可能希望将其设置为在“ Web”选项卡下的项目设置中的IIS Express下运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM