简体   繁体   English

在 URL 参数中传递用户名和密码 (Dart & Flutter)

[英]Pass the username and password in URL parameters (Dart & Flutter)

I have a flutter app and I'm using back4app.com and Parse RESTful api to register my users, I have read their docs about logging in users but I dont know how to pass my username and password in URL parameters as JSON encoded :我有一个 flutter 应用程序,我正在使用 back4app.com 和 Parse RESTful api 来注册我的用户,我已经阅读了他们关于登录用户的文档,但我不知道如何将我的用户名和密码作为 JSON 编码在 URL 参数中传递:

I tried this method:我试过这个方法:

Future <void>loginMethod(String username,String password) async {

var url = Uri.parse('https://myshoppingapp.b4a.io/login/$username:$password');


final response = await  http.get(url,  headers: {
          'X-Parse-Application-Id': kParseApplicationId,
          'X-Parse-REST-API-Key': kParseRestApiKey,
          'Content-Type': 'application/json'
          
          
          },);


final exData = jsonDecode(response.body);

print(exData);

but I've got some errors但我有一些错误

Remember one thing记住一件事

Don't use the GET method while sending your personal data to the server.在将您的个人数据发送到服务器时不要使用GET方法。 GET method data is sent data to the server followed by the URL like append with URL request which will be seen to everyone like below. GET方法数据将数据发送到服务器,后跟URL类似附加 URL 请求,每个人都会看到如下所示。

var url = Uri.parse('https://myshoppingapp.b4a.io/login/$username:$password');

This is how your personal data can be readable from a URL in a GET Method.这就是可以从GET方法中的 URL 读取您的个人数据的方式。

'https://myshoppingapp.b4a.io/login/Mehran@metra.org:abcd12345'

For login requests, we should use the POST method.对于登录请求,我们应该使用POST方法。 Because our login data is secure which needs security.因为我们的登录数据是安全的,需要安全。 When using the POST method the data is sent to the server in a bundle.使用POST方法时,数据以包的形式发送到服务器。

 Future loginMethod(String username,String password) async {
       var res = await http.post(Uri.parse('https://myshoppingapp.b4a.io/login/'),
            body: {"username": username, "password": password});
        print('res : ${res.body}');
       
        
if (res.statusCode == 200){ final exData = jsonDecode(res.body);
    
    print(exData);
return res.body;
} else{
final exData = jsonDecode(res.body);
    
    print(exData);
return res.statusCode;
}
        
   }

for HTTP basic authentication用于 HTTP 基本身份验证

final loginUrl = Uri(scheme: 'https', host: 'example.com', port: 8080, userInfo: 'username:password')
http.get(loginUrl)

but pass username and password via url is not recommended cause it's not safe.但不建议通过 url 传递用户名和密码,因为它不安全。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#access_using_credentials_in_the_url https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#access_using_credentials_in_the_url

so you should do it by using post formdata.所以你应该使用post formdata来做到这一点。

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

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