我正在编写一个 Windows Phone 8 应用程序,该应用程序使用 API 来提取该应用程序需要的一些数据,并且要使用该 api,需要用户名和密码。我已获得此用户名和密码,它似乎可以工作,但是我想知道在应用程序中使用此用户名和密码的正确方法是什么?
我可以简单地添加如下内容吗:
string userName = "username";
string passWord = "password";
然后将它们传递到WebRequest
需要的时候?或者我应该通过某种特殊方式将此信息存储在应用程序中?
需要明确的是,用户不需要自己的用户名或密码,这个通用的应该可以使用。
您可以对隔离存储中的数据进行加密。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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)