Currently I'm storing my C# mysql connection informations inside the class file itself, which doesn't seem that smart, since end users could simply use a reflector like NET Reflector to debug the source code in case it's not obfruscated.
Now a user on stackoverflow recommendet to create a web service which will manipulate the database.The software that the end-user would use then simply authenticates itself with the web service using the user's credentials and then uses that to access resources.
Now I have the following problem, My server is running on linux ubuntu and already stores a website which was created using plesk.
I know that I could use http://www.mono-project.com/ to host a webservice on linux. But I've never done that since I've always used PHP to do such things and I've got kinda confused on how to upload a c# web-service to the installed mono version on the ssh server.
Could I use something like the following PHP code as a C# web service in my winforms application?
PHP code:
<?php
class webService extends database
{
// Web service constructor
public function __construct($username, $password, $uniqueId, $versionId)
{
$this->username = $username;
$this->password = $password;
$this->salt = 'xxx';
$this->hash = 'xxx';
$this->uniqueId = $uniqueid;
$this->versionId = $versionId;
}
// Web service functions to check database values
// Web service user account check function
private function userCheck()
{
$this->connect();
$userCheck = $this->execute_query("SELECT username, password FROM Users WHERE username = '" . $this->username . "' AND password = '" . $this->hash . "'");
if($userCheck && mysqli_num_rows($userCheck) > 0)
{
return 'true';
}
else
{
return 'false';
}
}
// Web service unique id check function
private function uniqueCheck()
{
$this->connect();
$uniqueCheck = $this->execute_query("SELECT username, uniqueid FROM Users WHERE username = '" . $this->username . "' AND uniqueid = '" . $this->uniqueId . "'");
if($uniqueCheck && mysqli_num_rows($uniqueCheck) > 0)
{
return 'true';
}
else
{
return 'false';
}
}
// Web service first run check function
private function firstRunCheck()
{
$this->connect();
$firstRunCheck = $this->execute_query("SELECT username, firstrun FROM Users WHERE username = '" . $this->username . "' AND firstrun = '0'");
if($firstRunCheck && mysqli_num_rows($firstRunCheck) > 0)
{
return 'true';
}
else
{
return 'false';
}
}
// Web service user disabled check function
private function disabledUserCheck()
{
$this->connect();
$disabledUserCheck = $this->execute_query("SELECT disabled FROM Users WHERE username = '" . $this->username . "' AND disabled = '1'");
if($disabledUserCheck && mysqli_num_rows($disabledUserCheck) > 0)
{
return 'true';
}
else
{
return 'false';
}
}
// Web service update required check function
private function updateRequiredCheck()
{
$this->connect();
$updateRequiredCheck = $this->execute_query("SELECT requiredupdate FROM requiredupdate WHERE version = '" . $this->versionId . "' AND requiredupdate = 1");
if($updateRequiredCheck && mysqli_num_rows($updateRequiredCheck) > 0)
{
return 'true';
}
else
{
return 'false';
}
}
// Web service premium check function
private function userPremiumCheck()
{
$this->connect();
$userPremiumCheck = $this->execute_query("SELECT premium FROM Users WHERE username = '" . $this->username . "' AND premium = 1");
if($userPremiumCheck && mysqli_num_rows($userPremiumCheck) > 0)
{
return 'true';
}
else
{
return 'false';
}
}
// Web service functions to update database values
// Web service update first run parameters function
private function firstRunUpdate()
{
$firstRunCheck = $this->firstRunCheck();
if($firstRunCheck == 'true')
{
$this->connect();
$this->execute_query("UPDATE Users SET uniqueid = '" . $this->uniqueId . "', firstrun = '1' WHERE username = '" . $this->username . "'");
return 'true';
}
else
{
return 'false';
}
}
function to_xml(SimpleXMLElement $object, array $data)
{
foreach ($data as $key => $value) {
if (is_array($value)) {
$new_object = $object->addChild($key);
to_xml($new_object, $value);
} else {
$object->addChild($key, $value);
}
}
}
// Web service handler function
public function webService()
{
$userCheck = $this->userCheck();
if($userCheck == 'true')
{
$userArray = array (
'username' => $this->username,
'authentificated' => $this->userCheck(),
'firstRun' => $this->firstRunCheck(),
'firstRunUpdated' => $this->firstRunUpdate(),
'uniqueIdCheck' => $this->uniqueCheck(),
'Premium' => $this->userPremiumCheck(),
'Disabled' => $this->disabledUserCheck(),
'updateRequired' => $this->updateRequiredCheck()
);
}
else
{
$userArray = array (
'username' => $this->username,
'userCheck' => $this->userCheck()
);
}
echo str_replace("\/", "/", json_encode($userArray, JSON_PRETTY_PRINT));
}
}
?>
Or how is a PHP Web-Service created which could get used in my application?
The current response from the PHP script would look like the following:
{ "username": "dane", "authentificated": "true", "firstRun": "false", "firstRunUpdated": "false", "uniqueIdCheck": "true", "Premium": "true", "Disabled": "false", "updateRequired": "false" }