C# 管理员/用户登录时显示/隐藏按钮

发布于 2024-10-08 04:06:26 字数 733 浏览 0 评论 0原文

我正在编写一个具有客户端-服务器模型的应用程序。当客户端启动时,它将显示带有用户名和密码字段的登录表单。单击“登录”按钮后,客户端将发送这些用户名和密码。 pw到服务器来检查。如果服务器检查正常,则客户端主窗体将出现。我的代码是这样的:

在客户端,loginForm.cs:

if (execmd == "OK") // server sends "OK" or "FAILED" after checking authentication
    this.DialogResult = DialogResult.OK

在客户端,program.cs:

login = new loginForm();
DialogResult 结果 = login.showDialog();
登录.Dispose();
if (结果!= DialogResult.Cancel)
    Application.Run(new MainForm(登录));

现在我想通过管理员/用户身份验证改进我的应用程序。在MainForm中有一个设置按钮。如果用户登录,则该按钮将被隐藏。如果管理员登录,则会显示此按钮。使用上面的代码,如何根据服务器在检查身份验证后发送的字符串来使 MainForm Show() 或 Hide() 成为按钮?(例如 execmd == "admin" if管理员登录)。

预先感谢您的帮助。

I'm writing an app with client - server model. When the client starts, it will show the loginform with username and password field. When the Sign in button is clicked, client will send these username & pw to server to check. If server checks ok, then at client mainform will appear. my code is like this:

At client, loginForm.cs:

if (execmd == "OK") // server sends "OK" or "FAILED" after checking authentication
    this.DialogResult = DialogResult.OK

At client, program.cs:

login = new loginForm();
DialogResult result = login.showDialog();
login.Dispose();
if (result != DialogResult.Cancel)
    Application.Run(new MainForm(login));

Now I want to improve my app with admin/user authentication. In MainForm there's a Setting button. If users log in then this button will be hidden. If admin logs in then this button will be shown. With the above code, how do I do to make the MainForm Show() or Hide() the button depending on what string server will send after checking authentication?(for example execmd == "admin" if admin logs in).

Thanks in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年不长 2024-10-15 04:06:26

将现有逻辑保留在登录表单中,但添加一个测试以查看用户是否是管理员。然后在loginForm中设置一个布尔值,指示它是否是管理员(类似于IsAdmin)。

您已经将登录表单传递到 MainForm,因此您的 MainForm 可以检查 IsAdmin 属性以确定是否应显示该按钮。

btnSetting.Visible = login.IsAdmin;

Keep your existing logic in loginForm, but add a test to see if the user is the admin. And then set a boolean value in loginForm that indicates if it is the admin (something like IsAdmin).

You're already passing the loginForm to your MainForm, so your MainForm could then check the IsAdmin property to determine if the button should be displayed.

btnSetting.Visible = login.IsAdmin;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文