C# login examples

前端 未结 3 1946
傲寒
傲寒 2020-12-30 11:35

I am having trouble hiding my main form for a login form. Once user has logged in to close login form and show main form.

I have been confusing myself that much I ha

3条回答
  •  忘掉有多难
    2020-12-30 12:08

    So ... Here is my login :) You can like it or don't like, but so ... This is my solution.

        private void FRIIB_Load(object sender, EventArgs e)
        {
            try
            {
                QueryBuilder.insql = Crypto.DecryptStringAES(Model.DecryptRegisteryValue("inSQL"), "inSQL");
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            } // getting connection string
            if (!(new Func(() =>
                    {
                        Func l = null; l = () =>
                        {
                            using (LoginForm loginDialog = new LoginForm())
                            {
                                loginDialog.ShowDialog();
                                loginDialog.Focus();
                                if (loginDialog.IsExit) return false;
                                else
                                    if (loginDialog.IsAuthorized) return true;
                                    else return l();
                            }
                        }; return l();
                    }
                  )()
                )) Close(); 
            else w8( () => LoadData() );
        }
    

    and here is some description for code :

        private void w8(Action action)
        {
            Cursor.Current = Cursors.WaitCursor;
            Application.DoEvents();
            action();
            Cursor.Current = Cursors.Default;
        }
    
    Public Class DamnLogin
        Private db As FRIIB
    
        Public Sub New(ByVal connection As String)
            db = New FRIIB(connection)
        End Sub
    
        Public Function Login(ByVal name As String, ByVal password As String) As Boolean
            Dim GetUser = _
               From u In db.GetTable(Of [User])() _
               Where u.Name = name _
               And u.Password = password _
               Select u
            Return GetUser.Count = 0
        End Function
    End Class
    
    let Login usename password = 
        new LinqBase.DamnLogin(insql) |> fun damn ->
            not <| damn.Login(usename,password)
    

    and login form

    public partial class LoginForm : Form
    {
        bool isAuthorized;
        bool exit;
    
        public bool IsAuthorized    { get { return this.isAuthorized;   } }
        public bool IsExit          { get { return this.exit;           } }
    
        public LoginForm()
        {
            isAuthorized    = false;
            exit            = false;
    
            InitializeComponent();
        }
    
        private void Close_Click(object sender, EventArgs e)
        {
            exit = true; 
            this.Close();
        }
    
        private void LoginButton_Click(object sender, EventArgs e)
        {
            if (Login.Text != "")
            {
                if (Login.Text.ToUpper() == "ADMIN")
                {
                    if (Password.Text == Crypto.DecryptStringAES(Model.DecryptRegisteryValue("Password"), "Password"))
                        isAuthorized = true;
                }
                else
                {
                    if (QueryBuilder.Login(Login.Text, Password.Text))
                        isAuthorized = true;
                }
            }
    
            this.Close();
        }
    }
    

提交回复
热议问题