The object does not connect to the server - c#

I have this code:
public class Rabbit
{
public IConnection GetConnection(string hostName, string userName, string password)
{
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.HostName = "1.2.3.4";
connectionFactory.UserName = "login";
connectionFactory.Password = "pass";
connectionFactory.Port = 5672;
return connectionFactory.CreateConnection();
}
public void Send(string queue, string data)
{
using (IConnection connection = new ConnectionFactory().CreateConnection())
{
using (IModel channel = connection.CreateModel())
{
channel.QueueDeclare(queue, false, false, false, null);
channel.BasicPublish(string.Empty, queue, null, Encoding.UTF8.GetBytes(data));
}
}
}
public string Receive(string queue)
{
using (IConnection connection = new ConnectionFactory().CreateConnection())
{
using (IModel channelconsumer = connection.CreateModel())
{
channelconsumer.QueueDeclare(queue, false, false, false, null);
BasicGetResult result = channelconsumer.BasicGet(queue, true);
if (result != null)
{
string data = Encoding.UTF8.GetString(result.Body);
Console.WriteLine(data);
return data;
} else
{
return null;
}
}
}
}
}
I run my code:
var rabbit = new Rabbit();
rabbit.Send("sample.queue.queue", json);
My RABBIT object connects to the localhost, not the server whose configuration is in GetConnection.
Can I ask you to correct this code?
Login details are correct in my code.

It doesn't look like you consume the GetConnection method, you just create a completely separate ConnectionFactory which makes it use default values.
Looks like you should replace
using (IConnection connection = new ConnectionFactory().CreateConnection())
with
using (IConnection connection = GetConnection())

Related

Having Difficulty Connecting to PTZ Camera via Onvif Unity Library

I am trying to develop a unity application that, among other things, controls a PTZ camera remotely. I'm having trouble actually getting the connection set up though.
I am using the Onvif Unity library made by Oneiros90: https://github.com/Oneiros90/UnityOnvif
When I go into the test scene in Unity-OnvifMain > core > test > scenes (which only has an empty game object and the test script) and hit play, the script initializes all clients without errors but upon reaching the mediaClient.getProfiles() line, it throws an error "WebException: There was an error on processing web request: Status code 400(BadRequest): Bad Request"
Here is the relevant test script code for reference:
using System.Linq;
using Onvif.Core;
using UnityEngine;
public class OnvifClientTest : MonoBehaviour
{
[SerializeField]
private string host = "";
[SerializeField]
private string username = "admin";
[SerializeField]
private string password = "";
[SerializeField]
private string ptzServiceAddress = "";
[SerializeField]
private string mediaServiceAddress = "";
private Vector2 ptzPanSpeedLimits;
private Vector2 ptzTiltSpeedLimits;
private Vector2 ptzZoomSpeedLimits;
private OnvifClient onvif;
private string currentProfile;
private PTZClient ptzClient;
private MediaClient mediaClient;
private async void Start()
{
onvif = new OnvifClient(host, username, password);
var success = await onvif.Init();
if (!success)
{
Debug.LogError($"Cannot connect to {host}");
return;
}
ptzClient = await onvif.CreatePTZClient(ptzServiceAddress);
mediaClient = await onvif.CreateMediaClient(mediaServiceAddress);
var profiles = mediaClient.GetProfiles();
currentProfile = profiles.First().token;
var ptzConf = mediaClient.GetProfile(currentProfile).PTZConfiguration;
Space2DDescription panTiltSpace = null;
Space1DDescription zoomSpace = null;
//other stuff that does not matter yet because it doesn't get this far
}
}
And the OnvifClient code also:
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Threading.Tasks;
using Onvif.Security;
using UnityEngine;
namespace Onvif.Core
{
public class OnvifClient
{
private DeviceClient device;
private Binding binding;
private SoapSecurityHeaderBehavior securityHeader;
public Uri OnvifUri { get; private set; }
public string Host { get; private set; }
public string Username { get; private set; }
public string Password { get; private set; }
public Capabilities Capabilities { get; private set; }
public OnvifClient(string host, string username, string password)
{
Host = host;
OnvifUri = new Uri(host); //($"http://{host}/onvif/device_service");
Username = username;
Password = password;
}
public async Task<bool> Init()
{
try
{
//Have to set http continue to false since the server is not expecting it: https://forum.unity.com/threads/http-error-417-expectation-failed-unitywebrequest.651256/
System.Net.ServicePointManager.Expect100Continue = false;
binding = CreateBinding();
var endpoint = new EndpointAddress(OnvifUri);
device = new DeviceClient(binding, endpoint);
var timeShift = await GetDeviceTimeShift(device);
securityHeader = new SoapSecurityHeaderBehavior(Username, Password, timeShift);
device = new DeviceClient(binding, endpoint);
device.ChannelFactory.Endpoint.EndpointBehaviors.Clear();
device.ChannelFactory.Endpoint.EndpointBehaviors.Add(securityHeader);
device.Open();
Capabilities = device.GetCapabilities(new CapabilityCategory[] { CapabilityCategory.All });
return true;
}
catch (Exception e)
{
Debug.LogError(e.Message);
return false;
}
}
public void Ping()
{
device.GetCapabilities(new CapabilityCategory[] { CapabilityCategory.All });
}
public void Close()
{
if (device != null)
device.Close();
}
public async Task<DeviceClient> GetDeviceClient()
{
if (device == null)
await Init();
return device;
}
//original
public async Task<MediaClient> CreateMediaClient()
{
if (device == null)
await Init();
var media = new MediaClient(binding, new EndpointAddress(new Uri(Capabilities.Media.XAddr)));
media.ChannelFactory.Endpoint.EndpointBehaviors.Clear();
media.ChannelFactory.Endpoint.EndpointBehaviors.Add(securityHeader);
return media;
}
//custom
public async Task<MediaClient> CreateMediaClient(string endpoint)
{
if (device == null)
await Init();
var media = new MediaClient(binding, new EndpointAddress(new Uri(endpoint)));
media.ChannelFactory.Endpoint.EndpointBehaviors.Clear();
media.ChannelFactory.Endpoint.EndpointBehaviors.Add(securityHeader);
return media;
}
//original
public async Task<PTZClient> CreatePTZClient()
{
if (device == null)
await Init();
var ptz = new PTZClient(binding, new EndpointAddress(new Uri(Capabilities.PTZ.XAddr)));
ptz.ChannelFactory.Endpoint.EndpointBehaviors.Clear();
ptz.ChannelFactory.Endpoint.EndpointBehaviors.Add(securityHeader);
return ptz;
}
//custom
public async Task<PTZClient> CreatePTZClient(string endpoint)
{
if (device == null)
await Init();
var ptz = new PTZClient(binding, new EndpointAddress(new Uri(endpoint)));
ptz.ChannelFactory.Endpoint.EndpointBehaviors.Clear();
ptz.ChannelFactory.Endpoint.EndpointBehaviors.Add(securityHeader);
return ptz;
}
public async Task<ImagingPortClient> CreateImagingClient()
{
if (device == null)
await Init();
var imaging = new ImagingPortClient(binding, new EndpointAddress(new Uri(Capabilities.Imaging.XAddr)));
imaging.ChannelFactory.Endpoint.EndpointBehaviors.Clear();
imaging.ChannelFactory.Endpoint.EndpointBehaviors.Add(securityHeader);
return imaging;
}
private static Binding CreateBinding()
{
var binding = new CustomBinding();
var textBindingElement = new TextMessageEncodingBindingElement
{
MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None)
};
var httpBindingElement = new HttpTransportBindingElement
{
AllowCookies = true,
MaxBufferSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue
};
binding.Elements.Add(textBindingElement);
binding.Elements.Add(httpBindingElement);
return binding;
}
private static Task<TimeSpan> GetDeviceTimeShift(DeviceClient device)
{
return Task.Run(() =>
{
var utc = device.GetSystemDateAndTime().UTCDateTime;
var dt = new System.DateTime(utc.Date.Year, utc.Date.Month, utc.Date.Day,
utc.Time.Hour, utc.Time.Minute, utc.Time.Second);
return dt - System.DateTime.UtcNow;
});
}
}
}
I'm very new to .net client connection and so forth, so I'm really not sure why it would throw a 400 error, as the username and password are both correct. I also know for a fact that connecting to a camera works as I am working off of an existing project that does it through c# (outside of unity though).
Alternatively, I have tried avoiding using the helper scripts provided by Oneiros90 and connecting to the devices using the core onfiv scripts, as per this post: C# .NET Framework 4.52 Zoom/Focus for PELCO Camera via ONVIF
But when I do that, it reaches the mediaClient.getProfiles() line and instead produces an "InvalidOperationException: Use ClientCredentials to specify a user name for required HTTP Digest authentication." error, even though I am clearly setting the http digest client credentials just above. This is also how it works in the aforementioned existing project, although there isn't as much going on with the security headers and so on in that.
My Code in unity:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Onvif.Core;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
public class PTZController_Local : MonoBehaviour
{
public string hostAddress = "http://<secret>/onvif/device_service";
public string mediaAddress = "http://<secret>/onvif/media_service";
public string PTZAddress = "http://<secret>/onvif/ptz_service";
public string username = "";
public string password = "";
private OnvifClient onvifClient;
private string currentProfile;
private PTZClient ptzClient;
private MediaClient mediaClient;
private DeviceClient device;
private CustomBinding binding;
private CustomBinding mediaBinding;
private void Start()
{
////connect to onvif device service
//onvifClient = new OnvifClient(hostAddress, username, password);
//var success = await onvifClient.Init();
//if (!success)
//{
// Debug.LogError($"PTZController: Cannot connect to {hostAddress}");
// return;
//}
//Debug.Log($"PTZController: Successfully connected to {hostAddress}");
//device = await onvifClient.GetDeviceClient();
System.Net.ServicePointManager.Expect100Continue = false;
HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Digest;
httpTransport.MaxReceivedMessageSize = Int32.MaxValue;
httpTransport.MaxBufferSize = Int32.MaxValue;
httpTransport.ProxyAddress = null;
httpTransport.BypassProxyOnLocal = true;
httpTransport.UseDefaultWebProxy = false;
httpTransport.TransferMode = TransferMode.StreamedResponse;
binding = new CustomBinding(new TextMessageEncodingBindingElement(MessageVersion.Soap12WSAddressing10, System.Text.Encoding.UTF8), httpTransport);
binding.CloseTimeout = TimeSpan.FromSeconds(30.0);
binding.OpenTimeout = TimeSpan.FromSeconds(30.0);
binding.SendTimeout = TimeSpan.FromMinutes(10.0);
binding.ReceiveTimeout = TimeSpan.FromMinutes(3.0);
device = new DeviceClient(binding, new EndpointAddress(hostAddress));
device.ClientCredentials.HttpDigest.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
device.ClientCredentials.HttpDigest.ClientCredential.UserName = username;
device.ClientCredentials.HttpDigest.ClientCredential.Password = password;
var messageElement = new TextMessageEncodingBindingElement()
{
MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None)
};
HttpTransportBindingElement mediaHttpTransport = new HttpTransportBindingElement()
{
AuthenticationScheme = System.Net.AuthenticationSchemes.Digest
};
mediaBinding = new CustomBinding(messageElement, mediaHttpTransport);
mediaClient = new MediaClient(mediaBinding, new EndpointAddress(mediaAddress));
mediaClient.ClientCredentials.HttpDigest.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
mediaClient.ClientCredentials.HttpDigest.ClientCredential.UserName = username;
mediaClient.ClientCredentials.HttpDigest.ClientCredential.Password = password;
var profiles = mediaClient.GetProfiles();
currentProfile = profiles.First().token;
//other code that is never reached
}
}
Original project code (i've tried copying this into unity exactly and get the same error about setting the http credentials):
using System.IO;
using System.IO.Pipes;
using System.Text;
using System.ServiceModel;
using OnvifPipeClient.OnvifMedia10;
using OnvifPipeClient.OnvifPTZService;
using System.Timers;
using System.Diagnostics;
using System.Threading;
namespace OnvifPipeClient
{
public class PTZConnection
{
public static PTZClient ptzClient;
private static MediaClient mediaClient;
public static Profile profile;
// establish connection to PTZ
public static void OnvifLogin()
{
// create binding
System.Net.ServicePointManager.Expect100Continue = false;
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Digest;
// connect to PTZ interface
ptzClient = new PTZClient(binding, new EndpointAddress("http://<secret>/onvif/ptz_service"));
ptzClient.ClientCredentials.HttpDigest.ClientCredential.UserName = "secret";
ptzClient.ClientCredentials.HttpDigest.ClientCredential.Password = "secret";
ptzClient.ClientCredentials.HttpDigest.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
// connect to media client
mediaClient = new MediaClient(binding, new EndpointAddress("http://<secret>/onvif/media_service"));
mediaClient.ClientCredentials.HttpDigest.ClientCredential.UserName = "secret";
mediaClient.ClientCredentials.HttpDigest.ClientCredential.Password = "secret";
mediaClient.ClientCredentials.HttpDigest.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
// load profiles and configurations
var profiles = mediaClient.GetProfiles();
profile = mediaClient.GetProfile(profiles[0].token);
//other code that is reached without issues
}
}
The only thread of a possible explanation I could find is this github issue(https://github.com/jamidon/ONVIF-test/issues/1) saying that another onvif c# library that oneiros90 had previously tried did not work with pelco cameras for some reason (the camera I'm trying to connect to is a pelco camera).
So, yeah, sorry for the big code dumps here but it's very difficult to find resources about this. Thanks in advance for any help.

Response Bargain Finder Max Compress c#

good morning, I have a solution in .net where I call the webservices of saber bargain finder max. Now I want to download the compressed information but the response object returns null. I read that you have to call the interface IClientMessageInspector BeforeSendRequest and AfterReceiveReply but I do not know how to proceed. someone will have an example or solution about it? Thank you
The response for the service cannot handle the compressed response, so yes, you have to put middleware in order to process the decompression before you let it continue.
First of all, you need to import the BFM WSDL as a service instead of as a web services, so:
Right click on "Service References" and click on "Add Service Reference..."
Paste the WSDL URL under "Address:" and click on "Go"
Recommendation: Download the WSDL and the associated schemas so you can modify them, since .NET has some issues handling some things, which you can manually modify. Sabre has some covered here.
Name the service whatever you like, in this example I'll call it BargainFinderMaxRQ_4_1_0_Srvc, under "Namespace:" and click on "OK"
Then, I created 2 classes, one that will be the one calling BFM ("BFM_v410Service"), and another one for the middleware ("BFMInspector").
Let's start with the BFMInspector:
// The inspector class has to implement both IClientMessageInspector and IEndpointBehavior interfaces
public class BFMInspector : IClientMessageInspector, IEndpointBehavior
{
// This is the method to action after receiving a response from Sabre
public void AfterReceiveReply(ref Message reply, object correlationState)
{
try
{
// Get compressed response from reply and load that into a byte array.
XmlDictionaryReader bodyReader = reply.GetReaderAtBodyContents();
bodyReader.ReadStartElement("CompressedResponse");
byte[] bodyByteArray = bodyReader.ReadContentAsBase64();
// Create some helper variables
StringBuilder uncompressed = new StringBuilder();
String xmlString = "";
XmlDocument xmlPayload = new XmlDocument();
// Load the byte array into memory
using (MemoryStream memoryStream = new MemoryStream(bodyByteArray))
{
// Unzip the Stream
using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
byte[] buffer = new byte[1024];
int readBytes;
// Unzips character by character
while ((readBytes = gZipStream.Read(buffer, 0, buffer.Length)) != 0)
{
for (int i = 0; i < readBytes; i++)
// Append all characters to build the response
uncompressed.Append((char)buffer[i]);
}
}
xmlString = uncompressed.ToString();
xmlString = xmlString.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>", "");
}
// Convert the string into an XML
xmlPayload.LoadXml(xmlString);
// Create a new Message, which is what will substitute what was returned by Sabre
Message tempMessage = Message.CreateMessage(reply.Version, null, xmlPayload.ChildNodes[0]);
tempMessage.Headers.CopyHeadersFrom(reply.Headers);
tempMessage.Properties.CopyProperties(reply.Properties);
MessageBuffer bufferOfFault = tempMessage.CreateBufferedCopy(Int32.MaxValue);
// Replace the reply with the new Message
reply = bufferOfFault.CreateMessage();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
// Nothing is done here, so we simply return null
return null;
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
// Nothing done
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
// Add "this" as an endpoint to be inspected
clientRuntime.MessageInspectors.Add(this);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
// Nothing done
}
public void Validate(ServiceEndpoint endpoint)
{
// Nothing done
}
}
Now, the BFM_v410Service:
// BFM calling class
public class BFM_v410Service
{
// The constructor and CreateRequest simeply create a complete request
private BargainFinderMaxRQRequest service;
private OTA_AirLowFareSearchRQ request;
public OTA_AirLowFareSearchRS response;
private string endpoint;
public BFM_v410Service(string token, string pcc, string convId, string endpoint)
{
CreateRequest(pcc, true);
this.endpoint = endpoint;
service = new BargainFinderMaxRQRequest()
{
MessageHeader = new BargainFinderMaxRQ_3_4_0_Srvc.MessageHeader()
{
From = new From()
{
PartyId = new PartyId[]
{
new PartyId()
{
Value = pcc
}
}
},
To = new To()
{
PartyId = new PartyId[]
{
new PartyId()
{
Value = endpoint
}
}
},
ConversationId = convId,
CPAId = pcc,
Service = new Service()
{
Value = "BargainFinderMaxRQ"
},
Action = "BargainFinderMaxRQ",
MessageData = new MessageData()
{
Timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssK")
}
},
OTA_AirLowFareSearchRQ = request,
Security = new Security()
{
BinarySecurityToken = token
}
};
}
private void CreateRequest(string pcc, bool compressed)
{
request = new OTA_AirLowFareSearchRQ()
{
Version = "3.4.0",
POS = new SourceType[]
{
new SourceType()
{
PseudoCityCode = pcc,
RequestorID = new UniqueID_Type()
{
ID = "1",
Type = "1",
CompanyName = new CompanyNameType()
{
Code = "TN",
Value = "TN"
}
}
}
},
OriginDestinationInformation = new OTA_AirLowFareSearchRQOriginDestinationInformation[]
{
new OTA_AirLowFareSearchRQOriginDestinationInformation()
{
RPH = "1",
Item = "2018-09-21T11:00:00",
ItemElementName = ItemChoiceType.DepartureDateTime,
OriginLocation = new OriginDestinationInformationTypeOriginLocation()
{
LocationCode = "MVD"
},
DestinationLocation = new OriginDestinationInformationTypeDestinationLocation()
{
LocationCode = "KRK"
}
},
new OTA_AirLowFareSearchRQOriginDestinationInformation()
{
RPH = "2",
Item = "2018-09-28T11:00:00",
ItemElementName = ItemChoiceType.DepartureDateTime,
OriginLocation = new OriginDestinationInformationTypeOriginLocation()
{
LocationCode = "KRK"
},
DestinationLocation = new OriginDestinationInformationTypeDestinationLocation()
{
LocationCode = "MVD"
}
}
},
TravelerInfoSummary = new TravelerInfoSummaryType()
{
AirTravelerAvail = new TravelerInformationType[]
{
new TravelerInformationType()
{
PassengerTypeQuantity = new PassengerTypeQuantityType[]
{
new PassengerTypeQuantityType()
{
Code = "ADT",
Quantity = "1"
}
}
}
}
},
TPA_Extensions = new OTA_AirLowFareSearchRQTPA_Extensions()
{
IntelliSellTransaction = new TransactionType()
{
RequestType = new TransactionTypeRequestType()
{
Name = "50ITINS"
},
CompressResponse = new TransactionTypeCompressResponse()
{
Value = compressed
}
}
}
};
}
public void Execute()
{
try
{
// Instanciate the Inspector
BFMInspector inspector = new BFMInspector();
// Select the URL you'll be sending the request. I've passed this as a parameter in the constructor
EndpointAddress url = new EndpointAddress(new Uri(endpoint));
// Create a binding, with a couple of characteristics, because of the size of the response
Binding binding = new BasicHttpsBinding()
{
MaxReceivedMessageSize = Int32.MaxValue,
MaxBufferSize = Int32.MaxValue
};
// Create the executable the BargainFinderMaxPortTypeClient variable, which will allow me to call the service
BargainFinderMaxPortTypeClient execute = new BargainFinderMaxPortTypeClient(binding, url);
// Add the middleware. Here's where ApplyClientBehavior is called behind the scene and adds itself
execute.Endpoint.EndpointBehaviors.Add(inspector);
// Call BFM and successfully get the response as an OTA_AirLowFareSearchRS object
response = execute.BargainFinderMaxRQ(ref service.MessageHeader, ref service.Security, request);
Console.WriteLine(response);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Then, simply call this from a console application:
static void Main(string[] args)
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
string token = #"Shared/IDL:IceSess\/SessMgr:1\.0.IDL/Common/!ICESMS\/RESA!ICESMSLB\/RES.LB!-3146624380791354996!413892!0!1!E2E-1";
string pcc = "XXXX";
string convId = "HERE GOES YOUR CONVERSATION ID";
string endpoint = "https://webservices.havail.sabre.com";
BFM_v410Service bfm340 = new BFM_v410Service(token, pcc, convId, endpoint);
bfm410.Execute();
}
Hope this helps!

In WCF SOAP message, how to include certificate key inline (without Reference token)?

I need a C# configuration which would generate envelope shown on the third part of the screenshot. Currently I can only get as close as shown on the first two. My current config:
using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Security;
using System.ServiceModel.Security.Tokens;
using System.Text;
public class CustomAlgorithmSuite : SecurityAlgorithmSuite
{
public override string DefaultAsymmetricKeyWrapAlgorithm { get { return "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; }}
public override string DefaultAsymmetricSignatureAlgorithm { get { return "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; }}
public override string DefaultCanonicalizationAlgorithm { get { return "http://www.w3.org/2001/10/xml-exc-c14n#"; }}
public override string DefaultDigestAlgorithm { get { return "http://www.w3.org/2000/09/xmldsig#sha1"; }}
public override string DefaultEncryptionAlgorithm { get { return "http://www.w3.org/2001/04/xmlenc#aes256-cbc"; }}
public override int DefaultEncryptionKeyDerivationLength { get { return SecurityAlgorithmSuite.Default.DefaultEncryptionKeyDerivationLength; }}
public override int DefaultSignatureKeyDerivationLength { get { return SecurityAlgorithmSuite.Default.DefaultSignatureKeyDerivationLength; }}
public override int DefaultSymmetricKeyLength { get { return SecurityAlgorithmSuite.Default.DefaultSymmetricKeyLength; }}
public override string DefaultSymmetricKeyWrapAlgorithm { get { return "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; }}
public override string DefaultSymmetricSignatureAlgorithm { get { return "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; }}
public override bool IsAsymmetricKeyLengthSupported(int length) { return true; }
public override bool IsSymmetricKeyLengthSupported(int length) { return true; }
}
class Program
{
static void Main()
{
X509SecurityTokenParameters x509Params = new X509SecurityTokenParameters()
{
X509ReferenceStyle = X509KeyIdentifierClauseType.RawDataKeyIdentifier,
InclusionMode = SecurityTokenInclusionMode.AlwaysToRecipient,
ReferenceStyle = SecurityTokenReferenceStyle.External,
RequireDerivedKeys = false
};
SecurityBindingElement security = new TransportSecurityBindingElement()
{
MessageSecurityVersion = MessageSecurityVersion.WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10,
DefaultAlgorithmSuite = new CustomAlgorithmSuite()
};
security.EndpointSupportingTokenParameters.Endorsing.Add(x509Params);
security.SetKeyDerivation(false);
//security.IncludeTimestamp = false;
TextMessageEncodingBindingElement encoding = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);
HttpsTransportBindingElement transport = new HttpsTransportBindingElement();
//transport.RequireClientCertificate = true;
CustomBinding customBinding = new CustomBinding(security, encoding, transport);
ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
var twoCertificatesInOneFile = new X509Certificate2Collection();
twoCertificatesInOneFile.Import("foo path", "foo cert pass", X509KeyStorageFlags.Exportable);
someGeneratedServiceClass client = new someGeneratedServiceClass(customBinding, new EndpointAddress(new Uri("foo webservice address"), EndpointIdentity.CreateDnsIdentity(twoCertificatesInOneFile[0].FriendlyName)));
client.ClientCredentials.ServiceCertificate.DefaultCertificate = twoCertificatesInOneFile[0];
client.ClientCredentials.ClientCertificate.Certificate = twoCertificatesInOneFile[1];
//client.Endpoint.Contract.ProtectionLevel = System.Net.Security.ProtectionLevel.None;
client.ClientCredentials.UserName.UserName = "foo user";
client.ClientCredentials.UserName.Password = "foo pass";
client.someServiceCall("foo", "foo", false, out i1, out i2);
}
}
Is there any property I'm missing, or must I implement a custom token / encoder for this job?
This has the same answer as this question.
There was no standard way of achieving this nonstandard implementation, so I ended up rewriting parts of the generated SOAP message manually.

Trying to Save Gmail Atom Feed to XML - C#

I'm new to C# and I'm having some difficulty with saving the XML Atom feed from Gmail to an xml file. I'm certain I'm miles off of where I need to be and I'm embarassed to be asking this but I'm not getting anywhere on my own:(
I'm using the GmailHandler class that's been floating around for some time.
GmailHandler.cs
using System;
using System.Data;
using System.Xml;
using System.Net;
using System.IO;
/*
* this code made by Ahmed Essawy
* AhmedEssawy#gmail.com
* http://fci-h.blogspot.com
*/
/// <summary>
/// Summary description for Class1
/// </summary>
public class GmailHandler
{
private string username;
private string password;
private string gmailAtomUrl;
public string GmailAtomUrl
{
get { return gmailAtomUrl; }
set { gmailAtomUrl = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
public string Username
{
get { return username; }
set { username = value; }
}
public GmailHandler(string _Username, string _Password, string _GmailAtomUrl)
{
Username = _Username;
Password = _Password;
GmailAtomUrl = _GmailAtomUrl;
}
public GmailHandler(string _Username, string _Password)
{
Username = _Username;
Password = _Password;
GmailAtomUrl = "https://mail.google.com/mail/feed/atom";
}
public XmlDocument GetGmailAtom()
{
byte[] buffer = new byte[8192];
int byteCount = 0;
XmlDocument _feedXml = null;
try
{
System.Text.StringBuilder sBuilder = new System.Text.StringBuilder();
WebRequest webRequest = WebRequest.Create(GmailAtomUrl);
webRequest.PreAuthenticate = true;
System.Net.NetworkCredential credentials = new NetworkCredential(this.Username, this.Password);
webRequest.Credentials = credentials;
WebResponse webResponse = webRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
while ((byteCount = stream.Read(buffer, 0, buffer.Length)) > 0)
sBuilder.Append(System.Text.Encoding.ASCII.GetString(buffer, 0, byteCount));
_feedXml = new XmlDocument();
_feedXml.LoadXml(sBuilder.ToString());
}
catch (Exception ex)
{
//add error handling
throw ex;
}
return _feedXml;
}
}
Then I've got my Program.cs here:
I'm assuming the issue is with the code below since I'm responsible for that, and not what's above.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace GmailAtom
{
class Program
{
static void Main()
{
//Create the object from GmailHandler class
GmailHandler gmailFeed = new GmailHandler("username", "password");
//get the feed
XmlDocument myXml = gmailFeed.GetGmailAtom();
XmlTextWriter writer = new XmlTextWriter("data.xml", null);
writer.Formatting = Formatting.Indented;
myXml.Save(writer);
}
}
}
When I run the program I get a "WebException was unhandled - The remote server returned an error: (407) Proxy Authentication Required."
Any advice would be appreciated!
I have tried the code and it works fine (but I have no proxy in my network).
I have changed the GmailHandler.cs, the constructor now accepts a internet proxy.
using System;
using System.Data;
using System.Xml;
using System.Net;
using System.IO;
/*
* this code made by Ahmed Essawy
* AhmedEssawy#gmail.com
* http://fci-h.blogspot.com
*/
/// <summary>
/// Summary description for Class1
/// </summary>
public class GmailHandler
{
private string username;
private string password;
private string gmailAtomUrl;
private string proxy;
public string GmailAtomUrl
{
get { return gmailAtomUrl; }
set { gmailAtomUrl = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
public string Username
{
get { return username; }
set { username = value; }
}
public string Proxy
{
get { return proxy; }
set { proxy = value; }
}
public GmailHandler(string _Username, string _Password, string _GmailAtomUrl, string _proxy = null)
{
Username = _Username;
Password = _Password;
GmailAtomUrl = _GmailAtomUrl;
Proxy = _proxy;
}
public GmailHandler(string _Username, string _Password, string _proxy = null)
{
Username = _Username;
Password = _Password;
GmailAtomUrl = "https://mail.google.com/mail/feed/atom";
Proxy = _proxy;
}
public XmlDocument GetGmailAtom()
{
byte[] buffer = new byte[8192];
int byteCount = 0;
XmlDocument _feedXml = null;
try
{
System.Text.StringBuilder sBuilder = new System.Text.StringBuilder();
WebRequest webRequest = WebRequest.Create(GmailAtomUrl);
if(!String.IsNullOrWhiteSpace(Proxy))
webRequest.Proxy = new WebProxy(Proxy, true);
webRequest.PreAuthenticate = true;
System.Net.NetworkCredential credentials = new NetworkCredential(this.Username, this.Password);
webRequest.Credentials = credentials;
WebResponse webResponse = webRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
while ((byteCount = stream.Read(buffer, 0, buffer.Length)) > 0)
sBuilder.Append(System.Text.Encoding.ASCII.GetString(buffer, 0, byteCount));
_feedXml = new XmlDocument();
_feedXml.LoadXml(sBuilder.ToString());
}
catch (Exception ex)
{
//add error handling
throw ex;
}
return _feedXml;
}
}
Use this in your console application:
//Create the object from GmailHandler class
GmailHandler gmailFeed = new GmailHandler("username", "password", "http://proxyserver:80/");
//get the feed
XmlDocument myXml = gmailFeed.GetGmailAtom();
XmlTextWriter writer = new XmlTextWriter("data.xml", null);
writer.Formatting = Formatting.Indented;
myXml.Save(writer);

A static variable in static class asp.net mvc

I have a static class Data:
public static class Data
{
public static SqlConnection connexion;
public static bool Connect()
{
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Initial Catalog"] = "Upload";
builder["Data Source"] = "base";
builder["integrated Security"] = true;
string connexionString = builder.ConnectionString;
connexion = new SqlConnection(connexionString);
try { connexion.Open(); return true; }
catch { return false; }
}
public static void Disconnect()
{
if (connexion != null) connexion.Close();
connexion = null;
}
}
in an the action Home :
public ActionResult Home()
{
Data.Connect();
if (CompteModels.Connected)
{
ArrayList model = new ArrayList();
ClientModels clients = new ClientModels();
model.AddRange(clients.Client_List());
AdminModels admins = new AdminModels();
model.AddRange(admins.Admin_List());
return View(model);
}
else return RedirectToAction("Login", "Account");
}
the client class:
public List<ClientModels> Client_List()
{
List<ClientModels> l = new List<ClientModels>();
using (Data.connexion)
{
string queryString = #"select Login, Password, Mail, Name, Tentatives from Compte where User_type_id in ( select Id from User_type where Fonction = 'Client')";
SqlCommand command = new SqlCommand(queryString, Data.connexion);
try
{
SqlDataReader reader = command.ExecuteReader();
do
{
while (reader.Read())
{
ClientModels admin = new ClientModels { Login = reader.GetString(0), Password = reader.GetString(1), Mail = reader.GetString(2), Name = reader.GetString(3), Tentatives = reader.GetInt32(4) };
l.Add(admin);
}
} while (reader.NextResult());
return l;
}
catch { return null; }
}
For the function AdminList, the implementation is the same like Client_List but in the class Admin.
The problem is in the static variable connexion : in the first function Client_List its value is correct and i get the list of the clients , but it's become null in the second function despite it is a static variable in static class !!!
What is the reason of this alteration ? How can i fix it?
You're either setting connexion to null somewhere or not initializing it before you use it.
Most likely, one class is calling Disconnect which sets connexion to null, while another class assumes it's not null and tries to use it.
As mentioned in the comments, keeping a static reference to a resource like a SqlConnection is not a good idea. If you want to re-use code you can create a static function that returns a new SqlConnection instance and make the connection string static, but having a static reference to a connection that's share across the entire web site will give you more problems that it's worth (as you're already seeing).
One way to do it in a static function would be:
public static SqlConnection GetConnection()
{
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Initial Catalog"] = "Upload";
builder["Data Source"] = "base";
builder["integrated Security"] = true;
string connexionString = builder.ConnectionString;
connexion = new SqlConnection(connexionString);
return connexion;
}
Your client code would then look something like:
using (SqlConnection conn = Data.GetConnection())
This is a bad idea, as others have already mentioned.
You should simply create and open a connection when needed, and dispose it afterwards:
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// ... now use it
}
Of course, there are other patterns, that hide this mechanism, but that might be overkill in your case.
Your using statement disposes of the connexion using the IDisposable interface
public List<ClientModels> Client_List()
{
List<ClientModels> l = new List<ClientModels>();
using (Data.connexion) <--- here
}
Change this to create a new connection
public List<ClientModels> Client_List()
{
List<ClientModels> l = new List<ClientModels>();
using (var connection = Data.CreateConnexion())
}
similar to this
public static class Data
{
public static SqlConnection CreateConnection()
{
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Initial Catalog"] = "Upload";
builder["Data Source"] = "base";
builder["integrated Security"] = true;
string connexionString = builder.ConnectionString;
var connexion = new SqlConnection(connexionString);
connexion.Open();
return connexion;
}
}

Categories

Resources