迁移到App Services移动应用程序后的身份验证:uid vs sid

3 azure oauth-2.0 azure-web-sites azure-mobile-services

我已经从Azure移动服务迁移到新的App Services移动应用程序,我在客户端使用新的AMS 2.0.0-beta.

我有两个(目前)为OAuth 2.0实施的提供商:Google和Twitter.

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

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

关键是以前uid令牌可以唯一地标识用户.现在所有提供商的所有用户都是一样的!

如何通过以前能够通过Azure移动服务获得的App Services移动应用程序获取提供程序令牌?

还有,有人能指出Azure Mobile Services 2.0.0-beta的源代码吗?它是开源的吗?我似乎无法在GitHub上找到它.

编辑:以下是服务器端用户的屏幕截图: 在此输入图像描述

小智 5

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

ServiceUser user = (ServiceUser) this.User;

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

string mobileServicesUserId = creds.Provider + ":" + creds.UserId;
Run Code Online (Sandbox Code Playgroud)

现在,我无法找到"GetIdentitiesAsync",而ServiceUser具有Identities可枚举属性,所以我就是这样做的.(毕竟,它提供的信息与ServiceUser的迁移前版本非常相似.)但是,这种方法显然获得了比Identities枚举中已有的更多的数据.

我仍然无法找到GetIdentitiesAsync,但经过一段在类浏览器周围挖,我是能够找到延长方法的单一版本,称为GetIdentityAsyncMicrosoft.Azure.Mobile.Server.AppService.ServiceUserExtensions(它的存在的唯一方法).我将其跟踪到Microsoft.Azure.Mobile.Server.AppService命名空间,添加了一个using语句并尝试了以下代码:

var hmm2 = await serviceUser.GetIdentityAsync<GoogleCredentials>();
Run Code Online (Sandbox Code Playgroud)

我将离开名为"hmm2"的变量,因为我有以下屏幕截图:

调试serviceUser.GetIdentityAsync的检查

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

我希望一旦App Services准备好生产,我们就可以通过一些更简洁的方式获得非null提供者凭据,但是现在这个有效!

编辑:这是我的代码现在正在服务器端工作(客户端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);
}
Run Code Online (Sandbox Code Playgroud)