2017-10-12 146 views
0

我已经开始使用ASP.NET Web窗体(我是一个总的初学者)开发一个使用Windows身份验证来识别用户,但为了控制对各种页面的访问的Intranet网站, m希望根据基于SQL表中的数据的设置标准为用户分配角色(此数据可以每天更改)。Windows身份验证 - 使用自定义标准设置角色

到目前为止,我拥有'开箱即用'的带有Windows身份验证的ASP.NET Web窗体模板,它与我的(远程)SQL Server数据库有工作连接。

我很抱歉如果这已在其他地方得到解答,但我似乎无法找到适合我需求的解决方案。使用一些基本的IF逻辑,我将具有以下角色:'Admin','Moderator','HRA','Manager'和'Employee'。

仰望登录从SQL表用户的数据(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和它的工作原理对我来说是全新的。如果有更好的解决方案,那么太棒了!

同样值得注意的是,我网站的某些部分(例如Dashboards部分)将允许一些UserRoles控制对由SQL表控制的仪表板的自定义访问 - 但我可以在将来看到这一点。

回答

0

我以为我会自己回答这个,只是说它对任何人都有用。我实现了自己的自定义角色提供并连接到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> 
相关问题