WCF REST client windows authentication

↘锁芯ラ 提交于 2019-12-17 17:18:43

问题


I am following below example but getting Unauthorized 401 error on server side.

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

I tried below WCF REST Service with Windows Authentication and SSL

WCF Windows authentication issue with REST service

This is close to mine

how to consume wcf rest service with windows authentication

I have IService interface like below

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

In Windows Service app.config which is acting like host for my WCF Rest service I have below

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

I am making call from client like below

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();

Getting UnAuthorized 401 error at request.GetResponse()

UPDATE: If I give like below it is working but I am sure it is wrong since authentication will not come into picture. I tested with wrong username and still it works but I was expecting unauthorize error.

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

来源:https://stackoverflow.com/questions/54542109/wcf-rest-client-windows-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!