![]() We have not hooked the LoginView to the LoginViewModel. Here’s the structure of my solution so far: Ok, before continuing, make sure that your project runs. If you’re using a new WPF project, remember to set the StartupUri in App.xaml to “LoginView.xaml”: Let’s change the Title of the window “Login”. Now let us create the View that will be hooked up to this ViewModel. Handler(this, new Propert圜hangedEventArgs(propertyName)) Propert圜hangedEventHandler handler = Propert圜hanged Protected void RaisePropert圜hanged(string propertyName) ![]() Public event Propert圜hangedEventHandler Propert圜hanged Public class CommonBase:INotifyPropert圜hanged If (!string.Equals(value.ToString(), _username, StringComparison.OrdinalIgnoreCase))īecause just about all my ViewModels are likely to required property change notification, I decided to put this functionality in a base class, “CommonBase”. Properties bound to the specified username and password Property to hold the error message to be displayed Constructor passing the current instance into Property signaling a failed login attempt The comments should be sufficient for one to follow: It is bound to the UserLoginCommand (relationship indicated by a “3”). We will also discuss the Login button a bit later. ![]() To get started let us correct the picture that we used earlier to show the View/ViewModel relationship, based on the discussion above:Īs you can see, the source property in the ViewModel is of type SecureString and to make this clear I have renamed the ViewModel property to PasswordSecureString. Secondly, what techniques are available to compare two items of data obtained from the PasswordBox.SecurePassword property? This means that you cannot bind it to the “Password” property in your ViewModel. This question arises because of at least 2 problems that are normally encountered when attempting to use the PasswordBox.SecurePassword property:įirstly, the PasswordBox.SecurePassword property is not a Dependency Property and hence it cannot be the target of a binding in WPF. For example, you might want to compare an entered password to a password already stored in a database (or the hash of the original password that has been stored). Therefore, in order to avoid compromising the security of our systems, using the PasswordBox.SecurePassword property is recommended.īut then, the popular question is “how do we then compare two passwords if we use the PasswordBox.SecurePassword property”. There are other benefits of using the SecureString class, some of which are discussed in the link above. This means that when you work with this property, your entered password is automatically encrypted in memory and not exposed as plain text. This property returns a SecureString class instance. Accessing the PasswordBox.Password property exposes the entered password as a plain string in memory, which is a security vulnerability in your system.Įnter PasswordBox.SecurePassword. This control has 2 properties that are of interest to us, Password and SecurePassword. However, in WPF there is a special control that is used for passwords. This TextBox would be bound to the Password property in the ViewModel (relationship indicated by a “2”). This password TextBox could then have a special character (typically an asterisk) configured to display instead of the actual characters that the user is entering. And you could assume that you will have a TextBox for the Password as well (particularly if you’re coming from a WinForms background) that holds the password. The Username TextBox is bound to the Username property in the ViewModel (relationship indicated by a “1”). When planning this application this would look like a suitable structure. In this post we shall discuss one method to achieve just that.Ĭonsider the diagram below which depicts the relationship between the View (a XAML window in this case) and the ViewModel:įrom this picture you can see that the Login View has an associated LoginViewModel. If you have ever tried to build a WPF application using the MVVM design pattern you will probably have realised that there is a huddle to be overcome when you try to bind the user’s password to a property in the ViewModel.
0 Comments
Leave a Reply. |