WCF REST client windows authentication

2019-03-04 18:19发布

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>

0条回答
登录 后发表回答