WCF REST客户端Windows身份验证

时间:2019-02-05 19:58:39

标签: rest wcf client windows-authentication

我遵循以下示例,但服务器端出现未经授权的401错误。

https://www.dotnetcurry.com/ShowArticle.aspx?ID=434

我在下面尝试过 WCF REST Service with Windows Authentication and SSL

WCF Windows authentication issue with REST service

这很接近我

how to consume wcf rest service with windows authentication

我有如下所示的IService界面

[OperationContract]
[WebInvoke(
    Method = "POST",
    UriTemplate = "/Students/getStudents",
    BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json)]
 void GetAllStudents();

在Windows服务app.config中,它像我下面的WCF Rest服务的主机一样

<system.serviceModel>
<bindings>
<webHttpBinding>
   <binding name="webHttpBindingConfiguration" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                  maxArrayLength="2147483647" maxBytesPerRead="2147483647"
                  maxNameTableCharCount="2147483647" />
       <security mode="TransportCredentialOnly">
          <transport clientCredentialType="Windows" proxyCredentialType="Windows" />
        </security>
    </binding>
</webHttpBinding>
</bindings>
<behaviors>
      <serviceBehaviors>
         <behavior name="WCFRESTServiceBehavior">         
          <serviceMetadata httpGetEnabled="True" httpGetUrl="http://localhost:2023/MyRESTService"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
          <serviceCredentials>
            <windowsAuthentication allowAnonymousLogons="false" includeWindowsGroups="true"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>         
       <endpointBehaviors>
        <behavior name="webBehaviorConfiguration">
          <webHttp />
        </behavior>
      </endpointBehaviors>        
</behaviors>   
<services>
<service behaviorConfiguration="WCFRESTServiceBehavior"
        name="MYRESTService">
        <endpoint address="http://localhost:2023/MyRESTService"
          behaviorConfiguration="webBehaviorConfiguration" binding="webHttpBinding"
          bindingConfiguration="webHttpBindingConfiguration" contract="IMyRESTService" />
      </service>
</services>
</system.serviceModel>

我正在像下面这样打客户电话

url = "http://localhost:2023/MyRESTService/Students/getStudents";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
request.PreAuthenticate = true;
NetworkCredential credential = new NetworkCredential(@"domainname\username", "password");
request.Credentials = credential;

WebResponse response = request.GetResponse();

根据请求获取未经授权的401错误。GetResponse()

更新:如果我能像下面这样提供信息,则它可以正常工作,但是我肯定这是错误的,因为身份验证将不会出现。我测试了错误的用户名,但仍然可以使用,但是我期待未授权错误。

<system.web>
    <authentication mode="None"/>
  </system.web>

<webHttpBinding>
        <binding name="webHttpBindingConfiguration" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                        maxArrayLength="2147483647" maxBytesPerRead="2147483647"
                        maxNameTableCharCount="2147483647" />
          <security mode="None">
            <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>

0 个答案:

没有答案