用户名和密码数据 Windows phone 8 应用程序 [英] Username and Password data Windows phone 8 app

查看:19
本文介绍了用户名和密码数据 Windows phone 8 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a Windows phone 8 app that is using an API to pull in some data that the app will need and to use the api, a username and password is required. I have been supplied with this username and password and it seems to work, however I am wondering what is the correct way to use this with an app?

我可以简单地添加如下内容:

Can I simply add something like:

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

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

And then just pass those into the WebRequest when needed? Or is there some special way I should store this information in the app?

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

Just to be clear, the users won't need their own Username or password, this generic one should work.

推荐答案

您可以加密隔离存储中的数据.这里教程

You can encrypt data in isolated storage. Here is a tutorial

万一链接断开,这里是一个应用程序的代码,它可以写入和读取一个秘密 PIN.

In case the link ever goes down here is the code for an app that both writes and reads a secret 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;
}

这篇关于用户名和密码数据 Windows phone 8 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆