你好,我重新用教程里的C#工程修改了,并生成了类没有报错,但是上传到活字格工程还是不行。
我用C#写的窗体是可以读取到MYsql的用户表的。
using GrapeCity.Forguncy.SecurityProvider;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;
using openWeaverSecurityProvider;
using System.Collections;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;
using MySql.Data.MySqlClient;
namespace WeaverPasswordSecurityProvider
{
public class WeaverPasswordSecurityProvider : ISecurityProvider ,ISupportSettings
{
public string Name
{
get
{
return "WeaverPasswordSecurityProvider";
}
}
public AuthenticationType AuthenticationType
{
get
{
return AuthenticationType.UserNameAndPassword;
}
}
public UserInformationStorageMode UserInformationStorageMode
{
get
{
return UserInformationStorageMode.InMemoryCache;
}
}
public UserInformations UserInformations
{
get
{
//创建UserInformations对象
var userInfo = new UserInformations();
//数据库连接字符串
string constr = "server=172.18.18.79;port=3336;user=oa;password=123456;database=TD_OA";
MySqlConnection con = new MySqlConnection(constr);
//打开数据库链接
con.Open();
//从数据库中获取用户信息,返回userInfo
var userTable = GetUser(con, userInfo);
//从数据库中获取角色信息,返回userInfo
GetRoles(con, userInfo, userTable);
//从数据库中获取组织信息,返回userInfo
GetDepartment(con, userInfo, userTable);
//关闭数据库连接
con.Close();
//将获取到的userInfo返回
return userInfo;
}
}
private Dictionary<int, User> GetUser(MySqlConnection con, UserInformations userInfo)
{
string sql = string.Format("select * from user");
var dataTable = GetDataTable(sql, con);
try
{
Dictionary<int, User> userDic = new Dictionary<int, User>();
for (int i = 0; i < dataTable.Rows.Count; i++)
{
var row = dataTable.Rows;
var user = new User()
{
UserId = row["user_id"].ToString(),
FullName = row["user_name"].ToString(),
Email = row["email"].ToString(),
};
user.Properties.Add("性别", row["sex"].ToString());
user.Properties.Add("生日", row["birthday"].ToString());
user.Properties.Add("国籍", row["useing_key"].ToString());
user.Properties.Add("系统语言", row["using_finger"].ToString());
user.Properties.Add("手机", row["mobile_no"].ToString());
user.Properties.Add("电话", row["tel_no_detp"].ToString());
userInfo.Users.Add(user);
userDic.Add((int)row["id"], user);
}
return userDic;
}
catch (Exception e)
{
throw;
}
finally
{
}
}
private void GetRoles(MySqlConnection con, UserInformations userInfo, Dictionary<int, User> userDic)
{
var roleTable = GetDataTable("select priv_no,priv_name from user_priv", con);
var roleMemberTable = GetDataTable("select user_priv,user_priv_no from user", con);
var roleMemberList = GetList(roleMemberTable);
try
{
for (int i = 0; i < roleTable.Rows.Count; i++)
{
var roleRow = roleTable.Rows;
var role = new Role()
{
Name = roleRow["priv_name"].ToString()
};
var members = roleMemberList.Where(r => object.Equals(r["uid"], roleRow["priv_no"]));
foreach (var item in members)
{
var uId = (int)item["user_priv"];
if (userDic.ContainsKey(uId))
{
role.Users.Add(userDic[uId]);
}
}
userInfo.Roles.Add(role);
}
}
catch (Exception e)
{
throw;
}
finally
{
}
}
private void GetDepartment(MySqlConnection con, UserInformations userInfo, Dictionary<int, User> userDic)
{
var departmentTable = GetDataTable("select dept_id, dept_name, dept_no from department", con);
var userTable = GetDataTable("select uid,user_id, dept_id from user", con);
var DepartMemberList = GetList(userTable);
List<OrganizationInfo> orgList = new List<OrganizationInfo>();
try
{
for (int i = 0; i < departmentTable.Rows.Count; i++)
{
var departmentRow = departmentTable.Rows;
int? superID = null ;
try
{
superID = Convert.ToInt32(departmentRow["dept_no"]);
}
catch (Exception)
{
superID = null;
}
var current = new OrganizationInfo()
{
ID = Convert.ToInt32(departmentRow["id"]),
parentID = superID,
Node = new Organization()
{
Name = departmentRow["dept_name"].ToString()
}
};
orgList.Add(current);
}
userInfo.Organizations.AddRange(AddSubOrganization(null, orgList));
}
catch (Exception e)
{
throw;
}
finally
{
}
}
private List<Organization> AddSubOrganization(int? parentId, List<OrganizationInfo> allNodes)
{
var result = new List<Organization>();
foreach (var node in allNodes)
{
if(node.parentID == parentId)
{
result.Add(node.Node);
node.Node.SubOrganizations.AddRange(AddSubOrganization(node.ID, allNodes));
}
}
return result;
}
private List<DataRow> GetList(DataTable dataTable)
{
List<DataRow> list = new List<DataRow>();
for (int i = 0; i < dataTable.Rows.Count; i++)
{
list.Add(dataTable.Rows);
}
return list;
}
private DataTable GetDataTable(string sql, MySqlConnection con)
{
using (MySqlCommand com = new MySqlCommand(sql, con))
{
using (var dataAdapter = new MySqlDataAdapter(com))
{
var dataTable = new DataTable();
dataAdapter.Fill(dataTable);
return dataTable;
}
};
}
public bool AllowLogout
{
get
{
return true;
}
}
public List<SecurityProviderSettings> Settings => new List<SecurityProviderSettings>();
public User VerifyUser(Dictionary<string, string> properties)
{
var userName = properties["userName"];
var password = properties["password"];
string constr = "server=172.18.18.79;port=3336;user=oa;password=123456;database=TD_OA";
MySqlConnection con = new MySqlConnection(constr);
con.Open();
string sql = string.Format("select * from user where user_id = '{0}'", userName);
var loginTable = GetDataTable(sql, con);
try
{
for (int i = 0; i < loginTable.Rows.Count; i++)
{
var login = loginTable.Rows;
var loginid = login["user_id"].ToString();
var userpassword = login["password"].ToString();
if (userName != null && password != null)
{
var md5password = GetMD5(password);
if (userpassword.ToUpper() == md5password.ToUpper())
{
return new User
{
UserId = userName,
FullName = userName
};
}
}
return null;
}
return null;
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
public void UpdateSetting(Dictionary<string, object> dictionary){}
public static string GetMD5(string str)
{
var md5 = System.Security.Cryptography.MD5.Create();
byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data.ToString("x2"));
}
string s = sBuilder.ToString();
return s;
}
}
} |