I see the similar question asked 13 years ago but I can't seem to make that work the way I want. I have a windows forms app that needs to pass an object I created to store user entered credentials to authenticate to Active Directory to do do actions within a windows domain environment. Form1 is my Login page, Form2 is my main menu to access all other functionality. The login page creates the auth object and I need that object to be passed to every other form so it can be accessed permanently for as long as the application is open. I tried passing it to the other form at creation but Form2 doesn't seem to recognize it's properties.
public partial class LoginMenu : Form
{
public LoginMenu()
{
InitializeComponent();
}
void ButtonLogin_Click_1(object sender, EventArgs e)
{
Credentials auth = new()
{
Username = textBoxUsername.Text,
Password = textBoxPassword.Text
};
try
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "DOMAIN REDACTED"))
{
bool isValid = pc.ValidateCredentials(auth.Username, auth.Password);
if (isValid)
{
this.Hide();
var mainmenu = new MainMenu();
mainmenu.FormClosed += (s, args) => this.Close();
mainmenu.Show();
}
else
{
MessageBox.Show("Login Failed. Please check your username and passowrd and try again.");
auth.Username = string.Empty;
auth.Password = string.Empty;;
}
}
}
catch (Exception ex)
{
// todo catch exception
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
what do I need to do to this object to send it to other forms when the loginmenu is closed? Do I have to edit the mainmenu so it takes the credential object and pass it when it instantiates it? I tried to do that but for some reason the auth.username and auth.password were empty.
I see the similar question asked 13 years ago but I can't seem to make that work the way I want. I have a windows forms app that needs to pass an object I created to store user entered credentials to authenticate to Active Directory to do do actions within a windows domain environment. Form1 is my Login page, Form2 is my main menu to access all other functionality. The login page creates the auth object and I need that object to be passed to every other form so it can be accessed permanently for as long as the application is open. I tried passing it to the other form at creation but Form2 doesn't seem to recognize it's properties.
public partial class LoginMenu : Form
{
public LoginMenu()
{
InitializeComponent();
}
void ButtonLogin_Click_1(object sender, EventArgs e)
{
Credentials auth = new()
{
Username = textBoxUsername.Text,
Password = textBoxPassword.Text
};
try
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "DOMAIN REDACTED"))
{
bool isValid = pc.ValidateCredentials(auth.Username, auth.Password);
if (isValid)
{
this.Hide();
var mainmenu = new MainMenu();
mainmenu.FormClosed += (s, args) => this.Close();
mainmenu.Show();
}
else
{
MessageBox.Show("Login Failed. Please check your username and passowrd and try again.");
auth.Username = string.Empty;
auth.Password = string.Empty;;
}
}
}
catch (Exception ex)
{
// todo catch exception
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
what do I need to do to this object to send it to other forms when the loginmenu is closed? Do I have to edit the mainmenu so it takes the credential object and pass it when it instantiates it? I tried to do that but for some reason the auth.username and auth.password were empty.
Share Improve this question edited Mar 24 at 17:00 SignalRaptor asked Mar 24 at 16:51 SignalRaptorSignalRaptor 294 bronze badges 3- 1 The "credentials object" comes before the forms that need it; so "passing" between forms is misguided. You should have a "static" (readonly) method or field or "factory" that provides the "credential object" to anyone that asks for it. – Gerry Schmitz Commented Mar 24 at 17:00
- @Gerry Schmitz Where would that be instantiated? in Program.cs before it runs the LoginMenu form? – SignalRaptor Commented Mar 24 at 17:02
- The following may be of interest: stackoverflow/a/69743297/10024425 – It all makes cents Commented Mar 24 at 17:49
2 Answers
Reset to default 0Ok so it turns out that I am incredibly misinformed about how static classes and methods work. I just have this declared in my Program.cs file before the Program class
public static class Credentials
{
public static string Username { get; set; } = string.Empty;
public static string Password { get; set; } = string.Empty;
}
and then this in my form1 class
void ButtonLogin_Click_1(object sender, EventArgs e)
{
Credentials.Username = textBoxUsername.Text;
Credentials.Password = textBoxPassword.Text;
try
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "DOMAIN REDACTED"))
{
bool isValid = pc.ValidateCredentials(Credentials.Username, Credentials.Password);
if (isValid)
{
this.Hide();
var mainmenu = new MainMenu();
mainmenu.FormClosed += (s, args) => this.Close();
mainmenu.Show();
}
else
{
MessageBox.Show("Login Failed. Please check your username and passowrd and try again.");
Credentials.Username = string.Empty;
Credentials.Password = string.Empty;;
}
}
}
catch (Exception ex)
{
// todo catch exception
}
Do you need explanation how static classes work or do you know how they work now?
The way you wrote it only 1 User credentials can be saved at a time. If you have multiple users it would be better to have some data structure as storage of your users and pass reference to this structure in constructor of Form or you could have this structure as static attribute in your current Credentials class (this would be easier so you don't have to change a lot of your existing code)
for example you could use "public static List<(string, string)> list = new List<(string, string)>();"
to add items you just use "list.Add(("userEmail","userPassword"));". To get data you would have to iterate using loop of your choice. If you choose anything that isn't foreach you have to access data using indexer (like array) => list[indexOfUser].Item1/list[0].Item2; -with Item1 and Item2 you access each individual string, so for you Item1 is userEmail and Item2 is userPassword.
and for validation you can check if inserted email is in your list using loop and checking Item1. If you find a match check password.