在OnPost动作中,我收到:
System.NullReferenceException:'对象引用未设置为 对象的实例。'
ContactManager.Pages.Auth.LoginModel.user.get返回null。
在User use = Login(user.Email, user.Password);
Login.cshtml.cs:
{
public User user { get; set; }
public string Msg;
private DatabaseContext db;
public LoginModel(DatabaseContext _db)
{
db = _db;
}
public void OnGet()
{
user = new User();
}
public IActionResult OnPost()
{
User use = Login(user.Email, user.Password);
if (use == null)
{
Msg = "Invalid";
return Page();
}
else
{
HttpContext.Session.SetString("email", use.Email);
return RedirectToPage("L_Success");
}
}
private User Login(string email, string password)
{
var user = db.Users.SingleOrDefault(u => u.Email.Equals(email));
if (user != null)
{
if(BCrypt.Net.BCrypt.Verify(password, user.Password))
{
return user;
}
}
return null;
}
}
}
用户模型:
namespace ContactManager.Models
{
[Table("User")]
public class User {
[Key]
public int Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string Fullname { get; set; }
}
}
我的上下文类别:
namespace ContactManager.Models
{
public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}
public DbSet<User> Users { get; set; }
}
}