// PUT <controller>/?u=username&p=password public HttpResponseMessage Put([FromBody] string value, string p) { FinancialAccountUser model = ParseAccount(value); string path = GetFilePath(p); XmlSerializer serializer = new XmlSerializer(typeof(FinancialAccountUser)); if (File.Exists(path)) { FileStream file = new FileStream(path, FileMode.Truncate, FileAccess.ReadWrite); serializer.Serialize(file, model); HttpStatusCode code = HttpStatusCode.OK; HttpResponseMessage message = new HttpResponseMessage(code); message.ReasonPhrase = "Account successfully updated."; return(message); } else { HttpStatusCode code = HttpStatusCode.PreconditionFailed; HttpResponseMessage message = new HttpResponseMessage(code); message.ReasonPhrase = "Username or Password do not match JSON sent in " + "PUT request."; return(message); } }
// POST <controller> public HttpResponseMessage Post([FromBody] string value, string p) { FinancialAccountUser model = ParseAccount(value); string path = GetFilePath(p); XmlSerializer serializer = new XmlSerializer(typeof(FinancialAccountUser)); if (!File.Exists(path)) { FileStream file = new FileStream(path, FileMode.Create, FileAccess.ReadWrite); serializer.Serialize(file, model); HttpStatusCode code = HttpStatusCode.Created; HttpResponseMessage message = new HttpResponseMessage(code); message.ReasonPhrase = "Account created successfuly"; return(message); } else { HttpStatusCode code = HttpStatusCode.Found; HttpResponseMessage message = new HttpResponseMessage(code); message.ReasonPhrase = "Account found! Creation not necessary. Redirecting"; message.Headers.Location = new Uri(Environment.CurrentDirectory + "?p=" + model.PasswordHash); return(message); } }
public FinancialAccountUser SetupTestModel() { FinancialAccountUser model = new FinancialAccountUser(); model.UserName = "******"; model.Id = "12345"; model.PasswordHash = GetHashString("reallybigpassword"); model.AvailableBalance = "1000000000.00"; model.Stocks = new List <Stock>(); model.Transactions = new List <Transaction>(); Stock stock = new Stock(); stock.Symbol = "BZZ"; stock.UserId = "12345"; stock.PurchasePrice = "12.97"; stock.Quantity = "10000"; model.Stocks.Add(stock); Transaction transaction = new Transaction(); transaction.Timestamp = DateTime.Now.ToString(); transaction.UserId = "12345"; transaction.TypeTransaction = Transaction.TransactionType.PurchaseStock; transaction.TransactionId = new Guid().ToString(); transaction.Symbol = "BZZ"; transaction.StockPrice = "12.97"; transaction.Quantity = "10000"; transaction.FundsChange = "-12970.00"; model.Transactions.Add(transaction); return(model); }
private FinancialAccountUser ParseAccount(string body) { FinancialAccountUser model = new FinancialAccountUser(); XmlSerializer serializer = new XmlSerializer(typeof(FinancialAccountUser)); byte[] byteArray = Encoding.ASCII.GetBytes(body); MemoryStream stream = new MemoryStream(byteArray); model = serializer.Deserialize(stream) as FinancialAccountUser; return(model); }
public void TestMethod1() { FinancialAccountUser model = SetupTestModel(); XmlSerializer serializer = new XmlSerializer(typeof(FinancialAccountUser)); string path = @"D:\Source\BABurgess.XmlApiDbContext\" + @"BABurgess.XmlApiDbContext\App_Data\" + "TestAccount.xml"; Stream stream = new FileStream(path, FileMode.Create); serializer.Serialize(stream, model); Assert.IsTrue(File.Exists(path)); }
public void EncryptedStorageTest1() { FinancialAccountUser user = SetupTestModel(); string postBody = String.Empty; XmlSerializer serializer = new XmlSerializer(typeof(FinancialAccountUser)); using (StringWriter writer = new StringWriter()) { serializer.Serialize(writer, user); postBody = writer.ToString(); } string encryptedFile = StringEncryption.EncryptString(postBody, user.PasswordHash); string finalFile = StringEncryption.EmbedUserString(encryptedFile, user.PasswordHash); Console.WriteLine(finalFile); Assert.IsNotNull(finalFile); }
public HttpResponseMessage Get([FromBody] string value, string p) { FinancialAccountUser model = ParseAccount(value); if (model.UserName == null || p == null) { HttpStatusCode code = HttpStatusCode.Redirect; HttpResponseMessage message = new HttpResponseMessage(code); message.Headers.Location = new Uri("https://baburgessxmlapidbcontext.azurewebsites.net/swagger/ui/index"); return(message); } else { XmlSerializer serializer = new XmlSerializer(typeof(FinancialAccountUser)); string path = GetFilePath(p); if (File.Exists(path)) { string jsonResponse = String.Empty; FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read); string encryptedFile = String.Empty; using (StreamReader stream = new StreamReader(file)) { encryptedFile = stream.ReadToEnd(); } jsonResponse = StringEncryption.DecryptUserAccount(encryptedFile, p); HttpStatusCode code = HttpStatusCode.OK; HttpResponseMessage message = new HttpResponseMessage(code); message.Content = new StringContent(jsonResponse); return(message); } else { HttpStatusCode code = HttpStatusCode.NotFound; HttpResponseMessage message = new HttpResponseMessage(code); message.ReasonPhrase = "Account not found."; return(message); } } }