关闭会话后从 table 获取数据
Get data from a table after having closed the session
此时我尝试获取用户列表并检查用户是否在 BD 中
我正在使用 Web API Net 6 和 Sql 服务器
这是代码
[HttpPost("login")]
public async Task<ActionResult<string>> Login(LoginDto request)
{
//In this line I´m try to get the list of users (this bottom line doesn't work)
await _context.Users.ToListAsync();
if(user.UserName != request.UserName)
{
return BadRequest("User Not Found");
}
// ...
这里的问题是程序已经 运行 1 次,直到它正常工作,但是当我结束会话并再次回来时,第二次有一个应用程序,它再也找不到数据库中的用户。然后我的想法是添加那行不起作用的代码(我不知道它是由于 await 还是通过 ListAsync() 是错误的,或者是由于 if 中的用户未连接与数据库的 _context )
顺便说一下,那个用户是静态的,这样声明的
-> public static User user = new User();
谁能帮我解决这个问题,或者告诉我如何从 table
获取数据的更好解决方案
如果您只想在您的用户table 中搜索具有在 LoginDTO 实例中传递的名称的用户记录,那么您只需请求数据库上下文来搜索该名称。
var userInDb = await _context.Users.FirstOrDefaultAsync(x => x.UserName == request.UserName);
if(userInDb == null)
... not found ....
但是让我更好地理解你的问题。如果您要为用户实施自定义授权和验证基础架构,请三思,因为它并不像看起来那么简单。 (例如,你如何在 table 中存储密码?)微软有一个专门的库,名为 ASP.NET Identity
此时我尝试获取用户列表并检查用户是否在 BD 中
我正在使用 Web API Net 6 和 Sql 服务器
这是代码
[HttpPost("login")]
public async Task<ActionResult<string>> Login(LoginDto request)
{
//In this line I´m try to get the list of users (this bottom line doesn't work)
await _context.Users.ToListAsync();
if(user.UserName != request.UserName)
{
return BadRequest("User Not Found");
}
// ...
这里的问题是程序已经 运行 1 次,直到它正常工作,但是当我结束会话并再次回来时,第二次有一个应用程序,它再也找不到数据库中的用户。然后我的想法是添加那行不起作用的代码(我不知道它是由于 await 还是通过 ListAsync() 是错误的,或者是由于 if 中的用户未连接与数据库的 _context )
顺便说一下,那个用户是静态的,这样声明的 -> public static User user = new User();
谁能帮我解决这个问题,或者告诉我如何从 table
获取数据的更好解决方案如果您只想在您的用户table 中搜索具有在 LoginDTO 实例中传递的名称的用户记录,那么您只需请求数据库上下文来搜索该名称。
var userInDb = await _context.Users.FirstOrDefaultAsync(x => x.UserName == request.UserName);
if(userInDb == null)
... not found ....
但是让我更好地理解你的问题。如果您要为用户实施自定义授权和验证基础架构,请三思,因为它并不像看起来那么简单。 (例如,你如何在 table 中存储密码?)微软有一个专门的库,名为 ASP.NET Identity