我有下面的类将用于 xamarin.forms 移动应用程序
检索由 OAuth(webapi) 生成的 token 。一旦生成,我需要存储
在一个我可以再次访问它并且不会一直生成它的地方。
将它存储在 Pcl 中的最佳位置在哪里?我也希望能够删除一次
用户注销。
class LoginService
{
public async Task Login(string username, string password)
{
HttpWebRequest request = new HttpWebRequest(new Uri(String.Format("{0}Token", Constants.BaseAddress)));
request.Method = "POST";
string postString = String.Format("username={0}&password={1}&grant_type=password",
HttpUtility.HtmlEncode(username), HttpUtility.HtmlEncode(password));
byte[] bytes = Encoding.UTF8.GetBytes(postString);
using (Stream requestStream = await request.GetRequestStreamAsync())
{
requestStream.Write(bytes, 0, bytes.Length);
}
try
{
HttpWebResponse httpResponse = (HttpWebResponse)(await request.GetResponseAsync());
string json;
using (Stream responseStream = httpResponse.GetResponseStream())
{
json = new StreamReader(responseStream).ReadToEnd();
}
TokenResponseModel tokenResponse = JsonConvert.DeserializeObject(json);
return tokenResponse.AccessToken;
}
catch (Exception ex)
{
throw new SecurityException("Bad credentials", ex);
}
}
}
最佳答案
表单有一个内置的 Properties您可以在其中存储少量持久数据的字典。
Application.Current.Properties ["token"] = myToken;
关于xamarin.forms - 在 xamarin.form 应用程序中存储 token 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46004200/