Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi. I have a main form. when I click btnMain, a LoginForm will be open.in loginForm I clicked a button(btnok).in the event I call a SecureMethod from main form, doing something and show MainForm again(textbox values don't change). I want this calling(SecureMethod) be very secure and when I turn back ro MainForm Textbox values haven't change.Thanks in advance
C#
public partial class MainForm : Form
{
   private void btnMain_Click(object sender, EventArgs e)
    {
        LoginForm lfrm = new LoginForm;
        LoginForm.ShowDialog();
    }
   private void SecureMethod(){//do sth};
}
public partial class LoginForm : Form
{
   private void btnok_Click(object sender, EventArgs e)
    {
        SecureMethod(); //is not true
        this.close(); //close loginform
    }
}
Posted

Remember that forms are essentially classes. Private methods can not be called outside of their class, that's how the information is kept private. You will need a public method that can access the private method.

For a simple example you can check this nice tutorial.
C# Private Method[^]
 
Share this answer
 
If SecureMethod is private, then you can never call it in another form. I think that you want that SecureMethod is only accessible in the same assembly, so use internal[^]. Then, you'll need to pass the MainForm to the LoginForm:
C#
public partial class MainForm : Form
{
   private void btnMain_Click(object sender, EventArgs e)
    {
        LoginForm lfrm = new LoginForm;
        lfrm.mainFrm = this;
        LoginForm.ShowDialog();
    }
   internal void SecureMethod()
   {
       //do sth
   }
}
public partial class LoginForm : Form
{
   public MainForm mainFrm = null;
   private void btnok_Click(object sender, EventArgs e)
    {
        if (mainFrm != null)
        {
            SecureMethod(); //is not true
            this.Close(); //close loginform
        }
        else
        {
             // mainFrm is null, you can't call SecureMethod
        }
    }
}
 
Share this answer
 
v4
Comments
mit62 29-Jan-14 15:29pm    
thanks a lot. it works. but if i set my Securemethod Internal, I think its less secure because as you said other classes in assembly can access. is there a way that LoginForm inherit Mainform?(so securMethode will be protected and I think more secure than Internal)
Thomas Daniels 30-Jan-14 11:41am    
What do you mean with 'less secure'? If the classes that are only in your assembly can access SecureMethod, then that's not really a problem. There is only a security problem if classes outside your assembly can access it, but using internal solves that problem.
mit62 30-Jan-14 11:43am    
thanks your suggestion.
Since security in a log-in scenario appears to be your goal here, I'd base the "architecture" of the code on that. Of course, there's different degrees of security, ranging from lightweight techniques designed to stop "casual" hacking, then on to using Windows cryptography tools in .NET, up to very expensive professional third-party tools that integrate with databases and networks.

I'll illustrate with a sketch of a WinForms architecture for a "light-weight" secure log-in that is based on the following principles:

1. the Application's Main Form should not be created until the user has successfully logged in.

2. there should be absolutely no "coupling" (dependency) between the log-in process and the main Application. The Log-in Form should have no "knowledge or awareness" of the Main Form, and the Main Form have no knowledge or awareness of the Log-in Form.

Structure:

1. Two Forms, LogIn, and MainForm.

a. LogIn: Buttons btnCancel and btnLogIn, Textbox tbUserName for user name, MaskedTextBox tbMaskedPassword for password entry.

b. MainForm: one Label, 'lblUserInfo

2. The .NET WinForms app is set to be a single start-up Project, and the Program.cs file, as usual, set to be the start-up Object.

The Program.cs file Main method:
C#
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new LogIn());
    Application.Run(new MainForm());
}
This variation on the standard WinForms Program.cs file exhibits the following behavior:

1. first a LogIn Form will be created and shown, and the Application will not continue until the LogIn Form is disposed (closed).

2. after the LogIn Form is closed, then the MainForm is created and shown.

3. calling Application.Exit() at any time in the LogIn Form will terminate the Application.

LogIn Form:
C#
public partial class LogIn : Form
{
    public LogIn()
    {
        InitializeComponent();
    }

    // note the use of static variables here !
    private static int nTries = 3;
    private static int tryNumber = 1;

    private void btnLogin_Click(object sender, EventArgs e)
    {
        if (tryNumber > nTries)
        {
            this.Hide();
            MessageBox.Show("Access denied");
            Application.Exit();
        }

        tryNumber++;

        // simulate SecureMethod #1 ...
        if(tbMaskedPassword.Text == "true" && tbUserName.Text == "User")
        {
            // see definition of the LogInData Class
            LogInData.logInOk = true;
            LogInData.uName = tbUserName.Text;
            this.Close();
        }
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}
LogInData static Class:
C#
public static class LogInData
{
    public static bool logInOk { get; set; }
    public static string uName { get; set; }
}
Discussion:

1. static variables are used in the LogIn Form to count the number of attempts to log-in; if the limit is exceeded, a log-in denied message is shown, and the Application is terminated.

2. if the log-in is successful, the user name and password entered is saved by setting the static variables in the static LogInData class, at which point the LogIn Form is closed.

In the MainForm's Load EventHandler:
C#
private void Form1_Load(object sender, EventArgs e)
{
    if(LogInData.logInOk)
    {
        // call SecureMethod #2 here ...
        // if SecureMethod #2 fails: Appliction.Exit();

        lblUserInfo.Text += LogInData.uName + " logged in: " + DateTime.Now.ToLongDateString();
    }
    else
    {
        Application.Exit();
    }
}
There's an extra ... and really unnecessary level of further validation sketched in here just for the sake of raising the possibility of two-level validtion. The way the code is now, the MainForm should never be loaded/shown if the log-in process failed.

Big picture:

1. in this case the static LogInData Class is used as a repository for the result of a successful log-in
 
Share this answer
 
Comments
Karthik_Mahalingam 29-Jan-14 20:59pm    
5, neat
mit62 29-Jan-14 23:34pm    
oh wonderful answer. thanks a lot, you learn me a lot.
I have a plus question.If this application be installed on server and be multi user, does static variable make problem or not?
BillWoodruff 30-Jan-14 1:47am    
Multi-user ? If each user of the Application uses a separate process launched by the Server, I don't think there'll be a problem. Is there another scenario you have in mind ?

I'm glad you found some value in my reply. cheers, Bill
mit62 30-Jan-14 2:26am    
if you think there is no problem so I accept.thanks
Thomas Daniels 30-Jan-14 11:45am    
Your answer is great! +5!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900