简体   繁体   English

通过PHP会话进行服务器认证

[英]Server authentication via PHP session

I have a little question about possibilities to change authentication on my C# server from Login\\Password to PHP session. 关于将我的C#服务器上的身份验证从Login \\ Password更改为PHP会话的可能性,我有一个小问题。 I am using PHP, HTML5, and JavaScript as client side, and C# server with WebSockets lib as server side. 我使用PHP,HTML5和JavaScript作为客户端,使用WebSockets lib作为服务器端的C#服务器。 I already have authorization code, but it works only with login and password. 我已经拥有授权码,但它只适用于登录名和密码。

Well, I have the same authentication code, that checks login & password via PHP. 好吧,我有相同的身份验证代码,通过PHP检查登录名和密码。 But how I can authorize client on C# server, by using WebSockets with PHP session, instead account login & password? 但是我如何在C#服务器上授权客户端,使用带有PHP会话的WebSockets,而不是帐户登录和密码?

Little pic: 小图片:

Is this possible? 这可能吗? And any piece of code, demo, etc. for it? 还有任何代码,演示等等吗?

Genreate an access token that your c# server can validate. 创建您的c#服务器可以验证的访问令牌。 This is how i would implement it: 这是我如何实现它:

share a secret between your PHP and C# server (eg 64 randcom bytes) 在PHP和C#服务器之间共享一个秘密(例如64个randcom字节)

When PHP validates the user/login, generate a token: it contains random characters, the user ID and a signature that was created using the shared secret. 当PHP验证用户/登录时,生成一个令牌:它包含随机字符,用户ID和使用共享密钥创建的签名。 Eg something like this: 像这样的事情:

$random = base64_encode(mcrypt_create_iv(32));
$token = $random . '_' . $userID . '_' . hash_hmac('sha256', $random . '_' . $userID, SHARED_SECRET);

The C# server can then verify this token by calcuating the HMAC signature from $random and $token with the shared secret. 然后,C#服务器可以通过使用共享密钥从$ random和$ token计算HMAC签名来验证此令牌。 If correct, the C# server can work with the $userID and the MySQL databse. 如果正确,C#服务器可以使用$ userID和MySQL数据库。

As long as you pass the token to your JS code via https, you should be secure. 只要您通过https将令牌传递给JS代码,您就应该是安全的。

PS: You can improve the security of this scheme by adding an expiration date to the token and make your JS code request a new token in time. PS:您可以通过向令牌添加到期日期来提高此方案的安全性,并使您的JS代码及时请求新令牌。 Dont forget to include the expiry in the signature. 别忘了在签名中包含到期日。

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

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