MobileServiceClient LoginAsync成功,但随后的每个调用均失败

时间:2018-11-06 15:42:35

标签: xamarin.forms azure-web-sites

我正在Xamarin表单上使用Azure移动服务客户端。使用.LoginAsync(“ custom”,user)登录似乎成功,因为将令牌和用户ID返回给我的客户端。但是,下一次调用(MobileServiceTable上的ReadAsync)将失败(内部服务器错误)。

使用Fiddler,似乎移动服务客户端没有明显的原因就从内容类型application / json切换为html / text。

这是屏幕截图。 enter image description here

第一行是登录名,第二行是获取数据的失败尝试。

然后,出于测试目的,我在Azure门户上将“应用程序服务身份验证”设置为“关”,并删除了所有[Authorize]属性。

然后,使用相同的客户端(显然没有登录),我尝试相同的ReadAsync方法,这没关系。数据返回给客户端。

这是第二张Fiddler屏幕截图:

enter image description here

如您在第二张屏幕截图中所见,contenttype = application / json。

我也尝试使用Google登录而不是自定义登录:一切正常。

那是怎么回事?仅当使用自定义登录时,内容类型才会切换为html / text,并且会出现“内部服务器错误”。

这是服务器端的自定义身份验证代码:

    [Route(".auth/login/custom")]
public class CustomAuthController : ApiController
{
    private DataContext db;
    private string signingKey, audience, issuer;

    public CustomAuthController()
    {
        db = new DataContext();
        signingKey = Environment.GetEnvironmentVariable("WEBSITE_AUTH_SIGNING_KEY");
        var website = Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME");
        audience = $"https://{website}/";
        issuer = $"https://{website}/";
    }


    [HttpPost]
    public IHttpActionResult Post([FromBody] tblUser body)
    {
        if (body == null || body.Username == null || body.Password == null ||
            body.Username.Length == 0 || body.Password.Length == 0)
        {
            return BadRequest(); ;
        }

        if (!IsValidUser(body))
        {
            return Unauthorized();
        }

        var claims = new Claim[]
        {
            new Claim(JwtRegisteredClaimNames.Sub, body.Username)
        };

        JwtSecurityToken token = AppServiceLoginHandler.CreateToken(
            claims, signingKey, audience, issuer, TimeSpan.FromDays(30));
        return Ok(new LoginResult()
        {
            AuthenticationToken = token.RawData,
            User = new LoginResultUser { UserId = body.Username }
        });
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

    private bool IsValidUser(tblUser user)
    {
        return db.tblUsers.Count(u => u.Username.Equals(user.Username) && u.Password.Equals(user.Password)) > 0;
    }
}

public class LoginResult
{
    [JsonProperty(PropertyName = "authenticationToken")]
    public string AuthenticationToken { get; set; }

    [JsonProperty(PropertyName = "user")]
    public LoginResultUser User { get; set; }
}

public class LoginResultUser
{
    [JsonProperty(PropertyName = "userId")]
    public string UserId { get; set; }
}

共享代码中的客户端登录:

                MobileServiceUser mobileServiceUser = await client.LoginAsync("custom", JObject.FromObject(user));

如前所述,MobileServiceUser带有令牌返回。随后的每个调用都使用令牌,但失败。问题似乎在于,内容类型已切换为html / text而不是application / json。

我花了几天时间试图解决这个问题,但无济于事。我在这里想念什么? 还是有某种解决方法,例如强制IMobileServiceTable.ReadAsync()使用正确的内容类型?

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我通过删除并重新创建Azure应用程序解决了该问题。我确实在互联网上某处读到了(无法再次找到它),要使用自定义身份验证,您必须具有干净的配置。简洁的含义:如果您事先在Azure门户中配置了Google+身份验证,则将其删除后再继续使用同一应用并使用自定义身份验证将无法正常工作。

相关问题