我正在开发 Windows Phone 8 应用程序,并且在第一次运行时我让用户从他的 Microsoft 帐户进行身份验证。用户凭据非常节省,但每次我运行应用程序时都会出现权限屏幕(“让此应用程序访问您的信息”)。
如何仅保存一次选择并显示权限屏幕(第一次运行应用程序)?
代码片段:
private LiveConnectSession _session;
private async Task<Users> Authenticate()
{
var liveIdClient = new LiveAuthClient("XXXXXXXXXXXXXXX");
while (_session == null)
{
var result = await liveIdClient.LoginAsync(new[]
{
"wl.signin",
"wl.offline_access"
});
if (result.Status == LiveConnectSessionStatus.Connected)
{
_session = result.Session;
var client = new LiveConnectClient(result.Session);
var meResult = await client.GetAsync("me");
var user = await App.MobileService.LoginAsync(result.Session.AuthenticationToken);
return new Users
{
UserName = user.UserId,
RealName = string.Format("{0} {1}", meResult.Result["first_name"], meResult.Result["last_name"]),
TimeStamp = DateTime.Now,
IsAuthorised = false
};
}
else
{
_session = null;
MessageBox.Show("You must log in.", "Login Required", MessageBoxButton.OK);
}
}
return null;
}
在构造函数(public MainPage())中:
(DataContext as MainViewModel).User = await Authenticate();