【问题标题】:Username and Password data Windows phone 8 app用户名和密码数据 Windows phone 8 应用程序
【发布时间】:2014-01-09 06:02:36
【问题描述】:

我正在编写一个 Windows phone 8 应用程序,该应用程序使用 API 来提取应用程序需要的一些数据并使用 api,需要用户名和密码。我已经获得了这个用户名和密码,它似乎可以工作,但是我想知道在应用程序中使用它的正确方法是什么?

我可以简单地添加类似的内容:

string userName = "username";
string passWord = "password";

然后在需要时将它们传递给WebRequest?或者有什么特殊的方式我应该将这些信息存储在应用程序中?

需要明确的是,用户不需要自己的用户名或密码,这个通用的应该可以工作。

【问题讨论】:

    标签: c# windows-phone-8


    【解决方案1】:

    您可以加密隔离存储中的数据。 Here是教程

    如果此处的链接出现故障,则该应用程序的代码会写入和读取秘密 PIN。

    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Text;
    using System.Security.Cryptography;
    
    private string FilePath = "pinfile";
    
    private void BtnStore_Click(object sender, RoutedEventArgs e)
    {
        // Convert the PIN to a byte[].
        byte[] PinByte = Encoding.UTF8.GetBytes(TBPin.Text);
    
        // Encrypt the PIN by using the Protect() method.
        byte[] ProtectedPinByte = ProtectedData.Protect(PinByte, null);
    
        // Store the encrypted PIN in isolated storage.
        this.WritePinToFile(ProtectedPinByte);
    
        TBPin.Text = "";
    }
    
    private void WritePinToFile(byte[] pinData)
    {
        // Create a file in the application's isolated storage.
        IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream writestream = new IsolatedStorageFileStream(FilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, file);
    
        // Write pinData to the file.
        Stream writer = new StreamWriter(writestream).BaseStream;
        writer.Write(pinData, 0, pinData.Length);
        writer.Close();
        writestream.Close();
    }
    
    private void BtnRetrieve_Click(object sender, RoutedEventArgs e)
    {
        // Retrieve the PIN from isolated storage.
        byte[] ProtectedPinByte = this.ReadPinFromFile();
    
        // Decrypt the PIN by using the Unprotect method.
        byte[] PinByte = ProtectedData.Unprotect(ProtectedPinByte, null);
    
        // Convert the PIN from byte to string and display it in the text box.
        TBPin.Text = Encoding.UTF8.GetString(PinByte, 0, PinByte.Length);
    
    }
    
    private byte[] ReadPinFromFile()
    {
        // Access the file in the application's isolated storage.
        IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream readstream = new IsolatedStorageFileStream(FilePath, System.IO.FileMode.Open, FileAccess.Read, file);
    
        // Read the PIN from the file.
        Stream reader =  new StreamReader(readstream).BaseStream;
        byte[] pinArray = new byte[reader.Length];
    
        reader.Read(pinArray, 0, pinArray.Length);
        reader.Close();
        readstream.Close();
    
        return pinArray;
    }
    

    【讨论】:

    • 感谢您的快速回复。我不是在构建 API,我只是被开发它的第 3 方授予访问它的权限。
    • 那么静态和只读是我唯一的建议。否则你很高兴。
    • 谢谢。我需要加密用户名和密码还是将其保存在 IsolateStorage 中?它没有访问任何机密信息,但我想这只是我不确定的最佳做法。
    • 那是个好主意,我不知道 WP8 有内置的。
    【解决方案2】:

    我不知道有任何问题需要您将代码样式设置为任何不同的正常形式

    WebRequest request = WebRequest.Create("http://SomeProtectedUrl.com");
    request.Credentials = new System.Net.NetworkCredential("username", "password");
    

    将您的用户名和密码保存在一个位置,以便您可以轻松更改使用

    public static string uname = "yourUsername";
    public static string passwd= "yourPassword";
    
    request.Credentials = new System.Net.NetworkCredential(uname, passwd);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      相关资源
      最近更新 更多