Windows 身份验证 - 使用自定义条件设置角色

Windows Authentication - Set Roles with custom criteria

我已经开始使用 ASP.NET Web 表单(我完全是初学者)开发一个内联网网站,它使用 Windows 身份验证来识别用户,但为了控制对各种页面的访问,我'我希望根据 SQL table 秒内的数据设置的标准为用户分配角色(此数据每天都会更改)。

到目前为止,我有 'out of the box' ASP.NET 带有 Windows 身份验证的 Web 表单模板,它与我的(远程)SQL 服务器数据库建立了有效连接。

如果此问题已在其他地方得到解答,我深表歉意,但我似乎找不到适合我需要的解决方案。

使用一些基本的 IF 逻辑,我将有以下角色:'Admin'、'Moderator'、'HRA'、'Manager' 和 'Employee'。

从SQLtable(最多3-4个字段)中查找登录用户的数据,设置条件将确定用户的角色如下:

if (UserRole === null) Then
    If (ORG_ID === 30001000) Then
        UserRole === 'Admin'

    else if (ORG_ID === 30001001) Then
        UserRole === 'Moderator'

    else if (ORG_ID === 30001002) Then
        UserRole === 'HRA'

    else if (CHIEF === 'Chief') Then
        UserRole === 'Manager'

    else
        UserRole === 'Employee'
    End If
End if

我猜想这会在每个会话运行一次的 Site.Master 文件中起作用,但我不知道它究竟是如何工作的,以及是否需要将任何内容添加到配置文件中等等

提前致谢,我了解 php 的工作原理,但 ASP.NET 及其工作原理对我来说是全新的。如果有更好的解决方案那就太好了!

同样值得注意的是,我网站的某些部分(例如仪表板部分)将允许某些用户角色控制对由 SQL table 控制的仪表板的自定义访问 - 但我可以查看这在未来。

我想我会自己回答这个问题,以防它对其他人有用。我实现了自己的自定义角色提供程序并连接到 sql 数据以分配这样的角色:

public class CustomRoleProvider : RoleProvider
    {
        public override bool IsUserInRole(string username, string roleName)
        {
            var roles = GetRolesForUser(username);
            foreach (var role in roles)
            {
                if (role.Equals(roleName))
                {
                    return true;
                }
            }
            return false;
        }

        public override string[] GetRolesForUser(string username)
        {
            //create our List to hold our Roles
            List<string> r = new List<string>();

            r.Add("Employee");

            //get our sap number of current user to look up against the database
            var persno = Int32.Parse(10 + HttpContext.Current.User.Identity.Name.Substring(HttpContext.Current.User.Identity.Name.Length - 5));

            //connect to our sql database
            string strConnString = ConfigurationManager.ConnectionStrings["hrssportalConnectionString1"].ConnectionString;
            string str;
            SqlCommand com;
            SqlConnection con = new SqlConnection(strConnString);
            con.Open();

            //SQL Query
            str = "SELECT org_publisher.persno, org_publisher.record_type, org_publisher.org_string, map_user_roles.role_name FROM org_publisher LEFT JOIN users ON org_publisher.persno = users.persno LEFT JOIN map_user_roles ON users.role_id = map_user_roles.role_id WHERE org_publisher.persno = " + persno;
            com = new SqlCommand(str, con);

            //get our data
            //SqlDataReader reader = com.ExecuteReader();
            //reader.Read();

            DataTable dt = new DataTable();
            dt.Load(com.ExecuteReader());

            //if we have rows returned do our checks
            if (dt != null)
            {

                //get our data for checking
                //string org_string = reader["org_string"].ToString();
                //string line_manager = reader["record_type"].ToString();

                string org_string = dt.Rows[0]["org_string"].ToString();
                string line_manager = dt.Rows[0]["record_type"].ToString();

                //Line Manager Role check
                if (line_manager == "<ChiefPosition>")
                {
                    r.Add("Manager");
                }

                //HRSS Role Check
                if (org_string.Contains("30001803"))
                {
                    r.Add("HRSS");
                }

                //HRA Role Check
                if (org_string.Contains("30003237"))
                {
                    r.Add("HRA");
                }

                //add all custom roles by cycling through rows
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        if (row["role_name"].ToString() != null)
                        {
                            r.Add(row["role_name"].ToString());
                        }
                    }
                }

                //close our sql objects
                dt.Dispose();
                con.Close();

                //return List as an array
                string[] rolesArray = r.ToArray();
                return rolesArray;
            }
            else
            {
                //if no Rows returned from SQL, return only Employee role from List
                string[] rolesArray = r.ToArray();
                return rolesArray;
            }
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {

        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new System.NotImplementedException();
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName { get; set; }
    }

然后在web.config:

<roleManager defaultProvider="CustomRoleProvider" enabled="true">
  <providers>
    <clear/>
    <add name="CustomRoleProvider" type="ClassLibrary.CustomRoleProvider"
    applicationName="WebApplication1" writeExceptionsToEventLog="false"/>
  </providers>
</roleManager>