⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 users.cs

📁 稿件名称:基于Visual C# 2005的自定义登录验证框的实现 源代码名称:source code 调试环境:Win XP;.Net
💻 CS
字号:

using System.Security.Principal;
using System.Threading;
using System.IO;
using System;
using System.Data;
using System.Windows.Forms;

public class Users
{

   public bool IsLogin(string strName, string strPassword)
   {// 用于判断账号是否存在于 XML文件中的方法

        DataSet dsUsers = new DataSet();
		DataRow[] drRows;
	   bool ret = false;

        try {      

            dsUsers.ReadXml(@"..\..\Users.xml");

            drRows = dsUsers.Tables[0].Select("name = '" + 
                        strName + "' and password = '" + strPassword + "'");

           		if (drRows.Length > 0) 
			{

				ret = true;
			}
			else 
			{
				ret = false;
			}

       } catch(FileNotFoundException e)
		{
            MessageBox.Show("Users.Xml 文件不存在。", "无法验证用户。", MessageBoxButtons.OK, MessageBoxIcon.Warning);
			Application.Exit();

        }

	   return ret;

    }

    public GenericPrincipal GetLogin(string strName, string strPassword)
		{
        /* 返回封装 GenericIdentity类型数据和用户角色名称的一般主体GenericPrincipal类型数据的方法*/

        DataSet dsUsers = new DataSet();
		DataRow[] drRows = null;

        try {

            // 将XML文件中记录的所有已注册的用户名和密码读入DataSet中

            dsUsers.ReadXml(@"..\..\Users.xml");
           /*通过在DataSet中搜索输入的用户名和密码,将用户完整的信息读入到drRows中*/
            drRows = dsUsers.Tables[0].Select("name = '" + 
                    strName + "' and password = '" + strPassword + "'");

       } catch( FileNotFoundException e)
		{
            MessageBox.Show("Users.Xml 文件不存在。","关闭中...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
			Application.Exit();

        }

         // 创建一个代表用户的GenericIdentity类型数据

        GenericIdentity GenIdentity = new GenericIdentity(strName);

         // 将角色关系定义成字符数组

        string[] Roles  = {Convert.ToString(drRows[0]["Role"]), ""};
      
        /*通过封装GenericIdentity类型数据和用户角色名称创建一个一般主体
		GenericPrincipal类型数据*/

        GenericPrincipal GenPrincipal = new GenericPrincipal(GenIdentity, Roles);
        return GenPrincipal;

    }

    public bool IsAdministrator()
	{
        
        //使用WindowsPrincipal类可以用来检查用户是否为管理员角色
       
        AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

        WindowsPrincipal WinPrincipal = (WindowsPrincipal) Thread.CurrentPrincipal;

        // 判断用户是否为管理员角色

		if (WinPrincipal.IsInRole(WindowsBuiltInRole.Administrator)) 
		{

			return true;
		}
		else 
		{

			return false;

		}

    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -