我正在创建一个登录页面,以允许用户使用WPF
MVVM
应用程序。到目前为止,我使用Entity Framework model
,已连接到SQL database
。
我有一个表包含一些用户详细信息,例如用户名,密码,确认密码电子邮件和用户角色。
到目前为止,我已经为我的viewmodel(用户名和密码)中的某些属性创建了一个bound
的视图。
我还有一个注册页面,允许用户拥有用户凭证(也有一些验证,因此只有1个用户可以使用名称,但不能复制)。
显然,与ASP.NET
相比,您可以使用身份验证并以这种方式创建用户。我一直在关注如何创建登录页面的各种链接,但它们都不是我想要的。
但是,我不太确定我所做的是正确的,因为我如何匹配数据库中的结果以允许用户登录?
这是我到目前为止所做的事情;
public void CheckLogin()
{
var user = context.Users.Where(i => i.UserID == this.UserID).FirstOrDefault();
if (user != null )
{
MessageBox.Show("Unable to Login, incorrect credentials.");
}
else if (this.Username == user.Username || this.Password == user.Password)
{
MessageBox.Show("Successfully Logged in, " + user.Username + "");
}
else
{
MessageBox.Show("Unable to Login, incorrect credentials.");
}
}
private ICommand showLoginCommand;
public ICommand ShowLoginCommand
{
get
{
if (this.showLoginCommand == null)
{
this.showLoginCommand = new CommandBase(i => this.CheckLogin(), null);
}
return this.showLoginCommand;
}
}
XAML;
<TextBox Name="txtUserName" HorizontalAlignment="Left" Style="{StaticResource myErrorTemplate}"
Text="{Binding Username, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True, Mode=TwoWay}"/>
<PasswordBox HorizontalAlignment="Left" Name="txtPassword"
behavior:PasswordBoxAssistant.Attach="True"
behavior:PasswordBoxAssistant.Password="{Binding Password, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True, Mode=TwoWay}"/>
<Button Name="btnLogin" IsDefault="True"
Command="{Binding ShowLoginCommand}"/>
但是当输入细节时,它似乎不起作用。任何人都可以帮我创建一个EF语句来检查数据库中的用户并将其与登录页面输入的用户名和密码相匹配吗?
先谢谢。
答案 0 :(得分:0)
我认为代码中有if语句。当我查看代码时,它应该更像是:
if (user == null)
{
MessageBox.Show("Unable to Login, incorrect credentials.");
}
else if (this.Username == user.Username && this.Password == user.Password)
{
MessageBox.Show("Successfully Logged in, " + user.Username + "");
}
else
{
MessageBox.Show("Unable to Login, incorrect credentials.");
}
如果用户不存在,请显示该消息,用户名和密码必须相等。