C# 使用Windows.Security.authentication从成功的身份验证中清空数据
我正在将我的应用程序从windows 8.1升级到windows 10 UWP。以前,我使用Windows.Live API获取有关用户的信息,包括传递给MobileService.LoginAsync方法的令牌。为了使用windows.security.authentication,这一切都不得不放弃。不幸的是,在使用下面的代码之后,成功的SSO令牌请求会导致一个对象丢失我需要的大部分数据。现在,只要有一个用户名就好了,我可以从那里扩展。下面是我遇到问题的代码,在if语句末尾设置的断点显示空的account.username字段C# 使用Windows.Security.authentication从成功的身份验证中清空数据,c#,azure,uwp,C#,Azure,Uwp,我正在将我的应用程序从windows 8.1升级到windows 10 UWP。以前,我使用Windows.Live API获取有关用户的信息,包括传递给MobileService.LoginAsync方法的令牌。为了使用windows.security.authentication,这一切都不得不放弃。不幸的是,在使用下面的代码之后,成功的SSO令牌请求会导致一个对象丢失我需要的大部分数据。现在,只要有一个用户名就好了,我可以从那里扩展。下面是我遇到问题的代码,在if语句末尾设置的断点显示空的
string accessToken = "";
WebAccountProvider wap = await WebAuthenticationCoreManager.FindAccountProviderAsync("https://login.microsoft.com", "consumers");
WebTokenRequest wt = new WebTokenRequest(wap,"wl.signin", "none");
WebAccount account;
WebTokenRequestResult wtrr = await WebAuthenticationCoreManager.RequestTokenAsync(wt);
if (wtrr.ResponseStatus == WebTokenRequestStatus.Success)
{
accessToken = wtrr.ResponseData[0].Token;
account = wtrr.ResponseData[0].WebAccount;
var properties = wtrr.ResponseData[0].Properties;
}
现在,只要有一个用户名就好了
如果要使用WebAuthenticationCoreManager
获取用户名。您可以尝试使用以下代码:
private async void GetMsaTokenAsync(WebAccountProviderCommand command)
{
WebTokenRequest request = new WebTokenRequest(command.WebAccountProvider, "wl.basic");
WebTokenRequestResult result = await WebAuthenticationCoreManager.RequestTokenAsync(request);
if (result.ResponseStatus == WebTokenRequestStatus.Success)
{
string token = result.ResponseData[0].Token;
var restApi = new Uri(@"https://apis.live.net/v5.0/me?access_token=" + token);
using (var client = new HttpClient())
{
var infoResult = await client.GetAsync(restApi);
string content = await infoResult.Content.ReadAsStringAsync();
var jsonObject = JsonObject.Parse(content);
string username = jsonObject["name"].GetString();
}
}
}
使用WebAccount.UserName
属性可能无法获取用户名。但是一旦您拥有了访问令牌,就可以使用它调用Microsoft Live API来获取有关用户的基本信息。有关更多信息,请参阅中的
此外,当与MobileService.LoginWithMicrosoftAccountAsync(accessToken)一起使用时,从该响应接收的令牌返回“unauthorized” 这是因为上面的令牌是一个访问令牌。但是,需要一个身份验证令牌 参数 这个客户 类型:
要登录的客户端 身份验证令牌 类型:
Live SDK会话身份验证令牌 如果要使用Microsoft帐户对用户进行身份验证,可以通过如下调用方式使用服务导向身份验证:
var user = await MobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);
有关详细信息,请参阅。您是否尝试检查
响应数据[0]的值。属性
字段?它包含http响应返回的多个参数
例如,对于使用workplace或school帐户(Office365)登录的用户,我们可以获得以下数据:
[0]: {[UPN, <user principal name - user's email address>]}
[1]: {[DisplayName, <user's first name and last name>]}
[2]: {[TenantId, <tenant id>]}
[3]: {[PasswordExpiresOn, <no. of ticks from 1.01.1601 divided by 1 second>]}
[4]: {[PasswordChangeUrl, https://portal.microsoftonline.com/ChangePassword.aspx]}
[5]: {[FirstName, <user's first name>]}
[6]: {[TokenExpiresOn, <no. of ticks from 1.01.1601 divided by 1 second>]}
[7]: {[OID, <object identifier as guid>]}
[8]: {[Authority, https://login.microsoftonline.com/common]}
[9]: {[SignInName, <user's email address>]}
[10]: {[UserName, <user's email address>]}
[11]: {[UID, <unique identifier>]}
[12]: {[LastName, <user's last name>]}
我们还可以获得TokenExpiresOn值,因此我们可以轻松处理令牌刷新
要获取令牌到期日期,请将该值乘以10000 000(1秒内的滴答数),然后加上1600年,例如:
var date = new DateTimeOffset(13129745922 * 10000000, new TimeSpan()).AddYears(1600);
date.LocalDateTime.ToString(); // Output: "24.01.2017 15:38:42"
此外,当与MobileService一起使用时,从该响应接收到的令牌返回“unauthorized”。LoginWithMicrosoftAccountAsync(accessToken)感谢您的帮助,令牌之间的差异让我感到困惑,让它工作了。干杯
var date = new DateTimeOffset(13129745922 * 10000000, new TimeSpan()).AddYears(1600);
date.LocalDateTime.ToString(); // Output: "24.01.2017 15:38:42"