c# - 在 winforms 应用程序中使用 PHP webservice

标签 c# php

目前我将我的 C# mysql 连接信息存储在类文件本身中,这看起来并不聪明,因为最终用户可以简单地使用像 NET Reflector 这样的反射器来调试源代码,以防它被混淆。

现在 stackoverflow 上的用户建议创建一个将操纵数据库的 Web 服务。最终用户将使用的软件然后使用用户的凭据简单地通过 Web 服务验证自己,然后使用它来访问资源。

现在我有以下问题,我的服务器在 linux ubuntu 上运行并且已经存储了一个使用 plesk 创建的网站。

我知道我可以使用 http://www.mono-project.com/在 Linux 上托管 Web 服务。但我从来没有这样做过,因为我一直使用 PHP 来做这样的事情,而且我对如何将 c# 网络服务上传到 ssh 服务器上安装的单声道版本感到有点困惑。


我可以在我的 winforms 应用程序中使用类似于以下 PHP 代码的 C# 网络服务吗?

PHP代码:

<?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" }

最佳答案

Rômulo M. Farias 的回答非常准确。

但是,您可能需要更多的解释,第一次“手动”做一些事情可能会有所帮助,这样您就可以了解当您使用框架为你。所以我让我的答案保持不变:

...

看起来您的基本结构是正确的(MySQL 凭据和访问在服务器端处理)。

您没有理由需要在您的服务器上运行任何 C#,只是因为您的客户端应用程序 WinForms。 Web 服务的全部意义在于允许不同的平台轻松地相互对话。

网络服务可以被认为是一个返回数据而不是 HTML 的网站。 (实际上恰恰相反:网站或网页只是一种特定类型的返回 html 的网络服务)

因此,您现在所需要做的就是让您的 WinFroms 应用程序能够通过网络服务与您在上面发布的 PHP 代码进行对话。换句话说,我们希望将您获得的 PHP 代码包装在 Web 服务中,然后让您的 WinForms 应用程序使用该 Web 服务。

请记住,为了安全起见,您必须确保您使用的是 SSL 并执行 POST,而不是 GET(即密码必须在消息正文中加密,而不是粘贴在 URL 上!)

这里有一些关于创建 PHP 网络服务的教程(必须有更好的): https://davidwalsh.name/web-service-php-mysql-xml-json https://www.codeproject.com/Tips/671437/Creating-Web-Service-Using-PHP-Within-Minutes

有很多方法可以从 C# 中使用 Web 服务。 RestSharp 可能是理想的选择。

请注意,您可能会选择 SOAP 或 JSON 作为格式。因此,寻找使用相同格式的网络服务教程和消费者教程。 (构建一个可以根据客户端请求返回不同格式的 Web 服务是理想的,但更高级一些。我没有 PHP 中的教程)

(在此上下文中,“API”与“网络服务”同义,但请注意,“API”也可以具有完全不同的含义)

看起来你还有很长的路要走,但别担心,一旦你的第一个例子开始工作,使用相同的方法来做各种很棒的东西。

关于c# - 在 winforms 应用程序中使用 PHP webservice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41408702/

相关文章:

c# - 在 WPF 中,我如何实现 ICommandSource 以使我的自定义控件能够使用来自 xaml 的命令?

php - Jquery Qtip 的奇怪问题,第一次鼠标悬停后工具提示无法正常工作

c# - 我是 .NET 的新手。我对理解术语有一些疑问。.NET Core 和 .NET 5 之间有什么区别?

c# - 尝试使用 C# 将日期(不带时间)插入 .DBF(Visual Fox Pro 数据库)

c# - ServerManager CommitChanges 稍有延迟地进行更改

php - Mysqli 绑定(bind) - 未知数量的参数

php - PDO 发出带有 LIMIT 的语法错误

c# - 使用数据绑定(bind)在 WPF MVVM 中的 ListView(或更好的东西!)中显示图像

php - iOS 存折后栏中的特殊字符

php - MySQL数据库目标机主动拒绝连接