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

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

问题描述

我写的是使用API​​的一些数据,应用程序将需要和使用的API,需要用户名和密码来拉一款Windows Phone 8应用。

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?

只是要清楚,用户将不再需要自己的用户名或密码,这个通用1应工作。

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天全站免登陆