Я придерживаюсь приведенного ниже примера, но получаю ошибку Unauthorized 401 на стороне сервера.
https://www.dotnetcurry.com/ShowArticle.aspx?ID=434
Я попробовал ниже Служба REST WCF с аутентификацией Windows и SSL
Проблема аутентификации Windows WCF с сервисом REST
Это близко к моему
как использовать службу отдыха wcf с аутентификацией Windows
У меня есть интерфейс IService, как показано ниже
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "/Students/getStudents",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
void GetAllStudents();
В Windows Service 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>
IЯ делаю вызов от клиента, как показано ниже
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>