2013-04-17 41 views
0

我創建一個登錄頁面,以使用戶能夠使用WPFMVVM申請一個登錄頁面。到目前爲止,我正在使用Entity Framework model,連接到SQL database創建使用EF WPF

我有一張表,其中包含一些用戶詳細信息,如用戶名,密碼,確認密碼電子郵件和用戶角色。

到目前爲止,我已經創建了一直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

我認爲在代碼中有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."); 
} 

如果用戶不存在,顯示消息和用戶名密碼必須相等。

+0

感謝@NRonald的回覆,我也試過之前對我的問題,但似乎仍然沒有工作。它仍然打破了else語句,並且找不到已經在數據庫中的用戶。 – WPFNoob

+0

使用UserID從數據庫收集用戶。也許是不符合用戶名和密碼。還要檢查這些值是否沒有大小寫(大小寫)差異。 – NRonald

+0

感謝他回覆。我收集了這是用ID來做的,但不知道我應該怎麼做才能通過它的用戶ID找到用戶名。任何想法如何使用EF? – WPFNoob