using System; using System.Data; using System.Data.SqlClient; public class UserManager { private string connectionString = "YourConnectionString"; public bool AuthenticateUser(string username, string password) { bool isAuthenticated = false; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string query = "SELECT COUNT(*) FROM Users WHERE Username = @Username AND Password = @Password"; using (SqlCommand command = new SqlCommand(query, connection)) { command.Parameters.AddWithValue("@Username", username); command.Parameters.AddWithValue("@Password", password); int count = (int)command.ExecuteScalar(); isAuthenticated = count > 0; } } return isAuthenticated; } public string GetUserRole(string username) { string role = string.Empty; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string query = "SELECT RoleName FROM Roles WHERE RoleID = (SELECT RoleID FROM Users WHERE Username = @Username)"; using (SqlCommand command = new SqlCommand(query, connection)) { command.Parameters.AddWithValue("@Username", username); role = (string)command.ExecuteScalar(); } } return role; } public bool HasPermission(string roleName, string permissionName) { bool hasPermission = false; using (SqlConnection connection = new SqlConnection(connectionString))
时间: 2024-02-14 16:27:20 浏览: 142
很抱歉,我之前的回答有些问题。这里是完整的示例代码:
```csharp
using System;
using System.Data;
using System.Data.SqlClient;
public class UserManager
{
private string connectionString = "YourConnectionString";
public bool AuthenticateUser(string username, string password)
{
bool isAuthenticated = false;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "SELECT COUNT(*) FROM Users WHERE Username = @Username AND Password = @Password";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@Username", username);
command.Parameters.AddWithValue("@Password", password);
int count = (int)command.ExecuteScalar();
isAuthenticated = count > 0;
}
}
return isAuthenticated;
}
public string GetUserRole(string username)
{
string role = string.Empty;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "SELECT RoleName FROM Roles WHERE RoleID = (SELECT RoleID FROM Users WHERE Username = @Username)";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@Username", username);
role = (string)command.ExecuteScalar();
}
}
return role;
}
public bool HasPermission(string roleName, string permissionName)
{
bool hasPermission = false;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "SELECT COUNT(*) FROM Permissions WHERE RoleID = (SELECT RoleID FROM Roles WHERE RoleName = @RoleName) AND PermissionName = @PermissionName";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@RoleName", roleName);
command.Parameters.AddWithValue("@PermissionName", permissionName);
int count = (int)command.ExecuteScalar();
hasPermission = count > 0;
}
}
return hasPermission;
}
}
```
请将 "YourConnectionString" 替换为您的实际数据库连接字符串。这是一个简单的用户管理类,包括身份验证、获取用户角色和检查权限的功能。您可以根据自己的需求进行修改和扩展。
希望这个示例能满足您的需求。如果还有其他问题,请随时提问!
阅读全文