PasswordBox and MVVM

后端 未结 5 1572
再見小時候
再見小時候 2021-02-02 13:35

We have the following scenario:

  1. MVVM userinterface where a user can place his password (actually a PasswordBox)
  2. Server that shall do some wor
5条回答
  •  南方客
    南方客 (楼主)
    2021-02-02 14:17

    Personally I just pass the entire PasswordBox control to my LoginCommand

    I know it breaks MVVM because the ViewModel layer now references a View-specific object, but I think in this specific case it's OK.

    So I might have XAML that looks like this:

    And a LoginCommand that does something like this:

    private void Login(object obj)
    {
        PasswordBox pwBox = obj as PasswordBox;
    
        SomeBlackBoxClass.ValidatePassword(UserName, pwBox.Password);
    }
    

    I suppose you could also run some kind of encryption algorithm on the value and compare the hash of that value to the hash of the user's password too

    private void Login(object obj)
    {
        PasswordBox pwBox = obj as PasswordBox;
        var encryptedPassword = SomeLibrary.EncryptValue(pwBox.Password, someKey);
    
        if (encryptedPassword == User.EncryptedPassword)
            // Success
    }
    

    I'm no expert on the PasswordBox control or security, but I do know that you don't want to be storing the user's password in plain text anywhere in memory within your application

    (Technically, it's stored as plain text in PasswordBox.Password - you can use something like Snoop to verify this if you want - however typically the PasswordBox doesn't exist for longer than it takes the user to login, and the actual "password" is just text entered by the user, which may or may not be correct. A keylogger could get you the same information.)

提交回复
热议问题