【发布时间】:2021-06-22 07:44:41
【问题描述】:
我正在开发 ASP.NET Core 3.1 API
我想将用户的电子邮件地址作为 url(查询字符串)的一部分传递,并让 API 查找他们的密码哈希并以 JSON 格式将电子邮件和密码返回给我。
我的代码可以正常工作,但我的电子邮件地址值很奇怪。它要么封装在 "/" "" 中,要么变量 "email" 显示的不是电子邮件地址 test@test.com。
我可能没有正确编写 url,或者它无法处理电子邮件地址中的 @ 和句点?
https://localhost:5001/api/User/test@test.com
不起作用。
https://localhost:5001/api/User/"test@test.com"
返回:{"email":" \ "test@test.com\ "","passhash":"12121212121212"}
我想要 "email": "test@test.com" 之前或之后没有 "\"
https://localhost:5001/api/User/email?email=test@test.com
返回:{"email":"email","passhash":"12121212121212"}
我想要 "email": "test@test.com" 而不是 "email": "email"
我的用户控制器:
[HttpGet("{email}")]
public async Task<IActionResult> Get(string email)
{
email = email.ToString();
IUsers user = new Users();
IDictionary<string, string> fetch = await Task.Run(() => user.FetchUser(email));
System.Diagnostics.Debug.WriteLine("Fetch User - " + fetch);
if (fetch["passhash"] != null)
{
// send back the user's email and passhash only if passhash isn't null
return Ok(fetch);
}
else
{
// if passhash is null, send back a bad request HTTP 400.
return BadRequest();
}
}
访问数据库的FetchUser方法:
IDictionary<string, string> IUsers.FetchUser(string email)
{
using (dbConnection)
{
//SQL Command
string cmd = String.Format(@"select u.passhash from users u where email={0}", email);
// Save results to result.
string passhash = dbConnection.Query<string>(cmd).FirstOrDefault();
IDictionary<string, string> fetchedUser = new Dictionary<string, string>();
if (passhash != null)
{
// Add the result to the Dictionary
fetchedUser.Add( "email", email);
fetchedUser.Add("passhash", passhash);
System.Diagnostics.Debug.WriteLine("User {0} passhash was fetched", email);
}
else
{
// Add null result to the Dictionary
// I'll use this to signal that there was no match.
fetchedUser.Add("email", email);
fetchedUser.Add("passhash", null);
System.Diagnostics.Debug.WriteLine("User {0} provided incorrect passhash", email);
}
System.Diagnostics.Debug.WriteLine(Environment.NewLine);
return fetchedUser;
}
}
感谢任何建议:)
仅供参考:
我正在对用户的密码进行哈希处理并将其存储在数据库中,因此我只需要发送哈希而不是密码。用户提供的密码将被散列,如果它们都匹配,那么它们是好的。
我打算在访问 API 之前使用 JWT 令牌[授权],并使用 https 连接来使发送电子邮件和密码更加安全。
【问题讨论】:
-
“我正在对用户的密码进行哈希处理并将其存储在数据库中,所以我只需要发送哈希而不是密码” - 这样哈希就变成了密码.这到底解决了什么问题?此外,您有一个巨大的 SQL 注入漏洞,您的问题是由于 SQL 中缺少引号引起的。请改用参数化查询,或者更确切地说,如果您不知道自己在做什么,请不要进行自己的身份验证。
-
当您使用
https://localhost:5001/api/User/test@test.com时究竟会发生什么?它为我工作。 “不起作用” - 对问题的描述非常模糊。
标签: asp.net-core asp.net-core-webapi url-parameters asp.net-apicontroller