c# - 使用 Windows.Security.Authentication 成功进行身份验证后的空数据

标签 c# azure uwp

我正在将我的应用程序从 Windows 8.1 升级到 Windows 10 UWP。以前,我使用 Windows.Live API 来获取有关用户的信息,包括我将传递给 MobileService.LoginAsync 方法的 token 。这一切都必须被废弃,转而使用 windows.security.authentication。不幸的是,使用下面的代码后,成功的 SSO token 请求会导致对象丢失我需要的大部分数据。现在,只要获得一个用户名就很好了,我可以从那里扩展。这是我遇到问题的代码...在 if 语句末尾设置的断点显示空的 account.username 字段。

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;

        }

最佳答案

您是否尝试过检查 ResponseData[0].Properties 字段的值?它包含http响应返回的多个参数。

例如,对于使用工作场所或学校帐户 (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>]}

如您所见,我们能够获取用户的 DisplayName、FirstName、LastName 和电子邮件地址。我不确定个人帐户是否可以获得相同的数据,但您可以轻松找到它 - 只需将 ResponseData[0].Properties 转换为立即窗口中列出:

ResponseData[0].Properties.ToList();

我们还可以获取TokenExpiresOn值,这样我们就可以轻松处理 token 刷新。

要获取 token 到期日期,请将此值乘以 10 000 000(1 秒内的刻度数)并加上 1600 年,例如:

var date = new DateTimeOffset(13129745922 * 10000000, new TimeSpan()).AddYears(1600);
date.LocalDateTime.ToString();  // Output: "24.01.2017 15:38:42"

关于c# - 使用 Windows.Security.Authentication 成功进行身份验证后的空数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38080439/

相关文章:

c# - Rally SOAP API - 导入 .net 项目后缺少 RallyServiceService 类

azure - 使用 PowerShell 通过 Manager 生成所有 AD 用户的报告

azure - 使用审批从 azure 管道部署到 azure 应用服务

python - 使用 python 请求访问 login.microsoftonline oauth2 后面的站点

c# - 自定义控件的基类

c# - 来自现有 C API 的 SWIG C#

c# - c#main方法的工作方式

c# - 当我使用 x :bind in uwp: invalid binding path err 时出错

c# - 切换可观察集合中项目的顺序 - UWP

c# - UWP C# - 从通知点击重新启动应用程序