2017-10-12 86 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> 
相關問題