I'm creating a login page to enable a user to use a WPF MVVM application.
So far I'm using Entity
Framework model, connected to a SQL
database.
I have one table that contains some user details such as a username, password, confirm password email and a user role.
So far, I have created a view that has been bound to
some properties within my viewmodel (username and password).
I also have a registration page to allow a user to have a user credential (also has some validation so there can only be 1 user with a name, it can not be duplicated).
Obviously, compared to ASP.NET you
can use authentication and create users that way. I have been following some various links how to create a login page but none of them are exactly what I would like.
But, I am not too sure what I am doing is correct, in the sense that how do I match the results from the database to allow a user to login?
This is what I have done so far;
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 as, " + 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}"/>But when details have been entered it doesn't seem to work. Could anyone help me create a EF statement that checks users within the database and matches it to the username and password entered from the login page?
Also, it throws a `Object reference not set to an instance of an object.`when I execute it, throwing it around the; if(this.user == user.username...).Any ideas how I could solve this issue?
Thanks in advanced.