Friday, February 16, 2018

SOAP - HTTP with C# Basic Realm

private RrewardServiceClient GetLocalM2MSoapClient()
    {

        string endpointFromConfig = ConfigurationManager.AppSettings["RewardM2MEndpointAdress"];
        string username = ConfigurationManager.AppSettings["RewardServiceClientCredentialsUser"];
        string password = ConfigurationManager.AppSettings["RewardServiceClientCredentialsPassword"];

        BasicHttpBinding basicAuthBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);

        basicAuthBinding.MaxReceivedMessageSize = Convert.ToInt32(200000000);
        basicAuthBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
        basicAuthBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
        basicAuthBinding.Security.Transport.Realm = "AXIS";
        basicAuthBinding.Security.Transport.ExtendedProtectionPolicy = new ExtendedProtectionPolicy(PolicyEnforcement.Never);


        EndpointAddress basicAuthEndpoint = new EndpointAddress(endpointFromConfig);

        RrewardServiceClient sapClient = new RrewardServiceClient(basicAuthBinding, basicAuthEndpoint);

        try
        {

            ClientCredentials clientCredentials = new ClientCredentials();
            clientCredentials.UserName.UserName = username;
            clientCredentials.UserName.Password = password;

            sapClient.ChannelFactory.Endpoint.Behaviors.RemoveAt(1);
            sapClient.ChannelFactory.Endpoint.Behaviors.Add(clientCredentials);


            if (sapClient.State == CommunicationState.Faulted)
            {
                sapClient.Abort();
                return null;
            }
            return sapClient;
        }
        catch (Exception ex)
        {
            _log.Error("SOA Client error:" + ex);
            sapClient.Abort();
            return null;
        }

    }

No comments:

Post a Comment