Ysehporp
Ysehporp
CC#
Created by Ysehporp on 3/26/2023 in #help
✅ Sending an HTTP Exception in Web API
I am having some trouble with sending an HTTP exception in web api as it seems how its dont has changed over the course of different .net versions. There seems to be an issue of what return type I have, but I found examples online where people were able to do it with an ActionResult. I found examples which implemented it like this but it will not compile for me because it cannot convert the return type.
[HttpPost]
[Route("login")]
public ActionResult<String> login(Login req)
{

LoginReturn r = new LoginReturn();
Account query = (from m in msgDB.accounts
where m.userID == req.username
select m).FirstOrDefault<Account>();
if (query!= null)
{

}
else
{
//Why doesn't this compile?
return new HttpStatusCodeResult(HttpStatusCode.NotFound, "No user exists with that ID");
}
r.userID = req.username;
r.token = "Fake Token";

return JsonConvert.SerializeObject(r);

}
[HttpPost]
[Route("login")]
public ActionResult<String> login(Login req)
{

LoginReturn r = new LoginReturn();
Account query = (from m in msgDB.accounts
where m.userID == req.username
select m).FirstOrDefault<Account>();
if (query!= null)
{

}
else
{
//Why doesn't this compile?
return new HttpStatusCodeResult(HttpStatusCode.NotFound, "No user exists with that ID");
}
r.userID = req.username;
r.token = "Fake Token";

return JsonConvert.SerializeObject(r);

}
I have also tried what is shown on the learn microsoft page where it says essentially to do
throw new HttpResponseException(HttpStatusCode.NotFound);
throw new HttpResponseException(HttpStatusCode.NotFound);
However doing so causes my server to encounter an unhandled exception and cease to work. What is the correct way of doing this in .net 6???
54 replies
CC#
Created by Ysehporp on 3/17/2023 in #help
❔ Encoding Confusion and string variables
Hello! I am trying to encrypt a string, send it between two clients, and then decrypt the string. Which sounds simple enough but where I've hit a snag is in string encodings, or so I believe. I am using the following code to encrypt and decrypt my string (userPassWord)
public byte[] Encrypt(string userPassWord, string passphrase)
{
using Aes aes = Aes.Create();
aes.Key = DeriveKeyFromPassword(passphrase);
aes.IV = IV;
using MemoryStream output = new();
using CryptoStream cryptoStream = new(output, aes.CreateEncryptor(), CryptoStreamMode.Write);
cryptoStream.Write(Encoding.Unicode.GetBytes(userPassWord));
cryptoStream.FlushFinalBlock();
return output.ToArray();
}


public string Decrypt(byte[] encrypted, string passphrase)
{
using Aes aes = Aes.Create();
aes.Key = DeriveKeyFromPassword(passphrase);
aes.IV = IV;
using MemoryStream input = new(encrypted);
using CryptoStream cryptoStream = new(input, aes.CreateDecryptor(), CryptoStreamMode.Read);
using MemoryStream output = new();
cryptoStream.CopyTo(output);
return Encoding.Unicode.GetString(output.ToArray());
}
public byte[] Encrypt(string userPassWord, string passphrase)
{
using Aes aes = Aes.Create();
aes.Key = DeriveKeyFromPassword(passphrase);
aes.IV = IV;
using MemoryStream output = new();
using CryptoStream cryptoStream = new(output, aes.CreateEncryptor(), CryptoStreamMode.Write);
cryptoStream.Write(Encoding.Unicode.GetBytes(userPassWord));
cryptoStream.FlushFinalBlock();
return output.ToArray();
}


public string Decrypt(byte[] encrypted, string passphrase)
{
using Aes aes = Aes.Create();
aes.Key = DeriveKeyFromPassword(passphrase);
aes.IV = IV;
using MemoryStream input = new(encrypted);
using CryptoStream cryptoStream = new(input, aes.CreateDecryptor(), CryptoStreamMode.Read);
using MemoryStream output = new();
cryptoStream.CopyTo(output);
return Encoding.Unicode.GetString(output.ToArray());
}
This works if I run it in isolation and just pass the result from one into the other. However! Where I am hitting a snag is that I cannot send the encrypted string as a byte[] but I need to send it as a string instead. However whenever I convert that byte array to a string and then back, it ends up either being an incorrect blocksize or decoding incorrectly. Do any of you know how to do this? Thanks a ton!
6 replies
CC#
Created by Ysehporp on 2/12/2023 in #help
❔ WEBAPI [FromBody] Json with optional elements
I am building a .net Wep API application where I will be receiving requests where all of the data is in the JSON body. I understand this is not the preferred way to do this but this is how I have to do it for this application. I will receive a post from my frontend which essentially looks like this
{
"userID": "",
"token": "",
"conversationID": "",
"starttimestamp": "", optional
"endtimestamp": "", optional
}
{
"userID": "",
"token": "",
"conversationID": "",
"starttimestamp": "", optional
"endtimestamp": "", optional
}
My code is able to process this provided both startTimestamp and endTimestamp are occupied. However as they are optional attributes they will sometimes be absent from requests and in those cases I receive an error
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-8e1d4ff06e05e21050e95e3255d1828b-57d0cca122b223fd-00",
"errors": {
"endtimestamp": [
"The endtimestamp field is required."
],
"starttimestamp": [
"The starttimestamp field is required."
]
}
}
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-8e1d4ff06e05e21050e95e3255d1828b-57d0cca122b223fd-00",
"errors": {
"endtimestamp": [
"The endtimestamp field is required."
],
"starttimestamp": [
"The starttimestamp field is required."
]
}
}
I would like to be able to make these field not required but I am uncertain how. My method which receives the request starts like this
[HttpPost]
[Route("RouteHere")]
public string reqUpdate([FromBody] MessageUpdate req)
{
[HttpPost]
[Route("RouteHere")]
public string reqUpdate([FromBody] MessageUpdate req)
{
21 replies
CC#
Created by Ysehporp on 1/30/2023 in #help
❔ Entity Framework duplicating primary key with seeded data.
I have set up a database with EF6 and as part of verifying that it is working I have seeded in some data. The problem is, when I try to add data from those classes with auto incrementing keys the code attempts to start at id=1 instead of id=6 to position itself after my seeded data. It throws an error complaining about being unable to save data because of a duplicate primary key. How can I force it to respect my seeded data?
9 replies