public override bool ValidateUser(string username, string password) { using (var client = new HttpClient()) { HttpClientHelper.AddJsonRequestAcceptHeader(client); var response = client.PostAsJsonAsync( this.baseApiUrl + "/account/login", new { Username = username, Password = password }).Result; if (!response.IsSuccessStatusCode) { var status = response.StatusCode; var error = JsonConvert.DeserializeAnonymousType( response.Content.ReadAsStringAsync().Result, new { Message = string.Empty }); throw new HttpException((int)status, error.Message); } var id = response.Content.ReadAsAsync <string>().Result; if (id != null) { AuthenticatedSessionHelper.CurrentUserSessionKey = id; return(true); } else { return(false); } } }
public override string CreateUserAndAccount(string userName, string password, bool requireConfirmation, IDictionary <string, object> values) { if (values == null) { throw new ArgumentNullException("values"); } var registrationInfo = values["RegistrationInfo"] as RegistrationInfo; using (var client = new HttpClient()) { HttpClientHelper.AddJsonRequestAcceptHeader(client); var response = client.PostAsJsonAsync(this.baseApiUrl + "/account/register", registrationInfo).Result; var httpErrorMessage = new { message = string.Empty }; var error = JsonConvert.DeserializeAnonymousType(response.Content.ReadAsStringAsync().Result, httpErrorMessage); try { response.EnsureSuccessStatusCode(); return(userName); } catch (Exception) { throw new MembershipCreateUserException(error.message); } } }
public ActionResult Checkout(CheckoutModel checkoutModel) { if (checkoutModel == null) { throw new ArgumentNullException("checkoutModel"); } var baseApiUrl = ConfigurationManager.AppSettings["baseApiUrl"]; using (var client = new HttpClient()) { HttpClientHelper.AddJsonRequestAcceptHeader(client); var response = client.PostAsJsonAsync( baseApiUrl + "/orders", new { CartId = checkoutModel.CartId, ShippingAddressId = checkoutModel.ShippingAddress, BillingAddressId = checkoutModel.BillingAddress, CreditCardId = checkoutModel.CreditCard }).Result; var status = response.StatusCode; if (status == HttpStatusCode.Created) { // everything worked and the order was created so we return the orderId Response.StatusCode = (int)status; var orderId = response.Content.ReadAsAsync <string>().Result; return(new JsonResult() { Data = new { orderId = orderId } }); } if (status == HttpStatusCode.OK) { // the order was constructed OK, but there is an inventory/price issue that the user needs to handle Response.StatusCode = (int)status; var cartCheckoutItems = response.Content.ReadAsAsync <List <CartItem> >().Result; return(new JsonResult() { Data = new { cartCheckoutItems = cartCheckoutItems } }); } var error = JsonConvert.DeserializeAnonymousType( response.Content.ReadAsStringAsync().Result, new { Message = string.Empty }); return(new HttpStatusCodeResult(status, error.Message)); } }