Android客户端自定义登录Azure移动服务(.NET后端)

Android客户端自定义登录Azure移动服务(.NET后端)

问题描述:

我遵循以下文档中的步骤http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-get-started-custom-authentication/ 并构建了我的.NET后端 - 我已经能够使用移动服务的新CustomRegistration和CustomLogin函数 各自的api页面localhost:<portNumber>/help/Api/POST-api-CustomLogin处的“试用”按钮。 ./CustomRegistration。我已经发布了这些channges到我的天蓝色移动服务的服务。Android客户端自定义登录Azure移动服务(.NET后端)

我然后尝试使用我在Android Studio中构建的Android应用程序连接到该服务。我对Android非常陌生,因此我很难让我的应用使用我的移动服务提供的CustomLogin 。下面为Android APP

在Java/com.example.howzit文件夹的代码:

LoginResult.java

package com.example.howzit; 

import com.google.gson.annotations.SerializedName; 

public class LoginResult { 

    public String AuthenticationToken; 
    // 
    // Summary: 
    //  Gets or sets the Microsoft.WindowsAzure.Mobile.Service.Security.LoginResultUser 
    //  logged in. 
    public LoginResultUser User; 

} 

LoginResultUser.java

package com.example.howzit; 

import com.google.gson.annotations.SerializedName; 

public class LoginResultUser { 
    public String UserId ; 
} 

LoginRequest.java

package com.example.howzit; 
    public class LoginRequest 
    { 
     public String username ; 
     public String password ; 
    } 

在TodoActivity类我有这样的:

mClient = new MobileServiceClient(
        "https://mysite.azure-mobile.net/", 
        "APIKEY", 
        this).withFilter(new ProgressFilter()) 
      .withFilter(new RefreshTokenCacheFilter()); 

     LoginRequest loginUser = new LoginRequest(); 
      loginUser.username = "theUserName"; 
      loginUser.password = "thePassword"; 

       mClient.invokeApi("CustomLogin",loginUser,LoginResult.class, new ApiOperationCallback<LoginResult>() { 
        @Override 
        public void onCompleted(LoginResult result,Exception error, ServiceFilterResponse response){ 
         if (error != null) { 
            createAndShowDialog(error, "Error"); 
           } 
           else { 
            createAndShowDialog(result.User.UserId, "User"); 
           } 
        } 
       }); 

我已经调试这一点,它基本上打invokeApi呼叫并继续加载应用程序从未击中onCompleted例程的其余部分 ......在Android Studio中我有访问LogCat,但没有任何信息可以告诉我一个错误。我真的无法深究这一点。我已成功实施了Google登录提供程序以及Window Azure 提供程序,但无法使用我自己的自定义登录名。任何帮助将不胜感激。

更多信息:我已经在VS2013上设置了远程调试,如果我删除身份验证和调试,使用android应用程序时会触发断点,但使用身份验证时,控制器中的CustomLogin Post永远不会被击中。

+0

我已经完成了与Windows Phone的自定义登录身份验证,但不是Android,所以我可能会离开。当您连接到CustomLogin时,登录API是https://mysite.azure-mobile.net/api/CustomLogin。我知道桌面控制器都是用/ tables/....命令调用api是否需要api/CustomLogin?或者你可以使用Fiddler或其他东西来查看它尝试的URL。 – 2014-09-30 15:46:58

+0

@亚当我已经尝试了一切(与API /和没有) - 不能使用小提琴手,因为我的调试设备是我的android手机。不能在我的机器上使用仿真器,因为它永远不会加载.-我已经基本放弃了,因为我没有从adroid studio调试器反馈意见,说出了什么问题 - 非常令人沮丧 – Paul 2014-10-01 13:49:21

+0

它实际上是否在另一端触发了API 。例如。在CustomLogin方法上放置一个断点。第一个要点是查明它是否已经到达API以及错误是在那里连接还是在发回信息。 – 2014-10-02 00:59:47

我在同一个地方卡住这里与Android使用相同的教程+ .NET + CustomAuthentication。 “试一试”的工作,但Android应用程序不会。 我试过亚当的答案,但它没有帮助。

挖/闲逛了几个小时后,我用了Android的调试器,并保持进入SDK代码,直到我发现有一个与JSON解析到LoginResult.class

COM \谷歌问题\ GSON \内部\绑定\ ReflectiveTypeAdapterFactory.java

try { 
    in.beginObject(); 
    while (in.hasNext()) { 
     String name = in.nextName(); 
     BoundField field = boundFields.get(name); 
     if (field == null || !field.deserialized) { 
     in.skipValue(); 
     } else { 
     field.read(in, instance); 
     } 
    } 
    } catch (IllegalStateException e) { 
    throw new JsonSyntaxException(e); 
    } catch (IllegalAccessException e) { 
    throw new AssertionError(e); 
    } 

看来,.NET序列化JSON与Android不同的初始字母

我的工作代码对本教程之后,

.NET CustomLoginResult.cs

public class CustomLoginResult 
{ 
    public string UserId { get; set; } 
    public string MobileServiceAuthenticationToken { get; set; } 

} 

的Android CustomLoginResult.java

public class CustomLoginResult { 
    public String userId; 
    public String mobileServiceAuthenticationToken; 
} 

注意首字母是不同的,在Java变量名必须为,正好与相同,“试试这个“结果体。 enter image description here

不知道这是否解决了这个问题,保罗很可能早就离开/解决了这个问题,但我希望这可以帮助任何人坚持在同一个地方。

+0

吉姆 - 是的,我发现JSon和这个类的属性名称是这个问题......正如你上面讨论的那样 – Paul 2015-08-16 14:49:41

只是把这个问题放在一个更清晰的答案中,即使不确定它是否是正确的答案。

尝试把

[AuthorizeLevel(AuthorizationLevel.Anonymous)] 
[Route("api/Account/CustomLogin")] 
[HttpPost] 

如上您的自定义API控制方法的属性,然后尝试调用/ API /帐号/ CustomLogin

+0

谢谢我'我今晚会试试这个。只是为了让你知道......如果我使用测试客户端(web界面)并在界面中使用JSON发送自定义登录请求,那么它的所有成功 – Paul 2014-10-02 11:31:23

+0

好吧,那么它看起来像是SDK。只是为了检查,你有最新版本的一切吗? – 2014-10-02 13:17:06

+0

我已经尝试了上述但没有喜悦 - 谢谢你的建议 - 我已经在过去两周内下载了所有东西,所以我相信每个包都是最新的 – Paul 2014-10-02 22:01:15

我有同样的问题。亚当的回答非常有帮助! 但是,您无需致电/api/Account/CustomLogin,您需要致电帐户/ CustomLogin/

mClient.invokeApi("Account/CustomLogin/",...

然后,代码绝对应该在你的后台调用。可能仍然是这样,你不会从后端获得返回值。为了解决这个问题,这个问题可能会有所帮助:

Android and Azure Mobile Services: Using invokeAPI to return recordset