C# .Net Framework WPF:如何进行 POST 调用并保存登录 Baerer 密钥?

Posted

技术标签:

【中文标题】C# .Net Framework WPF:如何进行 POST 调用并保存登录 Baerer 密钥?【英文标题】:C# .Net Framework WPF: How can I do POST call and save login Baerer key? 【发布时间】:2022-01-16 00:44:13 【问题描述】:

我在 C# WPF 项目中使用了以下 POST 请求:https://***.com/a/8091963/17283265。所以我正在获取用户登录 Bearer id,我想保存它(我使用 localstorage 来保存它,就像 reactjs 中的 redux 一样)。相同的页面需要进行身份验证。我没有找到一个好的解决方案。这是我的代码。

            using (var wb = new WebClient())
            
                string Useremail = Login_email.Text;
                string Userpassword = Pass_password.Text;

                var data = new NameValueCollection();
                data["application_id"] = "22233sasdsa1123asdasxzczx";
                data["email"] = Useremail;
                data["password"] = Userpassword;

                var response = wb.UploadValues("https://example.com/user/api/logins?", "POST", data);
                string responseInString = Encoding.UTF8.GetString(response);
                test.Content = responseInString;
            

我正在收到来自服务器的响应。 API POST 没有问题。

【问题讨论】:

【参考方案1】:

您可以序列化您的回复并将其保存为文本文件。 它可以在任何时候或在您的应用程序中访问。 这是一个简单的演示,它只是从 Bing 的首页收集 html。 html 源代码现在已序列化并保存到示例应用程序可以在任何地方访问的文件中。

确保您的程序在部署时无需提升权限即可读取和写入文件(AppData 文件夹等)。本地测试没问题。以下是您可以放入新控制台应用程序以进行测试的代码。

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;



   namespace SerializeObject

    class Program
    
        static void Main(string[] args)
        
            //ensure you delete StorageFile.txt if you encounter errors or change structure of MyObject 
            GetInternetString("http://www.bing.com");
        

        private static void GetInternetString(string url)
        
            using (var wb = new WebClient())
            
                var response = wb.DownloadString(url);
                Serialize(response);
            
        

        private static void Serialize(string webPageContent)
        
            try
            
                MyObject saveThis = new MyObject(webPageContent);
                IFormatter formatter = new BinaryFormatter();
                using (Stream stream = new FileStream("StorageFile.txt", FileMode.Append, FileAccess.Write))
                
                    formatter.Serialize(stream, saveThis);
                    stream.Close();
                

                Deserialize();
            
            catch (Exception ex)
            
                Console.WriteLine(ex.Message + "\r" + ex.StackTrace);
            
        

        private static void Deserialize()
        
            try
            
                //change this to your specific use
                var lstSessionProjectFilesTrimsSaveThis = new List<MyObject>();
                using (var fileStream = new FileStream("StorageFile.txt", FileMode.Open))
                
                    var bFormatter = new BinaryFormatter();
                    while (fileStream.Position != fileStream.Length)
                    
                        //change this to your specific use
                        lstSessionProjectFilesTrimsSaveThis.Add((MyObject)bFormatter.Deserialize(fileStream));
                    
                

                foreach (var VARIABLE in lstSessionProjectFilesTrimsSaveThis)
                
                    Console.WriteLine(VARIABLE.WebPageContent);
                

                Console.ReadLine();
            
            catch (Exception ex)
            
                Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
            
        



[Serializable] //mandatory to have this declaration
    public class MyObject
    
        public MyObject(string webPageContent)
        
            WebPageContent = webPageContent;
        

        public string WebPageContent  get; set; 
    


【讨论】:

不应同时使用WebClientBinaryFormatter,尤其是后者,因为它会带来安全风险。

以上是关于C# .Net Framework WPF:如何进行 POST 调用并保存登录 Baerer 密钥?的主要内容,如果未能解决你的问题,请参考以下文章

.net core 和 WPF 开发升讯威在线客服系统:把 .Net Framework 打包进安装程序

.net core 和 WPF 开发升讯威在线客服系统:把 .Net Framework 打包进安装程序

.net core 和 WPF 开发升讯威在线客服系统:把 .Net Framework 打包进安装程序

如何使用 .NET、C# 和 WPF 检查 Internet 连接

如何查看c#的版本.net framework

C# WPF Entity Framework 6 同步数据库