【问题标题】:Azure mobile service getCurrentUserAzure 移动服务 getCurrentUser
【发布时间】:2015-06-03 04:13:35
【问题描述】:

我有一个 .net 后端 Azure 移动服务。我一直在尝试从 Android 客户端获取当前用户信息。我正在使用以下代码

private void authenticate() {

    LoginRequest login = new LoginRequest();
    login.setUsername(txtUsername.getText().toString());
    login.setPassword(txtPassword.getText().toString());
    ServiceConstant.mClient.invokeApi("CustomLogin", login, LoginRequest.class,
            new ApiOperationCallback<LoginRequest>() {
                @Override
                public void onCompleted(LoginRequest result,
                        Exception exception, ServiceFilterResponse response) {
                    if (exception == null) {
                        try 
                        {
                            if (chkRemember.isChecked()) 
                            {
                                cacheUserToken(ServiceConstant.mClient.getCurrentUser());
                                Toast.makeText(getApplicationContext(),"cache success",Toast.LENGTH_SHORT).show();
                            }
                        } 
                        catch (Exception e) 
                        {
                            Log.e(TAG, "cache fail");
                            Toast.makeText(getApplicationContext(),
                                    "cache fail",
                                    Toast.LENGTH_SHORT).show();
                        }
                        Log.i(TAG, "giris basarili " + result);

                        Intent intent = new Intent(LoginActivity.this, DeviceScanActivity.class);
                        startActivity(intent);

                    } else {
                        Log.e(TAG, "login fail" + exception);
                        alert("login", "success\n" + exception);
                    }
                }
            });
}

当我尝试获取当前用户时,它返回 null。我应该如何获取当前用户信息。我是 Azure 移动服务的新手,我不知道如何解决这个问题。

【问题讨论】:

标签: java android authentication azure-mobile-services azure-cloud-services


【解决方案1】:

此自定义登录代码基于自定义 API,因此不会像使用内置登录功能时那样自动存储结果。

当您从 API 获取结果时,您需要手动设置 mClient 的 CurrentUser 字段。

如果你关注this custom auth tutorial,返回类型实际上会匹配客户端的MobileServiceUser类型,你可以设置值。有关更多详细信息,请参阅该教程的最后一部分,标题为“使用来自客户端的自定义身份验证登录”。

【讨论】:

  • 首先感谢您的回答,我按照指示进行操作,此后当我尝试缓存用户 mAuthenticationToken 值时如下imgur.com/Niq00pO 看起来像这样imgur.com/mpPGiTi 为空,是这样一个问题?从现在开始我能做什么,你能给我一些想法吗
  • 看起来类型没有完全对齐。最简单的解决方案可能是将响应视为简单的 JSON 对象并提取 userID 和 authenticationToken 值。您可以在新的 MobileServiceUser 上设置它们,然后将 MobileServiceClient.CurrentUser 分配给它。
  • 如何从 response.getContent() 中提取这些值?
  • 我认为在您的代码中,您使用 LoginRequest 作为结果类型,而它应该类似于 MobileServiceUser。该调用应类似于invokeApi("CustomLogin", login, MobileServiceUser.class, new ApiOperationCallback&lt;MobileServiceUser&gt;() ...,然后您将能够从result 对象中获取值。此博客可能会有所帮助:blogs.msdn.com/b/writingdata_services/archive/2015/05/06/…
猜你喜欢
  • 1970-01-01
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
  • 2014-02-17
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多