存储/访问用户访问角色 C# Winforms
我设计了几个客户端/服务器应用程序。我正在开发一个项目,该项目涉及用户登录以访问应用程序。我正在寻找登录应用程序后存储用户权限的最有效且“简单”的方法,该方法可以在限制对主表单上某些选项卡的访问的整个过程中使用。
我创建了一个名为“User”的静态类,详细信息如下:
static class User
{
public static int _userID;
public static string _moduleName;
public static string _userName;
public static object[] UserData(object[] _dataRow)
{
_userID = (int)_dataRow[0];
_userName = (string)_dataRow[1];
_moduleName = (string)_dataRow[2];
return _moduleName;
}
}
当用户登录并且已通过身份验证时,我希望将 _moduleName 对象存储在内存中,以便我可以控制他们可以访问主表单选项卡控件上的哪些选项卡,例如例子;如果用户在数据库中被分配了以下角色:“销售分类帐”、“采购分类帐”,一旦登录表单被隐藏并且登录表单被隐藏,则通过使用 Switch - Case 块,他们只能看到表单上的相关选项卡。主窗体被实例化。一旦加载,我可以将 userID 和 userName 变量存储在主表单中,例如:
这里我们处理来自用户的登录数据:
DataAccess _dal = new DataAccess();
switch (_dal.ValidateLogin(txtUserName.Text, txtPassword.Text))
{
case DataAccess.ValidationCode.ConnectionFailed:
MessageBox.Show("Database Server Connection Failed!");
break;
case DataAccess.ValidationCode .LoginFailed:
MessageBox.Show("Login Failed!");
_dal.RecordLogin(out errMsg, txtUserName.Text, workstationID, false);
break;
case DataAccess.ValidationCode .LoginSucceeded:
frmMain frmMain = new frmMain();
_dal.GetUserPrivList(out errMsg,2); //< here I access my DB and get the user permissions based on the current login.
frmMain.Show();
this.Hide();
break;
default:
break;
}
private void frmMain_Load(object sender, EventArgs e)
{
int UserID = User._userID;
}
这很好用,但是 _modules 对象包含多个权限/角色,具体取决于已执行的内容在数据库中设置后,如何存储多个值并通过 Switch-Case 块访问它们?
I have designed several Client/Server applications. I am working on a project that involves a user logging in to gain access to the application. I am looking at the most efficient and "simple" method of storing the users permissions once logged in to the application which can be used throughout restricting access to certain tabs on the main form.
I have created a static class called "User" detailed below:
static class User
{
public static int _userID;
public static string _moduleName;
public static string _userName;
public static object[] UserData(object[] _dataRow)
{
_userID = (int)_dataRow[0];
_userName = (string)_dataRow[1];
_moduleName = (string)_dataRow[2];
return _moduleName;
}
}
When the user logs in and they have been authenticated, I wish to store the _moduleName objects in memory so I can control which tabs on the main form tab control they can access, for example; if the user has been assigned the following roles in the database: "Sales Ledger", "Purchase Ledger" they can only see the relevant tabs on the form, by way of using a Switch - Case block once the login form is hidden and the main form is instantiated. I can store the userID and userName variables in the main form once it loads by means of say for example:
Here we process the login data from the user:
DataAccess _dal = new DataAccess();
switch (_dal.ValidateLogin(txtUserName.Text, txtPassword.Text))
{
case DataAccess.ValidationCode.ConnectionFailed:
MessageBox.Show("Database Server Connection Failed!");
break;
case DataAccess.ValidationCode .LoginFailed:
MessageBox.Show("Login Failed!");
_dal.RecordLogin(out errMsg, txtUserName.Text, workstationID, false);
break;
case DataAccess.ValidationCode .LoginSucceeded:
frmMain frmMain = new frmMain();
_dal.GetUserPrivList(out errMsg,2); //< here I access my DB and get the user permissions based on the current login.
frmMain.Show();
this.Hide();
break;
default:
break;
}
private void frmMain_Load(object sender, EventArgs e)
{
int UserID = User._userID;
}
That works fine, however the _modules object contains mutiple permissions/roles depending on what has been set in the database, how can I store the multiple values and access them via a Switch-Case block?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,您希望能够将权限/角色作为每个用户的数据库中的值存储。
我相信您可以将整数写入数据库字段,例如:
RolesID
。将标志的值写入数据库字段。类似的东西:您应该能够读取该值并将其分配给声明为的字段:
我很确定我以前已经这样做过。
哈...
If I am understanding properly, you want to be able to store permissions/roles as a value in a database per user.
I believe you can write an integer to a database field like :
RolesID
. Write to you database field the value of the flag. Something like:You should be able to read that value in and assign to a field declared as:
I'm pretty certain I've done this before.
HTH...