3

我正在使用.NET背景的Xamarin Forms移動應用程序中工作。我儘可能多地跟蹤指南。但是這些在某種程度上還未完成,並且沒有完整的自定義身份驗證示例。我終於到了一個地步,我現在不知道如何前進。我無法登錄工作。Azure移動應用程序 - 自定義身份驗證 - 無法登錄

我得到這個錯誤後,客戶端獲取LoginAsync的迴應:

 user = await TodoItemManager.DefaultManager.CurrentClient.LoginAsync("CustomAuth", credentials); 

這是錯誤:

ex {"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException} 

如果我使用Google+等默認提供完美的作品。所以我認爲問題出現在後端。但我不知道我做錯了什麼。我循環了幾次代碼,看起來很好。 我試過調試服務器端,直到它到達客戶端時纔得到任何錯誤。

我在做什麼錯?

這是我在服務器端的代碼。

public IHttpActionResult Post(LoginRequest loginRequest) 
    { 
     if (isValidAssertion(loginRequest.username, loginRequest.password)) // user-defined function, checks against a database 
     { 
      JwtSecurityToken token = GetAuthenticationTokenForUser(loginRequest.username); 

      return Ok(new 
      { 
       AuthenticationToken = token.RawData, 
       User = new { UserId = loginRequest.username } 
      }); 
     } 
     else // user assertion was not valid 
     { 
      return Unauthorized(); 
     } 
    } 

的附配的功能:

private bool isValidAssertion(string username, string password) 
    { 
     AspNetUsers AspNetUser = db.AspNetUsers.SingleOrDefault(x => x.UserName.ToLower() == username.ToLower()); 
     return AspNetUser != null && VerifyHashedPassword(AspNetUser.PasswordHash, password); 
    } 

    private JwtSecurityToken GetAuthenticationTokenForUser(string username) 
    { 
     var claims = new Claim[] 
     { 
      new Claim(JwtRegisteredClaimNames.Sub, username) 
     }; 

     string signingKey = "123456789123456789...";//Environment.GetEnvironmentVariable("WEBSITE_AUTH_SIGNING_KEY"); 
     string audience = "https://todo.azurewebsites.net/"; // audience must match the url of the site 
     string issuer = "https://todo.azurewebsites.net/"; // audience must match the url of the site 

     JwtSecurityToken token = AppServiceLoginHandler.CreateToken(
      claims, 
      signingKey, 
      audience, 
      issuer, 
      TimeSpan.FromHours(24) 
     ); 

     return token; 
    } 

在啓動I類補充說:

 config.Routes.MapHttpRoute("CustomAuth", ".auth/login/CustomAuth", new { controller = "CustomAuth" }); 

這是我在客戶端代碼:

public async Task<bool> Authenticate() 
    { 
     string username = "[email protected]"; 
     string password = "todo"; 

     string message = string.Empty; 
     var success = false; 
     var credentials = new JObject 
     { 
      ["username"] = username, 
      ["password"] = password 
     }; 
     try 
     { 
      user = await TodoItemManager.DefaultManager.CurrentClient.LoginAsync("CustomAuth", credentials); 
      if (user != null) 
      { 
       success = true; 
       message = string.Format("You are now signed-in as {0}.", user.UserId); 
      } 
     } 
     catch (Exception ex) 
     { 
      message = string.Format("Authentication Failed: {0}", ex.Message); 
     } 
     await new MessageDialog(message, "Sign-in result").ShowAsync(); 
     return success; 
    } 

謝謝尋求幫助。

EDIT(解決方案):

我要澄清的人同樣的問題。該錯誤是關於一些大寫/小寫的差異。返回中的名稱必須是「user」,「userId」和「authenticationToken」。的確如此:

 return Ok(new 
     { 
      authenticationToken = token.RawData, 
      user = new { userId = loginRequest.username } 
     }); 
+0

當您通過Postman使用用戶名/密碼進行POST時,響應的外觀如何?另外,在POST之後,client.CurrentUser對象是什麼樣的?是否設置了Id和MobileServiceAuthenticationToken? –

+0

最後,如果您使用Postman將POST發送到您的.auth/login/CustomAuth,那麼請執行GET/tables/foo並將X-ZUMO-AUTH標頭設置爲您的身份驗證令牌,這是否正常工作? –

+0

在使用郵遞員的firts場景中,我獲得瞭如下所示的響應:{「MobileServiceAuthenticationToken」:「ey1234 ...」,「UserId」:「sid:1234 ...」}另一方面,client.CurrentUser設置爲null,拋出異常。在使用GET的第二種情況下,我得到表格的兩行。 – ajmena

回答

2

看起來您的服務器響應錯誤。看看有效的迴應,它看起來像它需要:

{ 
    "user": "your-user-id", 
    "authenticationToken": "the-jwt" 
} 

更正從您的服務器代碼的響應,看看是否有幫助。

+1

哦,我的天哪就是這樣。我之前嘗試過這些詞,但他們讓我失望,但現在我知道爲什麼。該錯誤是大寫/小寫的問題。名稱必須是「user」,「userId」和「authenticationToken」。完全像這樣。非常非常感謝你。 – ajmena

相關問題