【问题标题】:Passing email address as a route parameter is not working将电子邮件地址作为路由参数传递不起作用
【发布时间】: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


【解决方案1】:

你可以使用这个网址:

https://localhost:5001/api/User/"test@test.com"

但修正你的行为:

[HttpGet("{email}")]
 public async Task<IActionResult> Get(string email)
 {
var email = email.Trim('"');
....your code

它应该可以正常工作,但是如果您仍然有一些问题,请尝试:

[HttpGet("{email}")]
 public async Task<IActionResult> Get(string email)
 {
var email = email.Trim('"');

string cmd = String.Format(@"select u.passhash from users u where email='{0}'", email);

....your code

````

【讨论】:

  • 感谢您的回复,它确实有效,但使用参数化 SQL 查询效果最好
【解决方案2】:

改变这一行:

string cmd = String.Format(@"select u.passhash from users u where email={0}", email);

到:

string cmd = String.Format(@"select u.passhash from users u where email='{0}'", email);

它可能会解决您当前的问题,但是:

停止将参数加入查询字符串!这会导致sql注入。

改用参数化查询。

【讨论】:

  • 感谢您的回答。我按照您的建议使用参数化查询使其工作。 string cmd = String.Format(@"select u.passhash from users u where email = @Email"); string passhash = dbConnection.Query&lt;string&gt;(cmd, new {Email = email }).FirstOrDefault();
  • 现在可以使用这个网址:localhost:5001/api/User/test@test.com
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-17
  • 2012-02-03
  • 2020-11-05
  • 1970-01-01
  • 2015-12-21
  • 2017-06-02
  • 1970-01-01
相关资源
最近更新 更多