目前我将C#mysql连接信息存储在类文件本身中,这看起来并不聪明,因为最终用户可以简单地使用像.NET Reflector这样的反射器来调试源代码,以防它没有被模糊 .
现在,stackoverflow上的用户建议创建一个将操纵数据库的Web服务 . 最终用户将使用的软件然后使用用户的凭据简单地使用Web服务对自身进行身份验证,然后使用它来访问资源 .
现在我有以下问题,我的服务器在linux ubuntu上运行,并且已经存储了一个使用plesk创建的网站 .
我知道我可以使用http://www.mono-project.com/在linux上托管web服务 . 但是我总是用PHP来做这些事情,而且我对如何将s#web-service上传到ssh服务器上安装的单声道版本感到困惑 .
我可以在winforms应用程序中使用类似下面的PHP代码作为C#Web服务吗?
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));
}
}
?>
或者如何创建可以在我的应用程序中使用的PHP Web服务?
PHP脚本的当前响应如下所示:
{ "username": "dane", "authentificated": "true", "firstRun": "false", "firstRunUpdated": "false", "uniqueIdCheck": "true", "Premium": "true", "Disabled": "false", "updateRequired": "false" }