因此,我创建web API与asp net和使用swagger ui,当我试图发布数据,如注册用户,我得到以下错误:
response status 400
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-2eab5ca50f25670cbaae859bf26f0968-6647b29cdb08df1a-00",
"errors": {
"userRegister": [
"The userRegister field is required."
],
"$.pin": [
"The JSON value could not be converted to System.Nullable`1[System.Int32]. Path: $.pin | LineNumber: 7 | BytePositionInLine: 20."
]
}
}
字符串
这就是我想写的:
{
"email": "[email protected]",
"firstname": "test",
"lastname": "test",
"phone": 511223170,
"accountType": 1,
"iban": "GE05258155379184328548",
"pin": 12345678901,
"password": "qazwsxplmokn"
}
型
当我不改变任何东西,在价值观和离开他们作为“字符串”或为intiger 0它的作品
这是请求代码:
[HttpPost("Register")]
public IActionResult Register(UserRegister userRegister)
{
if(_db.GetAll<string>($"select Email from dbo.Users where Email = '{userRegister.Email}'").Count() == 0)
{
if (_db.GetAll<string>($"select Phone from dbo.Users where Phone = {userRegister.Phone}").Count() == 0)
{
byte[] passwordSalt = new byte[128 / 8];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
rng.GetNonZeroBytes(passwordSalt);
}
string PasswordSaltWithKey = _configuration.GetSection("AppSettings:PasswordKey") +
Convert.ToBase64String(passwordSalt);
byte[] passwordHash = KeyDerivation.Pbkdf2(
password: userRegister.Password,
salt: Encoding.ASCII.GetBytes(PasswordSaltWithKey),
prf: KeyDerivationPrf.HMACSHA256,
iterationCount: 100000,
numBytesRequested: 256 / 8
);
string addAuthSql = $@"
INSERT INTO dbo.Auth (
[Email],
[Phone],
[PasswordHash],
[PasswordSalt]
)VALUES(
'{userRegister.Email}',
{userRegister.Phone},
@PasswordHash,
@PasswordSalt)
";
string addUserSql = $@"
INSERT INTO dbo.Users(
[Email],
[Firstname],
[Lastname],
[Phone],
[AccountType],
[IBAN],
[PIN]
)VALUES(
'{userRegister.Email}',
'{userRegister.Firstname}',
'{userRegister.Lastname}',
{userRegister.Phone},
{userRegister.AccountType},
'{userRegister.IBAN}',
{userRegister.PIN}
)
";
List<SqlParameter> sqlParameters = new List<SqlParameter>();
SqlParameter passwordSaltParameter = new SqlParameter("@PasswordSalt", SqlDbType.VarBinary);
passwordSaltParameter.Value = passwordSalt;
SqlParameter passwordHashParameter = new SqlParameter("@PasswordHash", SqlDbType.VarBinary);
passwordHashParameter.Value = passwordHash;
sqlParameters.Add(passwordSaltParameter);
sqlParameters.Add(passwordHashParameter);
if (_db.ExecuteWithParameters(addAuthSql, sqlParameters) && _db.Execute(addUserSql))
{
return Ok("Succesfully added user");
}
else
{
throw new Exception("ERROR");
}
}
return BadRequest($"PHONE_{userRegister.Phone}_ALREADY_EXISTS");
}
return BadRequest($"EMAIL_{userRegister.Email}_ALREADY_EXISTS");
}
型
型号:
namespace Api.Models
{
public class UserRegister
{
public string? Email { get; set; }
public string? Firstname { get; set; }
public string? Lastname { get; set; }
public int Phone { get; set; }
public int AccountType { get; set; }
public string? IBAN { get; set; }
public int PIN { get; set; }
public string? Password { get; set; }
}
}
型
我以为这是因为命名,我试图添加[JsonPropertyName],但仍然没有工作,我找不到任何在谷歌,帮助我
1条答案
按热度按时间ctehm74n1#
问题是你的pin值,12345678901,高于
int
可以容纳的最大值2147483647。要么将pin的类型更改为字符串(如果你的逻辑指示它可以容纳字母,数字,特殊字符),添加最大值验证ex:9999,或者将类型更改为long
。P.S:不要把所有的逻辑都放在控制器中,把它分成逻辑模块(服务,存储库,验证器等)。