0

ユーザーがアプリケーションを使用できるようにするためのログインページを作成していWPF MVVMます。これまでのところEntity Framework model、に接続されたを使用していSQL databaseます。

ユーザー名、パスワード、パスワードの確認メール、ユーザー ロールなどのユーザーの詳細を含むテーブルが 1 つあります。

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 ステートメントの作成を手伝ってくれる人はいますか?

ありがとうございます。

4

1 に答える 1

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.");
 }

ユーザーが存在しない場合は、メッセージを表示します。ユーザー名パスワードは同じでなければなりません。

于 2013-04-17T12:30:58.710 に答える