Back to Devexpress

Login Form

windowsforms-404071-ui-templates-forms-login-form.md

latest3.7 KB
Original Source

Login Form

  • Nov 21, 2022
  • 2 minutes to read

The Login Form (LoginForm) is a sign-in form that accepts user email and password information.

What’s Inside

The Login Form includes the following UI components that ship as part of the DevExpress UI Templates:

Show Login Form

csharp
using DevExpress.UITemplates.Collection.Forms;

// ...
static void Main() {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    using(var frm = new LoginForm()) {
        if(frm.ShowDialog() == DialogResult.OK) {
            if(frm.IsAuthorizationComplete) {
                Application.Run(new Form1());
            }
        }
    }
}

Use the following properties of the form’s ViewModel to personalize its title, button, and links:

  • Title - the form’s title.

  • Action - the Login button’s caption.

  • RegisterLink - the Register link’s text.

  • RecoveryLink - the Recover Password link’s text.

  • C#

csharp
public partial class Login : HtmlFormBase {
    // ...
    public class ViewModel {
        public string Title {
            get { return "Log In"; }
        }
        public string Action {
            get { return "Log In"; }
        }
        public string RecoveryLink {
            get { return "Forgot the password?"; }
        }
        public string RegisterLink {
            get { return "Create an account"; }
        }
        // ...
    }
}

Form State and Actions

Use the form’s Status property to obtain its state.

ValueDescription
AuthorizationCompleteLogin/email authentication succeeded.
AuthorizationFailedLogin/email authentication failed.
RecoveryRequestedThe ‘Forgot the password?’ link has been clicked.
RegistrationRequestedThe ‘Create an account’ link has been clicked.
Unknown

Use the following methods of the ViewModel to implement the form actions, which include user credentials authentication, user/account registration, and password recovery.

  • QueryAuthorization - authenticates user credentials.

  • Recover - requests a password reset and closes the form.

  • Register - registers a new user/account and closes the form.

  • C#

csharp
public partial class Login : HtmlFormBase {
    // ...
    public class ViewModel {
        public virtual string Email { get; set; }
        public virtual string Password { get; set; }

        public virtual Status Status {
            get;
            protected set;
        }
        public void Recover() {
            Status = Status.RecoveryRequested;
            /* Implement password recovery. */
            CloseWindow();
        }
        public void Register() {
            Status = Status.RegistrationRequested;
            /* Register a new account. */
            CloseWindow();
        }
        public void LogIn() {
            Status = QueryAuthorization() ? Status.AuthorizationComplete : Status.AuthorizationFailed;
        }
        bool QueryAuthorization() {
            /* 
               Authenticate the user credentials.
               Return 'false' if login/email authentication failed.
            */
            return true;
        }
    }
}