HTTP-запрос не авторизован с помощью схемы аутентификации клиента «Anonymous» - PullRequest
0 голосов
/ 15 апреля 2020

Я разрабатываю клиент для использования веб-службы. Конечной точкой является HTTPS. Я получаю это исключение при попытке войти в систему. Почему?

Это метод

BasicHttpsBinding binding = new BasicHttpsBinding();
binding.Security.Mode = BasicHttpsSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;

var ea = new EndpointAddress(new Uri($@"https://endpoint"));

WSPDDClient client = new WSPDDClient(binding, ea);

client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "password";

client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "certificatename");

WSPDD.login login1 = new WSPDD.login()
{
     login1="username",
     password="password"
 };
 try
 {
     client.Open();

     WSPDD.loginResponse resLogin = client.login(login1);

     if (resLogin.@return.success)
     {

     }
     else
     {
        Debug.WriteLine("Err {0}", resLogin.@return.error);
     }
  }
  catch(Exception ex)
  {
       Debug.WriteLine("Err {0}", ex);
  }

Это web.config

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="WSPDDBinding" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="https://endpoint"
    binding="basicHttpBinding" bindingConfiguration="WSPDDBinding"
    contract="WSPDD" name="WSPDDPort" />
</client>

исключение:

HTTP-запрос не авторизован в схеме аутентификации клиента «Anonymous». Заголовок аутентификации, полученный от сервера: 'Mutual SSL realm = \ "WSO2 API Manager \", error = \ "неверный токен \", error_description = \ "Срок действия маркера истек \"'. "

Исключение составляет

WSPDD.loginResponse resLogin = client.login(login1);

Ответы [ 2 ]

0 голосов
/ 16 апреля 2020

Спасибо за ответ. Я сделал, как вы предложили, Авраам.

WSPDD.SimogWSPDDClient client = new WSPDD.SimogWSPDDClient();

WSPDD.login login1 = new WSPDD.login
{
     login1 = "username",
     password = "passowrd"
 };
WSPDD.loginResponse loginResponse = new WSPDD.loginResponse();

loginResponse = client.login(login1);
if(loginResponse.@return.success)
{

}
else
{
    Console.WriteLine("Err {0}", loginResponse.@return.error);
 }

end Web.config

<system.serviceModel>
<behaviors>  
  <endpointBehaviors>  
    <behavior name="endpointCredentialBehavior">  
      <clientCredentials>  
        <clientCertificate findValue="+++certificatename+++"  
                           storeLocation="LocalMachine"  
                           storeName="My"  
                           x509FindType="FindBySubjectName" />  
      </clientCredentials>  
    </behavior>  
  </endpointBehaviors>  
</behaviors>  
<bindings>
  <wsHttpBinding>
    <!-- configure wsHttpbinding with Transport security mode  
               and clientCredentialType as Certificate -->
    <binding name="SimogWSPDDBinding">
      <security mode="Transport">
        <transport clientCredentialType="Certificate"/>
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
<client>
  <endpoint address="https://*****/****/1.0.0"
    binding="wsHttpBinding" bindingConfiguration="SimogWSPDDBinding"
    contract="WSPDD.SimogWSPDD" name="SimogWSPDDPort" behaviorConfiguration="endpointCredentialBehavior"  />
</client>

Все ошибки в одной строке кода (loginResponse = client.login(login1);):

System.ServiceModel.Security.MessageSecurityException
  HResult=0x80131501
  Messaggio=La richiesta HTTP non è autorizzata con lo schema di autenticazione client 'Anonymous'. Intestazione di autenticazione ricevuta dal server: 'Mutual SSL realm="WSO2 API Manager", error="invalid token", error_description="The access token expired"'.
  Origine=mscorlib
  Analisi dello stack:
   in System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   in System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   in ANAC_WS_3.WSPDD.SimogWSPDD.login(loginRequest request)
   in ANAC_WS_3.WSPDD.SimogWSPDDClient.ANAC_WS_3.WSPDD.SimogWSPDD.login(loginRequest request) in C:\Users\Francesco\source\repos\ANAC_WS_3\ANAC_WS_3\Connected Services\WSPDD\Reference.cs: riga 14534
   in ANAC_WS_3.WSPDD.SimogWSPDDClient.login(login login1) in C:\Users\Francesco\source\repos\ANAC_WS_3\ANAC_WS_3\Connected Services\WSPDD\Reference.cs: riga 14540
   in ANAC_WS_3.Controllers.HomeController.Index() in C:\Users\Francesco\source\repos\ANAC_WS_3\ANAC_WS_3\Controllers\HomeController.cs: riga 25
   in System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   in System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   in System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   in System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   in System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   in System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   in System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   in System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0()
   in System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()

Eccezione interna 1:
WebException: Errore del server remoto: (401) Non autorizzato.
0 голосов
/ 16 апреля 2020

Все эти фрагменты кода расположены на стороне клиента, и некоторые настройки дублируются, например конфигурация привязки Basichttp. Параметры конфигурации в фрагментах кода не соответствуют параметрам в Webconfig.
. Обычный способ вызова службы WCF - это создание прокси-сервера клиента путем добавления ссылки на службу, что также приводит к настройкам привязки, которые соответствуют на стороне сервера в файле конфигурации, расположенном на стороне клиента.
Из ошибок, произошедших на стороне клиента, конфигурация привязки в фрагментах кода должна быть правильной и соответствовать стороне сервера.

BasicHttpsBinding binding = new BasicHttpsBinding();
binding.Security.Mode = BasicHttpsSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

Другими словами, сервер аутентифицирует клиента с помощью сертификата, клиент должен предоставить сертификат клиента при вызове удаленной службы. во время этого процесса мы должны установить sh доверительные отношения между серверной и клиентской сторонами.
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/transport-security-with-certificate-authentication
Не стесняйтесь сообщать мне, есть ли что-то, что я может помочь с.

...