简体   繁体   English

通过PHP REST api使用C#进行安全身份验证和请求

[英]Secure authentication and requests with C# through a PHP REST api

Introduction 介绍

Okay so I've got a website (PHP) with a database (MySQL). 好的,所以我有一个带有数据库(MySQL)的网站(PHP)。 You can create an account on this website and edit your details and so on (let's say date of birth, real name, address, and so on). 您可以在此网站上创建一个帐户并编辑您的详细信息等(让我们说出生日期,真实姓名,地址等)。

Now, what I wanna do is to create a desktop application (most likely c# with WPF) that interacts with a REST api (in PHP) from the website, which will allow to : 现在,我想做的是创建一个桌面应用程序(很可能是带有WPF的c#),它与网站上的REST api(用PHP)交互,这将允许:

  • Create accounts directly from the application 直接从应用程序创建帐户
  • Log in to your account 登录到您的帐户
  • Be able to edit your details directly from the application 能够直接从应用程序编辑您的详细信息

As of now I only want to do a desktop application, but it might evolve into a mobile app. 截至目前,我只想做一个桌面应用程序,但它可能会演变成一个移动应用程序。 I don't think that is relevant though. 我不认为这是相关的。

Creating accounts and logging in 创建帐户并登录

I'm struggling with the create account / log in part. 我正在努力使用创建帐户/日志部分。 I want it to be as secure as possible. 我希望它尽可能安全。 On the website, I'm using password_hash(PASSWORD_BCRYPT) with a cost of 10 to store passwords in the database, and password-verify to check log ins. 在网站上,我使用password_hash(PASSWORD_BCRYPT),成本为10,在数据库中存储密码,密码验证,以检查登录。

Regarding the application, I imagine the way to go is to pass the username and password to the REST api and do the encryption directly on the server side. 关于应用程序,我想要的方法是将用户名和密码传递给REST API,并直接在服务器端进行加密。 But sending the password itself sounds terrible so I thought about encrypting it with a secret key that only the application and the website know, so the website can figure out the password and encode it correctly in the database. 但是发送密码本身听起来很糟糕所以我考虑用只有应用程序和网站知道的密钥加密它,所以网站可以找出密码并在数据库中正确编码。

If I'm right, what encryption algorithm should I use? 如果我是对的,我应该使用什么加密算法? If not, how should I do that? 如果没有,我该怎么做?

Edit your details directly from the application 直接从应用程序编辑您的详细信息

I'm thinking about doing the following : Let's say the user has logged in through the application. 我正在考虑执行以下操作:假设用户已通过该应用程序登录。 If the authentication is successful, the server randomly generates a token (I'll probably use ircmaxell's Random-Lib ) and send it as a reply to the application. 如果身份验证成功,服务器会随机生成一个令牌(我可能会使用ircmaxell的Random-Lib )并将其作为对应用程序的回复发送。 From then onward, when the application wants to make a request to the api, it adds the username to the datas string, and generates a hash (sha256 for example) with the data string plus the token; 从那时起,当应用程序想要向api发出请求时,它会将用户名添加到数据字符串中,并使用数据字符串和令牌生成散列(例如sha256); then the server can repeat the exact process using the token stored in database to make sure the user actually has the right to access/modify his details, and nobody is trying to pretend to be an user he isn't. 然后服务器可以使用存储在数据库中的令牌重复确切的过程,以确保用户实际上有权访问/修改他的详细信息,并且没有人试图伪装成他不是的用户。

I feel like the problem is, if someone listens to the first api reply after the authentication, he gets the token and can use it to act as if he was the user. 我觉得问题是,如果有人在认证后听了第一个api回复,他就会得到令牌并可以用它来表现就好像他是用户一样。 Would encrypting it with the same process used for the password be enough to make sure this can't happen? 使用与密码相同的过程加密它是否足以确保不会发生这种情况?

Last but not least, if I want the user to be able to be directly logged in the next time he starts the application, I guess I have to give the token a permanent durability and store it in a file or something on the computer. 最后但并非最不重要的是,如果我希望用户能够在下次启动应用程序时直接登录,我想我必须给予令牌永久的持久性并将其存储在计算机上的文件或其他内容中。 But that doesn't sound really safe because anyone could just read the file content and figure out the token. 但这听起来并不安全,因为任何人都可以只读取文件内容并找出令牌。

Final questions 最后的问题

So. 所以。 What do you think ? 你怎么看 ? Does it sound good or am I completely off the tracks? 这听起来不错还是我完全脱离了轨道?

The website hosting is in http://* so I guess using HTTPS communications is not an option here. 网站托管在http:// *所以我想这里不能使用HTTPS通信。 I know that's a big problem to create something really secured, but I'd still like to make something as safe as possible with what I have. 我知道创造一些真正安全的东西是一个大问题,但我仍然希望用我所拥有的东西尽可能安全。

Thanks a lot for your advices. 非常感谢您的建议。 :) :)

Btw I tried to make this as clear as possible, hope it worked. 顺便说一下,我试图尽可能清楚,希望它有效。 I have close to pretty little knowledge of security, encryption, ... so you'll have to speak like to a 3 years old to me. 我几乎没有关于安全性,加密知识的知识......所以你必须对我这么说3岁。

I think the first, most important piece of advice I could give is to never try to roll your own security code, unless you are an absolute expert. 我认为我可以给出的第一个也是最重要的建议是永远不要尝试推出自己的安全代码,除非你是绝对的专家。 You'll want to put together a solution based on the frameworks that .net provides for you. 您将希望根据.net为您提供的框架制定解决方案。

First things first - your REST API. 首先要做的事 - 你的REST API。 I'd suggest building on top of ASP.NET Web API. 我建议在ASP.NET Web API之上构建。 HTTPS is mandatory here - if your hosting provider can't give it to you, you need a new hosting provider. 此处必须使用HTTPS - 如果您的托管服务提供商无法提供给您,则需要新的托管服务提供商。 It also conveniently takes care of encryption for you. 它还可以方便地为您加密。

There are various security options available for ASP, I'd read this for a detailed overview: https://docs.asp.net/en/latest/security/ . 有各种可用于ASP的安全选项,我在这里阅读详细的概述: https//docs.asp.net/en/latest/security/ Using the existing ASP options will also take care of your requirements around user account creation and self-service. 使用现有的ASP选项还将满足您关于创建用户帐户和自助服务的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM