【问题标题】:Azure Mobile Services - Getting more user informationAzure 移动服务 - 获取更多用户信息
【发布时间】:2014-04-19 07:47:20
【问题描述】:

我继承了一个用 XAML 编写的 Windows 8 应用程序。所以在 C# 中,当我进行此调用时

user = await MobileServices.MobileService
                    .LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);

(适用于 Azure 移动服务)

用户对象为我提供了 Token 和 MicrosoftAccount:........

为了对人员进行身份验证,我需要能够看到 WHO 正在请求访问权限...

我在看下面的文章,但我似乎遗漏了什么?文章中的这个 javascript 是我必须用 Node.js 编写的吗?

示例文章: http://blogs.msdn.com/b/carlosfigueira/archive/2013/12/12/expanded-login-scopes-in-azure-mobile-services.aspx

【问题讨论】:

    标签: javascript node.js azure windows-8.1 azure-mobile-services


    【解决方案1】:

    目前,为了能够获取有关已登录用户的更多信息,您需要再次调用该服务以检索用户信息。您实际上不需要要求额外的登录范围(您提到的帖子的主题)来检索用户名,因为默认情况下所有提供者都会提供该用户名。

    This post 应该有您需要在服务器端 (node.js) 编写的代码,以获取有关登录用户的更多信息。 TL;DR 版本如下:

    在服务器端:添加此自定义 API(我将其命名为“userInfo”;将 GET 的权限设置为“user”,将所有其他权限设置为 admin):

    exports.get = function(request, response) {
        var user = request.user;
        user.getIdentities({
            success: function(identities) {
                var accessToken = identities.microsoft.accessToken;
                var url = 'https://apis.live.net/v5.0/me/?method=GET&access_token=' + accessToken;
                var requestCallback = function (err, resp, body) {
                    if (err || resp.statusCode !== 200) {
                        console.error('Error sending data to the provider: ', err);
                        response.send(statusCodes.INTERNAL_SERVER_ERROR, body);
                    } else {
                        try {
                            var userData = JSON.parse(body);
                            response.send(200, userData);
                        } catch (ex) {
                            console.error('Error parsing response from the provider API: ', ex);
                            response.send(statusCodes.INTERNAL_SERVER_ERROR, ex);
                        }
                    }
                }
                var req = require('request');
                var reqOptions = {
                    uri: url,
                    headers: { Accept: "application/json" }
                };
                req(reqOptions, requestCallback);
            }
        });
    }
    

    在客户端,成功登录后,调用该 API:

    user = await MobileServices.MobileService
        .LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);
    var userInfo = await MobileServices.MobileService.InvokeApiAsync(
        "userInfo", HttpMethod.Get, null);
    

    userInfo 将包含带有用户信息的JObjecthttp://feedback.azure.com/forums/216254-mobile-services/suggestions/5211616-ability-to-intercept-the-login-response 有一个开放的功能请求来改进它。

    【讨论】:

    • 好的,听起来我需要添加服务器 Node.js 代码,我在哪里添加呢?我有几个 db azure 表,所以我有一个“用户”表,其中必须使用默认 node.js 方法进行 CRUD 操作。这会在那里添加吗?那么如果在node.js代码中添加了因为名为“userInfo”,那么c# userInfo有用户信息吗?我觉得我错过了一块。
    • 另外,这段代码似乎不是当前的 var table = MobileService.GetTable("Identities"); var response = await table.ReadAsync(""); var identities = response.GetArray()[0].GetObject(); response.GetArray ....
    • 最好的地方是将它添加到自定义 API 中,正如我在上面的答案中提到的(当写博客文章时,自定义 API 还不支持,这就是逻辑在一张桌子)。客户端中对InvokeApiAsync 的调用将调用该API,并且由于客户端已登录,它将向API 发送身份验证信息。 API 将获取该信息(通过调用 user.getIdentities)并联系身份验证提供者以获取有关用户的更多详细信息。
    • 刚刚阅读您关于创建自定义 API 的文章,就在看到您的评论之前!谢谢,正在努力!!!
    • 什么是 liveAccessToken ,那个变量被传入了?这将是一个静态令牌,链接到与 Azure 帐户关联的真实帐户吗?
    猜你喜欢
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多