站点地图学习笔记(一)

一、根据不同的角色来显示各自不同的站点地图。

   web.sitemap文件中,先用siteMapNode的roles属性设置各自角色自己的名称。

 

 <siteMapNode
      title="External Links"
      description="Links to external Websites"
      roles="RoleA,RoleB,RoleC">
      <siteMapNode
        title="Google"
        url="http://www.Google.com"
        description="The Google Website"
        roles="RoleA" />
      <siteMapNode
        title="Microsoft"
        url="http://www.Sina.com"
        description="The Sina Website"
        roles="RoleB" />
      <siteMapNode
        title="Yahoo"
        url="http://www.Yahoo.com"
        description="The Yahoo Website"
        roles="RoleC" />
    </siteMapNode>

 

在web.config文件中,一定要启用securityTrimmingEnabled="true",才可以达到不同角色看到不同的链接。

 

 

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <system.web>

    <authentication mode="Windows" />
    <roleManager enabled="true" />

    <siteMap defaultProvider="MySiteMapProvider">
      <providers>
        <add
          name="MySiteMapProvider"
          type="System.Web.XmlSiteMapProvider"
          securityTrimmingEnabled="true"
          siteMapFile="Web.sitemap" />

      </providers>
    </siteMap>

  </system.web>
</configuration>

在secutityroles.aspx中

 

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="securityroles.aspx.cs" Inherits="securityroles" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <style type="text/css">
    html
    {
       background-color:silver;
    }
    .role
    {
       background-color:white;
       float:left;
       width:300px;
       border:Solid 1px black;
       padding:10px;
    }
   
    </style>
</head>
<body >
    <form id="form1" runat="server" >
    <div class="role">
        <asp:CheckBoxList ID="CheckRolesList" runat="server" BorderWidth="1px">
            <asp:ListItem Selected="True">RoleA</asp:ListItem>
            <asp:ListItem Selected="True">RoleB</asp:ListItem>
            <asp:ListItem Selected="True">RoleC</asp:ListItem>
        </asp:CheckBoxList>&nbsp;
        <asp:Button ID="selectBtn" runat="server" Text="SelectRole" OnClick="Button1_Click" />
        </div>
     <div class="role">
         &nbsp;&nbsp;
         <asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1">
         </asp:TreeView>
         <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
     
     </div>
    </form>
</body>
</html>

 

在securityroles.aspx.cs

 

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class securityroles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            foreach (ListItem item in CheckRolesList.Items)
                if (!Roles.RoleExists(item.Text))
                {
                    Roles.CreateRole(item.Text);
                    Roles.AddUserToRole(User.Identity.Name, item.Text);
                }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (ListItem item in CheckRolesList.Items)
        {
            if (item.Selected)
            {
                if (!User.IsInRole(item.Text))
                    Roles.AddUserToRole(User.Identity.Name, item.Text);
            }
            else
            {
                if (User.IsInRole(item.Text))
                {
                    Roles.RemoveUserFromRole(User.Identity.Name, item.Text);
                }
            }
        }
        Response.Redirect(Request.Path);
    }
    void Page_PreRender()
    {
        foreach (ListItem item in CheckRolesList.Items)
            item.Selected = User.IsInRole(item.Text);
    }
}


 这样就可以达到安全修整的目的,

你可能感兴趣的:(学习笔记)