Click here to Skip to main content
14,640,679 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi all,

i have quite question.i have a small application.but there is a problem with it.i have a login form and main form(it is not an MDI form,just normal form).also i have got menu authentication with user who has got his own menu rights.if admin,he sees all menu rights.but if he is a user,he has rescricted rights not all of menu rights.

after i have loginned with correct username and password,i press OK button and close with Close() method and open main form and load menu rights with user.here is the problem:

When i call login form for a second time,i type username and password again and then press OK button,program doesnt load my main form for the new user rights and menu rights.after i pressed OK button on login form,it calls main form newly opened with new user and menu rights.BUT it doesnt?

i tried to call my login form from main form constructor or load event,but it doesnt work.

How could i do something like that? please help

thanks in advance.

Kerem

hi DavidKiryazi,Karthik. A,DaveAuld and all,

The codes that I tried to do this work.

I call main form from Program.cs
   [STAThreadAttribute]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new frmMainForm());

}



I added the login form calling code into the main form constructor
public frmMainForm()
 {
     InitializeComponent();


     frmUserLogin fUserLogin = new frmUserLogin();

     fUserLogin.ShowDialog();

 }



The login form OK button code that I added is below:


  private void btnLogin_Click(object sender, EventArgs e)
  {

 //in this area I am checking USername and password textboxes and etc.
 //   ....

     if (sPassword == txtPassword.Text)
      {


          this.Close();
      }
      else
      {
          MessageBox.Show("Geçersiz şifre", CGlobalVars.FCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
          txtPassword.Focus();
          return;
      }

   if (GetPermissions() == false)     // this if block is getting the menu item       {                       //permissions

          return;
      }

}


and I added this code to my menuitem click event
private void tsmiChangeUsers_Click(object sender, EventArgs e)
  {

          frmUserLogin fUserLogin = new frmUserLogin();

          fUserLogin.ShowDialog();
  }


These are the my running steps for program.

1.run program
2.enter username and pasword on login form.
3.call mainform and give permissions for menuitems which username is used.(if admin,give all permissions,else restrict permissions menu items' visibility)
4.now i want to change user when my main form is still on the screen.I call login from menuitem click.
5.after i have pressed login button on login form,load main form with new users rights and menu items permissions.

Till 4th step,it is OK but i couldnt figure out the fifth.when i run 5th step,the permissions belong to previous user.
How could I do this scenario.

[modify the original post if you are providing more information...don't post it as an answer]
Posted
Updated 29-Oct-10 6:03am
v2
Comments
DavidKiryazi 28-Oct-10 20:42pm
   
add some code kerem, will make it easier to find problem
Karthik. A 28-Oct-10 23:54pm
   
Yes, provide some code. I guess you are just showing the form again w/ Form.Show() after hiding the form for the 1st user/1st time. You need to re-construct the form using InitializeComponent() or the constructor that you show post login every time per user.
Rate this:
Please Sign up or sign in to vote.

Solution 3

It's not a good idea to have the Login dialog pop out of the main form constructor. A simpler design would be for the login dialog itself to be the startup form. On successful authentication, hide the login form and show the main form. If the user selects change-login from the menu, close the main form and re-show the login form. On successful re-authentication, bring up the main form again but this time with whatever data/UI is needed for the new user-login.
   
Comments
keremburak 29-Oct-10 13:51pm
   
also thanks for your opinion
Rate this:
Please Sign up or sign in to vote.

Solution 1

One method would be to create a class object that deals with the login and session.

This would be used by any form or other object that needs to know about the current logged in session.

Your login form and your mainform would use the same object and its shared member properties.

When the logged in user changes raise an event, which the mainform subscribes to and triggers the necessary code to change the menus.
   
Comments
keremburak 29-Oct-10 13:51pm
   
also thanks for your opinion
Rate this:
Please Sign up or sign in to vote.

Solution 4

this is definitely not the way that I would do it,

but with the code you provided, instead of opening a new login form, you want to create a new main form and then load it. So, in whatever method that you want to do the re-login, it would look like:

Me.Hide();
MainForm newMainForm = new MainForm();
newMainForm.Show();
Me.Close();


Another thing that you could do is once the ShowDialog() for the login form is complete, reset your main form. With your code, it probably just means another call to the main form's load event.

Though honestly, I can't see how you are actually telling the main form what kind of user rights they have. Yes, you have them log in, but nowhere do you tell the main form whether it was successful or what kind of user it was that logged in. By your code, it looks like the user could just as easily close the log in screen and still get the same results.


Really, though, you should have the login form be the startup form. Then, when the login is successful, it opens up the main form.

You need to think through logically what is going on and you would see why this is the case.

In your menu item, all that you do is create a new login form. But, what does the login form do when you click the button to log in?

It simply closes the login form. So, why would you expect it to close the main form and then open a new main form.
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 5

hi William,

i didnt give the whole code.if I need to start over,i did all these tasks database-based security.i have role table,user table,and menu table.which user entered at this point,i get the permissions from database with menu rights and load the main form with menus for this user.my login form whole code is below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using librarian.BOLayer;

namespace librarian
{
    public partial class frmUserLogin : Form
    {
        CDBProcess CDBP = new CDBProcess();

        private static int iUserNo;
        private static string sUserName;
        private string sPassword;

        public frmUserLogin()
        {
            InitializeComponent();
        }

        private void frmUserLogin_Load(object sender, EventArgs e)
        {

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {

            if (txtUserName.Text == "")
            {
                MessageBox.Show("Username must be entered", CGlobalVars.FCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUserName.Focus();
                return;
            }
            else if (FindBlank(txtUserName.Text))
            {
                lblWarning.Visible = true;
                txtUserName.Focus();
                return;         
            }
            else
            {
                lblWarning.Visible = false;
                txtPassword.Focus();
            }

            DataTable dTable = CDBP.FindUser(txtUserName.Text.Trim()).Tables[0];

            if (dTable.Rows.Count == 0)
            {
                MessageBox.Show("Invalid username!", CGlobalVars.FCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUserName.Focus();
                return;
            }

            iUserNo = (int)dTable.Rows[0][0];
            sUserName = dTable.Rows[0][1].ToString();
            sPassword = dTable.Rows[0][2].ToString();

            if (txtPassword.Text == "")
            {
                MessageBox.Show("Password must be entered", CGlobalVars.FCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPassword.Focus();
                return;
            }

            if (sPassword == txtPassword.Text)
            {

                //Thread thRunForm = new Thread(new ThreadStart(RunMainForm));
                //thRunForm.Start();

                //if (CGlobalVars.Is_fUserLoginOpened==true)
                //{
                   

                //    this.Close();

            // frmMainForm f=new frmMainForm();
             // f.Show();
                //}
                //else 
                //{
                //    this.Close();
                //}

                //return;

                this.Close();
            }
            else
            {
                MessageBox.Show("Invalid password", CGlobalVars.FCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPassword.Focus();
                return;
            }

            if (GetPermissions() == false)
            {
                return;
            }


        }

        private bool GetPermissions()
        { 
            DataTable dTable=new DataTable();

            dTable = CDBP.GetPermissionListForUser(iUserNo).Tables[0];

            if(dTable.Rows.Count == 0)
            {
                return false;
            }
            for (int i=0;i<dTable.Rows.Count;i++)
            {
                int iIndex = (int)dTable.Rows[i][0];
                CGlobalVars.bPermissionArray[iIndex]=(bool)dTable.Rows[i][1];
                CGlobalVars.sArray[iIndex] = dTable.Rows[i][1].ToString();
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private bool FindBlank(string sText)
        { 
            bool isBlank=false;

            for (int i=0; i < sText.Length; i++)
            {
                if (sText.Substring(i, 1) == Convert.ToChar(32).ToString())
                    isBlank = true;
            }

                return isBlank;
        }

        private void txtUserName_Leave(object sender, EventArgs e)
        {
            if (txtUserName.Text == "")
            {
                MessageBox.Show("You must enter username.", CGlobalVars.FCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUserName.Focus();
                return;
            }
            else if (FindBlank(txtUserName.Text))
            {
                lblWarning.Visible = true;
                txtUserName.Focus();
                return;
            }
            else
            {
                lblWarning.Visible = false;
                txtPassword.Focus();
            }
        }
    }
}


and the other methods for main form codes are;

  private void LoadMenusForUser()
    {
        mFile.Visible = CGlobalVars.bPermissionArray[1];
        mMenu.Visible = CGlobalVars.bPermissionArray[2];
        mSettings.Visible = CGlobalVars.bPermissionArray[3];
        mAbout.Visible = CGlobalVars.bPermissionArray[4];
        tsmiBackupDB.Visible = CGlobalVars.bPermissionArray[5];
        tsmiExit.Visible = CGlobalVars.bPermissionArray[6];
        tsmiRecordOperations.Enabled = CGlobalVars.bPermissionArray[7];
        tsmiLists.Visible = CGlobalVars.bPermissionArray[8];
        tsmiLoanOperations.Visible = CGlobalVars.bPermissionArray[9];
        tsmiUserOperations.Visible = CGlobalVars.bPermissionArray[10];
        tsmiChangePassword.Visible = CGlobalVars.bPermissionArray[11];
        tsmiChangeUsers.Visible = CGlobalVars.bPermissionArray[12];
        tsmiHelp.Visible = CGlobalVars.bPermissionArray[13];
        tsmiAbout.Visible = CGlobalVars.bPermissionArray[14];
        tsmiRecDirectives.Visible = CGlobalVars.bPermissionArray[15];
        tsmiRecCulturalPubs.Visible = CGlobalVars.bPermissionArray[16];
        tsmiRecEducationMedia.Visible = CGlobalVars.bPermissionArray[17];
        tsmiRecMember.Visible = CGlobalVars.bPermissionArray[18];
        tsmiLibraryList.Visible = CGlobalVars.bPermissionArray[19];
        tsmiMemberList.Visible = CGlobalVars.bPermissionArray[20];
        tsmiLoanDirectives.Visible = CGlobalVars.bPermissionArray[21];
        tsmiLoanCulturalPubs.Visible = CGlobalVars.bPermissionArray[22];
        tsmiLoanEducationMedia.Visible = CGlobalVars.bPermissionArray[23];
    }

    void printPerm()
    {
        for (int i = 1; i < CGlobalVars.sDizi.Length; i++)
        {
            if (CGlobalVars.sDizi[i] == null)
                continue;
            else
            lb.Items.Add(CGlobalVars.sDizi[i].ToString());
        }
    }

    private void tsmiChangeUsers_Click(object sender, EventArgs e)
    {

            frmUserLogin fUserLogin = new frmUserLogin();

            fUserLogin.ShowDialog();


    }


}


private void mainForm_Load(object sender, EventArgs e)
   {
       LoadMenusForUser();
   }


what is the your opinion after this?

how can we do this scneario? is this applicable or not?

thanks

Kerem.
   
Rate this:
Please Sign up or sign in to vote.

Solution 6

Do one thing add user role into the windows registry and pick the role from main form load event and populate the menu accordingly .
Like this:-
RegistryKey regKeyApplRoot = Registry.logedUser.CreateSubKey(sPath);

Imran Parvez
MCTS
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100