无法将“system.string”类型的对象强制转换为“system.int32”类型

unguejic  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(575)

我是一个新的开发人员和全新的azuresql数据库,我已经发布了一个应用程序,它在我的本地机器上运行良好。但我得到铸造错误时,试图登录发布应用程序,我不知道为什么。

这就是错误

  1. System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32'.
  2. at Microsoft.Data.SqlClient.SqlBuffer.get_Int32()
  3. at Microsoft.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
  4. at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , Int32[] , ResultCoordinator )
  5. at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
  6. at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
  7. at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
  8. at MydateAPI.Repositories.Repo.AuthRepository.Login(String username, String password) in C:\MyProjects\MyDate\MydateAPI\Repositories\Repo\AuthRepository.cs:line 22
  9. at MydateAPI.Controllers.AuthController.Login(UserForLoginDto userForLoginDTO) in C:\MyProjects\MyDate\MydateAPI\Controllers\AuthController.cs:line 67
  10. at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
  11. at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
  12. at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
  13. at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
  14. at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
  15. at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
  16. at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
  17. at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
  18. at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
  19. at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
  20. at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
  21. at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

这是登录方法

  1. [HttpPost("login")]
  2. public async Task<IActionResult> Login(UserForLoginDto userForLoginDTO)
  3. {
  4. //throw new Exception("Computer says No!");
  5. var userFromRepo = await _repo.Login(userForLoginDTO.Username.ToLower(), userForLoginDTO.Password);
  6. if (userFromRepo == null)
  7. return Unauthorized();
  8. var claims = new[]
  9. {
  10. new Claim(ClaimTypes.NameIdentifier, userFromRepo.Id.ToString()),
  11. new Claim(ClaimTypes.Name, userFromRepo.Username)
  12. };
  13. //Created a security Key for the signin credential and encrypted the key
  14. var key = new SymmetricSecurityKey(Encoding.UTF8
  15. .GetBytes(_config.GetSection("AppSettings:Token").Value));
  16. //Sign in credential
  17. var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);
  18. //Security token descriptor to contain claim, expiry date of token and signin credential
  19. var tokenDescriptor = new SecurityTokenDescriptor
  20. {
  21. Subject = new ClaimsIdentity(claims),
  22. Expires = DateTime.Now.AddDays(1),
  23. SigningCredentials = creds
  24. };
  25. var tokenHandler = new JwtSecurityTokenHandler();
  26. var token = tokenHandler.CreateToken(tokenDescriptor);
  27. var user = _mapper.Map<UserForListDto>(userFromRepo);
  28. //return the token as an object to the client
  29. return Ok(new
  30. {
  31. token = tokenHandler.WriteToken(token),
  32. user
  33. });
  34. }

我的存储库中的登录方法

  1. public async Task<User> Login(string username, string password)
  2. {
  3. //var user = await _context.Users.Include(p => p.Photos).FirstOrDefaultAsync(x => x.Username == username);
  4. var user = await _context.Users.FirstOrDefaultAsync(x => x.Username == username);
  5. if (user == null)
  6. {
  7. return null;
  8. }
  9. if (!VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt))
  10. return null;
  11. return user;
  12. }

错误出现在authcontroller login的第67行(下面)

  1. var userFromRepo = await _repo.Login(userForLoginDTO.Username.ToLower(), userForLoginDTO.Password);

以及我的存储库登录的第22行(下面)

  1. var user = await _context.Users.FirstOrDefaultAsync(x => x.Username == username);

但是我看不到我在哪里把'system.string'类型转换成'system.int32'。
我已在azure资源组中附加了sql数据库的快照。
我可以注册一个新用户,但不能登录。但在我的本地机器上,我可以注册和登录没有任何错误。
需要帮助吗

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题