Reputation: 341
I had this function in the web api of the mobile service declared
public HttpResponseMessage Post(LoginRequest loginRequest)
{
EasyParkContext context = new EasyParkContext();
User user = context.Users.SingleOrDefault(a => a.UserName == loginRequest.UserName);
if (user != null)
{
if (BCrypt.Net.BCrypt.Verify(loginRequest.Password, user.Password))
{
ClaimsIdentity claimsIdentity = new ClaimsIdentity();
claimsIdentity.AddClaim(new Claim(ClaimTypes.PrimarySid, user.Id));
claimsIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, loginRequest.UserName));
LoginResult loginResult = new EasyParkLoginProvider(handler).CreateLoginResult(claimsIdentity, Services.Settings.MasterKey);
return this.Request.CreateResponse(HttpStatusCode.OK, loginResult);
}
}
return this.Request.CreateResponse(HttpStatusCode.Unauthorized, "Invalid username or password");
}
And this function declared in my client
public async Task<bool> Login(string userName, string password)
{
LoginRequest loginRequest = new LoginRequest() { UserName = userName, Password = password };
try
{
HttpResponseMessage loginResult = await _service.InvokeApiAsync<LoginRequest, HttpResponseMessage>("EasyParkLogin", loginRequest);
JObject json = JObject.Parse(loginResult.Content.ToString());
_service.CurrentUser = new MobileServiceUser(json["user"]["userId"].ToString().Replace("EasyPark:", ""))
{
MobileServiceAuthenticationToken = json["authenticationToken"].ToString()
};
return true;
}
catch (Exception e)
{
return false;
}
}
It works at the mobile service page, but is returning null during my client code, sorry I can't attached the image due to my reputation...
Upvotes: 2
Views: 332
Reputation: 15
try to check the return value of
await _service.InvokeApiAsync<LoginRequest, HttpResponseMessage>("EasyParkLogin", loginRequest);
Upvotes: -1
Reputation: 341
public async Task<bool> Login(string userName, string password)
{
LoginRequest loginRequest = new LoginRequest() { UserName = userName, Password = password };
try
{
var loginResult = await _service.InvokeApiAsync("EasyParkLogin", JToken.FromObject(loginRequest));
JObject json = JObject.Parse(loginResult.ToString());
_service.CurrentUser = new MobileServiceUser(json["user"]["userId"].ToString().Replace("EasyPark:", ""))
{
MobileServiceAuthenticationToken = json["authenticationToken"].ToString()
};
return true;
}
catch (Exception e)
{
return false;
}
}
by changing the from the code:
HttpResponseMessage loginResult = await _service.InvokeApiAsync<LoginRequest, HttpResponseMessage>("EasyParkLogin", loginRequest);
JObject json = JObject.Parse(loginResult.Content.ToString());
to
var loginResult = await _service.InvokeApiAsync("EasyParkLogin", JToken.FromObject(loginRequest));
JObject json = JObject.Parse(loginResult.ToString());
Upvotes: 2