azure - 迁移到应用服务移动应用程序后进行身份验证 : uid vs sid

标签 azure oauth-2.0 azure-mobile-services azure-web-app-service

我已从 Azure 移动服务迁移到新的应用服务移动应用,并且在客户端使用新的 AMS 2.0.0-beta。

我(当前)有两个为 OAuth 2.0 实现的提供商:Google 和 Twitter。

以前,我能够通过服务器上主体中的声明获取提供者 token ,并且会有一个 uid(唯一 ID)声明,该声明可以是“Google:123456789”或“Twitter:123456789010”(或许多字母数字)。我相信 MobileServiceClient.UserId 也暴露了这一点。

现在,在我迁移到新的应用程序服务移动应用程序后(我现在正在使用预览门户,这在很大程度上非常棒),不再有 uid 声明,而是一个单一的sid( session ID)声明,类似于:“sid:ABCDEFGHIJKLMNOPQRSTUVWXYZ”,无论我使用哪个提供商登录。当我在客户端查看 MobileServiceClient.UserId 值时,它也给出了这个“sid”值。

重点是,以前 uid token 可以唯一标识用户。现在,对于所有提供商的所有用户来说都是一样的!

如何通过应用服务移动应用获取之前通过 Azure 移动服务获取的提供商 token ?

另外,有人可以向我指出 Azure 移动服务 2.0.0-beta 的源代码吗?它是开源的吗?我在 GitHub 上似乎找不到它。

编辑:这是服务器端用户的屏幕截图: enter image description here

最佳答案

好的,重新阅读 migration documentation 后我无数次重新审视我之前的步骤之一,发现它有一个无效的假设。在文档中,它提到了身份验证的注意事项,包括以下代码块:

ServiceUser user = (ServiceUser) this.User;

FacebookCredentials creds = (await user.GetIdentitiesAsync()).OfType< FacebookCredentials >().FirstOrDefault();

string mobileServicesUserId = creds.Provider + ":" + creds.UserId;

现在,我无法找到“GetIdentitiesAsync”,并且 ServiceUser 有一个 Identities 可枚举属性,所以我就这么做了。 (毕竟,它提供了与 ServiceUser 的迁移前版本非常相似的信息。)但是,此方法显然获取的数据多于 Identities 枚举中已存在的数据。

我仍然找不到 GetIdentitiesAsync,但在类浏览器中进行一番挖掘后,我找到了名为 GetIdentityAsync 的扩展方法的单一版本在 Microsoft.Azure.Mobile.Server.AppService.ServiceUserExtensions 中(这是唯一的方法)。我跟踪到了 Microsoft.Azure.Mobile.Server.AppService 命名空间,添加了一条 using 语句并尝试了以下代码:

var hmm2 = await serviceUser.GetIdentityAsync<GoogleCredentials>();

我保留名为“hmm2”的变量,因为我有以下屏幕截图:

debug inspection of serviceUser.GetIdentityAsync

右侧带有数字的绿色框是我在迁移之前获得的唯一标识符!因此,要获取 uid,需要针对所有提供者凭据调用此扩展方法。找到非空凭据后,它可以使用 nameidentifier 声明来获取提供商的用户唯一标识符。

我希望一旦应用程序服务准备好投入生产,我们就有一种更简洁的方法来获取非空提供者凭据,但目前这是可行的!

编辑:这是我现在在服务器端运行的代码(客户端 MobileServiceClient.UserId 不起作用。您必须从服务器返回信息):

var serviceUser = (Microsoft.Azure.Mobile.Server.Security.ServiceUser)Thread.CurrentPrincipal;

try
{
    var googleCreds = await serviceUser.GetIdentityAsync<GoogleCredentials>();
    if (googleCreds != null && googleCreds.Claims != null)
    {
        _CurrentProvider = "Google";
        var nameClaim = googleCreds.Claims.Single(x => x.Key.Contains("nameidentifier"));
        _CurrentProviderKey = nameClaim.Value;
        return;
    }

    var twitterCreds = await serviceUser.GetIdentityAsync<TwitterCredentials>();
    if (twitterCreds != null && twitterCreds.Claims != null)
    {
        _CurrentProvider = "Twitter";
        var nameClaim = twitterCreds.Claims.Single(x => x.Key.Contains("nameidentifier"));
        _CurrentProviderKey = nameClaim.Value;
        return;
    }

    throw new NotSupportedException("The OAuth Provider is not supported.");
}
catch (Exception ex)
{
    throw new InvalidOperationException("There was an error updating the authentication provider. InnerException: " + ex, ex);
}

关于azure - 迁移到应用服务移动应用程序后进行身份验证 : uid vs sid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29836075/

相关文章:

azure - 将 Azure ARM VM 与多个负载均衡器关联

python - 如何在 Google App Engine 上防止 "ImportError: No module named oauth2client.client"?

oauth-2.0 - 使用 Google 外部登录 PHPBB 3.1

spring-boot - 如何在 Spring Boot 中将 keycloak 与不同的上下文根和反向代理集成

c# - Newtonsoft.Json.JsonReaderException MobileServicesClient RegisterAsync

c# - Azure 移动服务和 Newtonsoft.Json 7

Azure 逻辑应用 : Change HTTP request body via condition

azure - CosmosDb 搜索索引与分区键

android - 嵌入式 Web 浏览器上的 Azure Oauth 身份验证将于 2017 年 4 月 20 日被阻止,发出警告

asp.net-mvc-4 - 跨 Web 角色和客户端应用程序共享 ASP.NET simplemembership 身份验证