ONVIF api capture image in C# - c#

I have an ONVIF ip camera.
I want to to capture an image from the camera so that I can process that image and save it to the file system.
I found out that there is an onvif api which provides a method GetSnapshotUri which should provide me with an image snapshot:
http://www.onvif.org/onvif/ver10/media/wsdl/media.wsdl
I managed to import this api in visual studio by adding a service reference to it:
How do I construct a client to call GetSnapshotUri from this service?

So, after lots of searching I managed to capture an image from the camera.
The first Problem was that I have used "Add Service Reference->Advanced->Add Web reference" instead of typing the service address directly in the "Add Service Reference" box.
Here, I added the address: http://www.onvif.org/onvif/ver10/media/wsdl/media.wsdl
Then I could use the MediaClient class, correctly pointed out by pepOS in a comment, and the final code looks like:
var messageElement = new TextMessageEncodingBindingElement();
messageElement.MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None);
HttpTransportBindingElement httpBinding = new HttpTransportBindingElement();
httpBinding.AuthenticationScheme = AuthenticationSchemes.Basic;
CustomBinding bind = new CustomBinding(messageElement, httpBinding);
EndpointAddress mediaAddress = new EndpointAddress("http://192.168.1.168:10001/onvif/Media");
MediaClient mediaClient = new MediaClient(bind, mediaAddress);
mediaClient.ClientCredentials.UserName.UserName = "admin";
mediaClient.ClientCredentials.UserName.Password = "admin";
Profile[] profiles = mediaClient.GetProfiles();
string profileToken = profiles[1].token;
MediaUri mediaUri = mediaClient.GetSnapshotUri(profileToken);
The uri of the image could then be fount at the MediaUri.Uriaddress

The GetSnapshotUri returns a uri for downloading an image using HTTP get.
So in theory you just need to call this function, and use the returned uri in the function shown in this Stackoverflow article:
https://stackoverflow.com/a/3615831/4815603

I am using onvif device manager dll here. To implement this method camera's IP, username and password must be known.
// Onvif ODM
using onvif.services;
using odm.core;
using onvif.utils;
using utils;
public string GetSnapshotUrl()
{
try
{
string camera_ip = "http://" + camIp + "/onvif/device_service";
Uri Camuri = new Uri(camera_ip);
NvtSessionFactory sessionFactory = new NvtSessionFactory(account);
INvtSession session = sessionFactory.CreateSession(Camuri);
Profile[] Profiles = session.GetProfiles().RunSynchronously();
var snapshotlink = session.GetSnapshotUri(Profiles[0].token).RunSynchronously(); // taking snapshot on the first profile of the camera
return snapshotlink.uri;
}
catch (Exception ex)
{
return null;
}
}

For me basic authentication didn't work. The following code works for me and downloads the image:
string username = "username";
string password = "password";
string cameraIP = "";
var messageElement = new TextMessageEncodingBindingElement()
{
MessageVersion = MessageVersion.CreateVersion(
EnvelopeVersion.Soap12, AddressingVersion.None)
};
HttpTransportBindingElement httpBinding = new HttpTransportBindingElement()
{
AuthenticationScheme = AuthenticationSchemes.Digest
};
CustomBinding bind = new CustomBinding(messageElement, httpBinding);
MediaClient mediaClient = new MediaClient(bind, new EndpointAddress($"http://{cameraIP}/onvif/device_service"));
mediaClient.ClientCredentials.HttpDigest.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
mediaClient.ClientCredentials.HttpDigest.ClientCredential.UserName = username;
mediaClient.ClientCredentials.HttpDigest.ClientCredential.Password = password;
Profile[] profiles = mediaClient.GetProfiles();
string profileToken = profiles[0].token;
MediaUri mediaUri = mediaClient.GetSnapshotUri(profileToken);
WebClient webCl = new WebClient()
{
Credentials = new NetworkCredential(username, password)
};
webCl.DownloadFile(mediaUri.Uri, #"D:\test.jpg");

Related

How to generate GetSystemDateAndTime xml

I have the following bit of code that i took from this source...
public bool Initialise(string cameraAddress, string userName, string password)
{
bool result = false;
try
{
var messageElement = new TextMessageEncodingBindingElement()
{
MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None)
};
HttpTransportBindingElement httpBinding = new HttpTransportBindingElement()
{
AuthenticationScheme = AuthenticationSchemes.Digest
};
CustomBinding bind = new CustomBinding(messageElement, httpBinding);
mediaClient = new MediaClient(bind, new EndpointAddress($"http://{cameraAddress}/onvif/Media"));
mediaClient.ClientCredentials.HttpDigest.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
mediaClient.ClientCredentials.HttpDigest.ClientCredential.UserName = userName;
mediaClient.ClientCredentials.HttpDigest.ClientCredential.Password = password;
var profs = mediaClient.GetProfiles();
//rest of the code...
When i run wireshark while going through the GetProfiles() part in the debugger, I see that the generated XML looks like:
What code would it take to change the xml to look like:
How am i supposed to call the GetSystemDateAndTime function?
To call the GetProfiles function, I had to create a MediaClient and, then, call that function...
Is there such thing as a MediaClient to get access to the GetSystemDateAndTime??
Edit:
I found that you could use the DeviceClient to get access the the GetSystemDateAndTime function...
You'll need to add the device management wsdl to your connected services before:
https://www.onvif.org/ver10/device/wsdl/devicemgmt.wsdl
I also added System.Net.ServicePointManager.Expect100Continue = false; in there because i saw someone said it helped at this link...
So i added :
CustomBinding bind = new CustomBinding(messageElement, httpBinding);
System.Net.ServicePointManager.Expect100Continue = false;
DeviceClient d = new DeviceClient(bind, new EndpointAddress($"http://{cameraAddress}/onvif/device_service"));
var time = d.GetSystemDateAndTime();
Note:
I'm still getting the error:
ErrorMessage "The header 'To' from the namespace 'http://www.w3.org/2005/08/addressing' was not understood by the recipient of this message, causing the message to not be processed. This error typically indicates that the sender of this message has enabled a communication protocol that the receiver cannot process. Please ensure that the configuration of the client's binding is consistent with the service's binding. " string
This error is saying that there is trouble when trying to read a message, so i tough it was probably due to some sort of encoding ...
AND I WAS RIGHT!!
All I had to do was changing a parameter in the TextMessageEncodingBindingElement's creation.
MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.WSAddressing10)
All you need to do is make sure that you have good encoding and AuthenticationScheme...
Here's my final code to get an onvif camera's (here cohuHD camera) system and date and time settings:
public bool Initialise(string cameraAddress, string userName, string password)
{
bool result = false;
try
{
var messageElement = new TextMessageEncodingBindingElement()
{
MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.WSAddressing10)
};
HttpTransportBindingElement httpBinding = new HttpTransportBindingElement()
{
AuthenticationScheme = AuthenticationSchemes.Digest
};
CustomBinding bind = new CustomBinding(messageElement, httpBinding);
System.Net.ServicePointManager.Expect100Continue = false;
DeviceClient deviceClient = new DeviceClient(bind, new EndpointAddress($"http://{cameraAddress}/onvif/device_service"));
var temps = deviceClient.GetSystemDateAndTime();
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
}
return result;
}
Bonus:
If you want to execute a function that needs credentials, you can add those to your deviceClient like so:
//DIGEST (httpBinding)
deviceClient.ClientCredentials.HttpDigest.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
deviceClient.ClientCredentials.HttpDigest.ClientCredential.UserName = userName;
deviceClient.ClientCredentials.HttpDigest.ClientCredential.Password = password;
Watch out also for the EndpointAddress' URL... I think some cameras use Device_service and other device_service .

An Application Connect with Multiple database and set Membership cookies

I'm writing a method that set all of database connection string with it.The method has some parameters like connection string and cookie domain (for single sign on state) , ...
I can get Membership and role Information with specified connection string that send as a parameter.
//membership
System.Web.Security.SqlMembershipProvider mp = new System.Web.Security.SqlMembershipProvider();
System.Collections.Specialized.NameValueCollection config_Membership = new System.Collections.Specialized.NameValueCollection();
config_Membership.Add("connectionString", connectionstring);
config_Membership.Add("applicationName", "/");
mp.Initialize("SQL_test_Membership", config_Membership);
var u = mp.GetUser(username, false);
int TotalRecords = 0;
var p = mp.GetAllUsers(0, 1, out TotalRecords);
//login
bool valid = mp.ValidateUser(username, password);
System.Web.Security.SqlRoleProvider rp = new System.Web.Security.SqlRoleProvider();
System.Collections.Specialized.NameValueCollection config_Role = new System.Collections.Specialized.NameValueCollection();
config_Role.Add("connectionString", connectionstring);
config_Role.Add("applicationName", "/");
rp.Initialize("SQL_test_Role", config_Role);
var roles = rp.GetRolesForUser(username);
I want to get ProfileBase Information like above code
https://technet.microsoft.com/nl-nl/library/system.web.profile.profilebase.initialize(v=vs.85).aspx
and I found below code:
System.Web.Profile.ProfileBase pro = new System.Web.Profile.ProfileBase();
System.Collections.Specialized.NameValueCollection config_profile = new System.Collections.Specialized.NameValueCollection();
config_profile.Add("connectionString", connectionstring);
config_profile.Add("applicationName", "/");
pro.Initialize(?????)
but I dont know how to send parameter to pro.Initialize(), can any one help me? Thanks.
My problem solved. My code was changed :
//login
bool valid = mp.ValidateUser(username, password);
if (valid)
{
System.Web.Profile.ProfileBase pro = new System.Web.Profile.ProfileBase();
System.Collections.Specialized.NameValueCollection config_profile = new System.Collections.Specialized.NameValueCollection();
config_profile.Add("connectionString", connectionstring);
config_profile.Add("applicationName", "/");
pro.Initialize(username, true);
string Name = pro.GetPropertyValue("Name").ToString();
string Family = pro.GetPropertyValue("Family").ToString();
string phone = pro.GetPropertyValue("Phone").ToString();
string address = pro.GetPropertyValue("Address").ToString();
}

Cannot connect and create user on self hosted Parse Server in Unity3D

I recently deployed Parse Server to Amazon which is working fine when I connect and create users from iOS but doesn't work when I try to connect from Unity3D and there are no logs aswell. Is there any specific setting or something for Unity?? What am I missing? Below is the code for both platforms;
Unity Code (Not working)
// Initialization
string serverUrl = "http://myserverip.amazonaws.com:80/parse/";
ParseClient.Initialize(new ParseClient.Configuration {ApplicationId = "MYAPPID", WindowsKey = "MYCLIENTKEY", Server = serverUrl});
// User Creation
ParseUser user = new ParseUser ();
user.Username = "myname";
user.Password = "mypass";
user.SignUpAsync ().ContinueWith(t =>
{
if (t.IsFaulted || t.IsCanceled){
Debug.Log("Faliled" + t.IsFaulted);
}
else{
Debug.Log("Success");
var userId = ParseUser.CurrentUser.ObjectId;
print (userId);
}
});
iOS Code (working)
// Initialization
[Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> _Nonnull configuration) {
configuration.applicationId = #"MYAPPID";
configuration.clientKey = #"MYCLIENTKEY";
configuration.server = #"http://myinstanceIP.amazonaws.com:80/parse";
configuration.localDatastoreEnabled = YES;
}]];
// User Creation
PFUser *user = [PFUser user];
user.username = #"my name2";
user.password = #"my pass";
user.email = #"email2#example.com";
[user signUp];
You have to set appID and client key in ParseInitializeBehaviour before calling ParseClient.Initialize. Which is pretty weird because ParseClient.Initialize also takes appID and client key but I got it working this way.
Also add "/" at the end of your server url.
ParseInitializeBehaviour _script = new GameObject("ParseInitializeBehaviour").AddComponent<ParseInitializeBehaviour> ();
_script.applicationID = "APPID";
_script.dotnetKey = "CLIENTKEY";
ParseClient.Initialize (new ParseClient.Configuration ()
{
WindowsKey = "APPID",
ApplicationId = "CLIENTKEY",
Server = serverUrl
});
Everything else i.e signup, signin etc. works normally after this.

Code connect to Google Analytics API with C# error

I trying using Google Analytics with C# to get stats information to display in my webiste
Here is my code
public ActionResult Index()
{
string userName = "admin#email.com";
string passWord = "mypass";
string profileId = "ga:xxxxxxxx";
string key = "2d751338cb092ef8da65f716e37a48604386c9sw";
string dataFeedUrl = "https://www.google.com/analytics/feeds/data"+key;
var service = new AnalyticsService("API Project");
service.setUserCredentials(userName, passWord);
var dataQuery = new DataQuery(dataFeedUrl)
{
Ids = profileId,
Metrics = "ga:pageviews",
Sort = "ga:pageviews",
GAStartDate = new DateTime(2010, 3, 1).ToString("yyyy-MM-dd"),
GAEndDate = DateTime.Now.ToString("yyyy-MM-dd")
};
var dataFeed = service.Query(dataQuery);
var totalEntry = dataFeed.Entries[0];
ViewData["Total"] = ((DataEntry)(totalEntry)).Metrics[0].Value;
dataQuery.GAStartDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
dataQuery.GAEndDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
dataFeed = service.Query(dataQuery);
var yesterdayEntry = dataFeed.Entries[0];
ViewData["Yesterday"] = ((DataEntry)(yesterdayEntry)).Metrics[0].Value;
dataQuery.GAStartDate = DateTime.Now.ToString("yyyy-MM-dd");
dataQuery.GAEndDate = DateTime.Now.ToString("yyyy-MM-dd");
dataFeed = service.Query(dataQuery);
var todayEntry = dataFeed.Entries[0];
ViewData["Today"] = ((DataEntry)(todayEntry)).Metrics[0].Value;
return View(dataFeed.Entries);
}
But when i run the code it always said "{"Invalid credentials"}"
Not sure why i facing this error while i checked many time about the key,username,password and profileId
Anyone facing this problem,can help me?
Many thanks
I think that your url is wrong. try in this way (you are missing ?key=).
string dataFeedUrl = "https://www.google.com/analytics/feeds/data?key="+key;
refer this google example where there is this example that should help you
public DataFeedExample()
{
// Configure GA API.
AnalyticsService asv = new AnalyticsService("gaExportAPI_acctSample_v2.0");
// Client Login Authorization.
asv.setUserCredentials(CLIENT_USERNAME, CLIENT_PASS);
// GA Data Feed query uri.
String baseUrl = "https://www.google.com/analytics/feeds/data";
DataQuery query = new DataQuery(baseUrl);
query.Ids = TABLE_ID;
query.Dimensions = "ga:source,ga:medium";
query.Metrics = "ga:visits,ga:bounces";
query.Segment = "gaid::-11";
query.Filters = "ga:medium==referral";
query.Sort = "-ga:visits";
query.NumberToRetrieve = 5;
query.GAStartDate = "2010-03-01";
query.GAEndDate = "2010-03-15";
Uri url = query.Uri;
Console.WriteLine("URL: " + url.ToString());
// Send our request to the Analytics API and wait for the results to
// come back.
feed = asv.Query(query);
}
refer also this guide to configure your project
Also follow this guide to use OAuth 2.0

"An error occurred when verifying security for the message" exception when authenticating to ADFS

As I described in another question I build a web service that will take username/password and based on these credentials authenticate users (mobile apps) in ADFS2. My web service is configured as RP on the ADFS. ADFS issues SAML 2.0 tokens.
Here is a code of the web method:
public class MobileAuthService : IMobileAuthService
{
private const string adfsBaseAddress = #"https://<my_adfs_hostname>/adfs/services/";
private const string endpointSuffix = #"trust/13/issuedtokenmixedsymmetricbasic256";
public string AuthenticateUser(string username, string password)
{
var binding = new WS2007HttpBinding(SecurityMode.Message);
binding.Security.Message.EstablishSecurityContext = false;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
var trustChannelFactory = new WSTrustChannelFactory(binding, new EndpointAddress(adfsBaseAddress + endpointSuffix))
{
TrustVersion = TrustVersion.WSTrust13
};
trustChannelFactory.Credentials.UserName.UserName = username;
trustChannelFactory.Credentials.UserName.Password = password;
var tokenClient = (WSTrustChannel)trustChannelFactory.CreateChannel();
var rst = new RequestSecurityToken(RequestTypes.Issue, KeyTypes.Symmetric);
var token = tokenClient.Issue(rst);
// do some token-related stuff
return token.Id;
}
}
When I try to run it (GET call from browser since it's configured with web http binding for this endpoint) I get the following exception:
System.ServiceModel.Security.MessageSecurityException - "An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail."
with inner exception:
System.ServiceModel.FaultException - "An error occurred when verifying security for the message."
I guess it's related with the response signature or certificate but I have no idea how to overcome this since I'm quite new in WIF.
I've managed to (partly) solve this issue. I've changes few things in my code, but the problems seems to be related with:
STS endpoint -should be /trust/13/usernamemixed for this type of authentication
RST key type - when I've set it Bearer it started returning a SAML token
Here is my most recent version:
public class MobileAuthService : IMobileAuthService
{
private const string stsEndpointAddress = #"https://<my_adfs_hostname>/adfs/services/trust/13/usernamemixed";
private const string relyingPartyAddress =
"https://<my_service_addr>/Auth.svc";
public string AuthenticateUser(string username, string password)
{
var binding = new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential)
{
ClientCredentialType = HttpClientCredentialType.None
};
var trustChannelFactory = new WSTrustChannelFactory(binding, new EndpointAddress(stsEndpointAddress))
{
TrustVersion = TrustVersion.WSTrust13
};
var channelCredentials = trustChannelFactory.Credentials;
channelCredentials.UserName.UserName = username;
channelCredentials.UserName.Password = password;
channelCredentials.SupportInteractive = false;
var tokenClient = (WSTrustChannel)trustChannelFactory.CreateChannel();
var rst = new RequestSecurityToken(RequestTypes.Issue, KeyTypes.Bearer)
{
AppliesTo = new EndpointReference(relyingPartyAddress),
ReplyTo = relyingPartyAddress,
TokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0"
};
// to some token-related stuff (like transformations etc...)
}
}
I hope this will help people who ends up with similar problem.

Categories