使用ASP.net身份与xamarin时的System.Net.Http.HttpRequestException:400(错误请求)

时间:2016-09-03 11:01:39

标签: c# asp.net-web-api xamarin xamarin.forms asp.net-identity

这里我尝试使用Xamarin.forms注册用户使用REST 但永远的时间我发布Httpclient我返回此错误

  

System.Net.Http.HttpRequestException:400(错误请求)

我试过每一次但总是同样的错误这是我的RegisterService类

private const string WebServiceUrl = "http://MyIP:49932/api/Account/Register";
        public async Task Register(string username, string password, string confirmPassword)
        {
            RegisterModel model = new RegisterModel
            {
                ConfirmPassword = confirmPassword,
                Password = password,
                Email = username
            };


            var httpClient = new HttpClient(new NativeMessageHandler());

            var json = JsonConvert.SerializeObject(model);

            HttpContent httpContent = new StringContent(json);

            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            try
            {
                var result = await httpClient.PostAsync(WebServiceUrl, httpContent);
                result.EnsureSuccessStatusCode();
            }
            catch(Exception ex)
            {

            }
}
}
}

这是Api From AccountController

   // POST api/Account/Register
    [AllowAnonymous]
    [Route("Register")]
    public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        //string UserName, string Password,string ConfirmPassword
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };
        user.RegistrationDate = DateTime.Now;
        IdentityResult result = await UserManager.CreateAsync(user, model.Password);

        if (!result.Succeeded)
        {
            return GetErrorResult(result);
        }

        return Ok();
    }

编辑*

Registermodel

public class RegisterModel
    { 
        public string Email { get; set; }

        public string Password { get; set; }

        public string ConfirmPassword { get; set; }
    }

RegisterBindingModel

 public class RegisterBindingModel
    {
        [Required]
        [Display(Name = "Email")]
        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }

请任何人帮助我,我已经被困了一个星期

Thanx IN Advance

0 个答案:

没有答案