ArcGIS.Server.9.2.DotNet自带例子分析(八、一)

目的:
1.arcgis server9.2 ADF实现根据用户权限不同对地图功能进行控制

准备工作:
1.用ArcGis Server Manager或者ArcCatalog发布一个叫usa的Map Service,并且把这个Service启动起来。
2.找到DeveloperKit\SamplesNET\Server\Web_Applications目录下的Common_SecurityCSharp.zip。

开始:
1.Esri提供的范例是用MDF数据库和微软提供的Membership进行用户的创建登录管理等,这里为了方便没有用数据库了就写死2个权限不同的用户admin/admin,user/user。
2.新建名为Security的ASP.NET Web应用程序,然后新建Login.aspx页面用来进行用户的登录,一个用户名输入框;一个密码输入框;一个登录按钮就可以了非常的简单了不详细说明了,具体看一下登录代码:

 1//登录按钮的事件
 2protected void Button1_Click(object sender, EventArgs e)
 3        {
 4            if (TextBox1.Text == "admin" && TextBox2.Text == "admin")
 5            {
 6                Session["user"= "admin";
 7                Session["type"= "管理员";
 8                Response.Redirect("Default.aspx");
 9            }

10            else if (TextBox1.Text == "user" && TextBox2.Text == "user")
11            {
12                Session["user"= "user";
13                Session["type"= "普通用户";
14                Response.Redirect("Default.aspx");
15            }

16            else
17            {
18                Label1.Text = "登录失败!";
19            }

20        }
3.然后在Default.aspx页面中添加一个Label1用来显示用户登录信息,然后在cs代码中添加方法Page_PreRenderComplete,在这个方法中判定用户是否登录没有登录就跳到Login.aspx页面。
Code
4.在Default.aspx页面上添加MapResourceManager1、Map1、Toc1、Toolbar1,然后对这些控件做相应的设置了,这个比较简单了前面的文章都已经说了很详细了。
5.在Toolbar1中添加一个Command,用来获取地图和图层的信息,添加一个<div id="divMapInfo"></div> 用来显示获取的信息。当只有管理组的时候才可以使用这个功能。具体的定义如下:
1<esri:Command ClientAction="" DefaultImage="~/identify_1.gif" JavaScriptFile="" Name="GetMapInfo" ServerActionAssembly="Security" ServerActionClass="Security.GetMapInformation" Text="Info" ToolTip="Get information about the map layers" />
GetMapInformation的代码如下:
 1namespace Security
 2{
 3    public class GetMapInformation : IMapServerCommandAction
 4    {
 5        public void ServerAction(ToolbarItemInfo info)
 6        {
 7            //获取地图控件
 8            Map mapCtrl = (Map)info.BuddyControls[0];
 9            //获取地图和图层信息
10            string mapInfo = GetMapAndLayerInformation(mapCtrl);
11            //把地图和图层信息显示在divMapInfo内
12            CallbackResult cbr = new CallbackResult("HtmlGenericControl""divMapInfo""innerContent", mapInfo);
13            mapCtrl.CallbackResults.Add(cbr);
14        }

15
16        private string GetMapAndLayerInformation(Map mapControl)
17        {
18            //初始化Functionalities
19            if (!mapControl.InitializedFunctionalities)
20            {
21                mapControl.InitializeFunctionalities();
22            }

23            System.Text.StringBuilder sbInfo = new System.Text.StringBuilder("<h2>Map Information:</h2>");
24            string[] fields, layerIDs, layerNames;
25            //获取MapFunctionality
26            foreach (IMapFunctionality mapFunct in mapControl.GetFunctionalities())
27            {
28                IGISResource gisResource = mapFunct.Resource;
29                //获取ResourceDefinition、Name用来显示
30                sbInfo.AppendFormat("<h3>Service: {0} ({1})</h3>", gisResource.ResourceDefinition, gisResource.Name);
31                if (gisResource.SupportsFunctionality(typeof(IQueryFunctionality)))
32                {
33                    //创建QueryFunctionality
34                    IQueryFunctionality queryFunct =(IQueryFunctionality)gisResource.CreateFunctionality(typeof(IQueryFunctionality), null);
35                    //查询图层的id和名称
36                    queryFunct.GetQueryableLayers(nullout layerIDs, out layerNames);
37                    for (int i = 0; i < layerIDs.Length; i++)
38                    {
39                        //获取地图名称用来显示
40                        sbInfo.AppendFormat("<h4>Layer {0}</h4>Fields:<br/>", layerNames[i]);
41                        //查询图层的字段用来显示
42                        fields = queryFunct.GetFields(null, layerIDs[i]);
43                        foreach (string fld in fields)
44                        {
45                            sbInfo.AppendFormat("{0}<br/>", fld);
46                        }

47                        sbInfo.Append("<br>");
48                    }

49                }

50                sbInfo.Append("<br>");
51            }

52            
53            return sbInfo.ToString();
54        }

55    }

56}

57
6.接下来用QueryAttributesTask在做一个通过选择states图层的STATE_NAME进行属性查询的功能,也只有是管理员组的用户才能用这个功能。
7.在页面上添加Menu1、TaskResults1、MapResourceManager1,把MapResourceManager1的BuddyControl设为Menu1,在MapResourceManager1添加一个QueryAttributesTask1,把QueryAttributesTask1的TaskResultsContainers-BuddyControl的属性设为TaskResults1,点击QueryAttributesTask1的PredefinedQuery属性栏弹出对话框进行设置,设置如下:Resource Manager:MapResourceManager1;Resource:MapResourceItem0;Layer:states;Label Text:Name;Associated Field:STATE_NAME;Operator:=;勾上Show Pick List并且点击Get Sample Values按钮添加选择项。
8.接下在做一个查询人口大于3000000的国家功能,添加QueryAttributesTask2,TaskResultsContainers-BuddyControl的属性设为TaskResults1PredefinedQuery属性设置如下:Resource Manager:MapResourceManager1;Resource:MapResourceItem0;Layer:counties;Label Text:Name;Associated Field:pop1999;Operator:>;Default value:3000000。
9.这样功能就开发完成了可以运行查看一下效果,接下来要根据登录用户的权限进行功能的控制。

10.当用户属于管理员组的时候可以在Map1和Toc1中显示ushigh图层,当用户不属于管理员组是不显示ushigh图层,在Page_PreRenderComplete方法中添加如下代码:
  1//要隐藏的图层名称
  2        private string layerToHide = "ushigh";
  3
  4        protected void Page_PreRenderComplete(object sender, EventArgs e)
  5        {
  6            if (Session["user"== null)
  7            {
  8                Response.Redirect("Login.aspx");
  9            }

 10            else
 11            {
 12                Label1.Text = " 登录用户:" + Session["user"].ToString() + " 用户组:" + Session["type"].ToString();
 13            }

 14
 15            //如果用户不是管理员组的
 16            string ut = Session["type"].ToString();
 17            if ( ut!= "管理员")
 18            {
 19                //隐藏ushigh图层,在Map和Toc1中都不显示
 20                HideLayer(Map1, layerToHide);
 21            }

 22        }

 23
 24        //隐藏图层
 25        private void HideLayer(Map map, string layerName)
 26        {
 27            foreach (IMapFunctionality mapFunct in map.GetFunctionalities())
 28            {
 29                //根据名称获取图层id
 30                string layerId = GetLayerId(layerName, mapFunct);
 31                if (!String.IsNullOrEmpty(layerId))
 32                {
 33                    //设置图层不可见
 34                    mapFunct.SetLayerVisibility(layerId, false);
 35                }
   
 36            }

 37
 38            //查找页面的上Toc控件,并且去掉图层名为ushigh的点
 39            Toc tocCtrl = FindControlOfType(typeof(Toc), Page.Controls) as Toc;
 40            if (tocCtrl != null && tocCtrl.BuddyControl == map.ID)
 41            {
 42                foreach (TreeViewPlusNode resource in tocCtrl.Nodes)
 43                {
 44                    TreeViewPlusNode nodeToRemove = null;
 45
 46                    foreach (TreeViewPlusNode layerNode in resource.Nodes)
 47                    {
 48                        TreeViewPlusNode matchNode = FindNodeRecursive(layerNode, layerName);
 49                        if (matchNode != null)
 50                        {
 51                            nodeToRemove = matchNode;
 52                            break;
 53                        }

 54                    }

 55                    if (nodeToRemove != null)
 56                    {
 57                        nodeToRemove.Remove();
 58                    }

 59                        
 60                }

 61            }

 62        }

 63
 64        //查找TreeViewPlusNode
 65        private TreeViewPlusNode FindNodeRecursive(TreeViewPlusNode node, string nodeName)
 66        {
 67            if (node.Text == nodeName)
 68            {
 69                return node;
 70            }

 71            foreach (TreeViewPlusNode node2 in node.Nodes)
 72            {
 73                TreeViewPlusNode childNode = FindNodeRecursive(node2, nodeName);
 74                if (childNode != null)
 75                {
 76                    return childNode;
 77                }

 78                    
 79            }

 80            return null;
 81        }

 82
 83
 84        //根据图层名称获取图层的id
 85        private string GetLayerId(string layerName, IMapFunctionality mapFunctionality)
 86        {
 87            string layerId = String.Empty;
 88            string[] layerIDs, layerNames;
 89            //查询图层名称
 90            mapFunctionality.GetLayers(out layerIDs, out layerNames);
 91            for (int i = 0; i < layerIDs.Length; i++)
 92            {
 93                if (layerNames[i] == layerName)
 94                {
 95                    layerId = layerIDs[i];
 96                    break;
 97                }

 98            }

 99            return layerId;
100        }

101
102        //查询控件
103        public static Control FindControlOfType(Type type, ControlCollection controls)
104        {
105            foreach (Control ctl in controls)
106            {
107                if (type.IsInstanceOfType(ctl))
108                {
109                    return ctl;
110                }
 
111                else if (ctl.HasControls())
112                {
113                    Control childCtl = FindControlOfType(type, ctl.Controls);
114                    if (childCtl != null)
115                    {
116                        return childCtl;
117                    }

118                        
119                }

120            }

121            return null;
122        }

123
11.这样可以用不同的用户登录看看效果了。
12.接下来实现当用户属于管理员组的时候可以使用GetMapInfo按钮和QueryAttributesTask1,当用户不属于管理员组时不能使用GetMapInfo按钮和QueryAttributesTask1。具体代码如下:
Code
13.完成所有功能的编写,运行用不同用户登录测试效果。
原文地址:https://www.cnblogs.com/hll2008/p/1278935.html