Answered by:
C# convert to VB

Question
-
User-2130600361 posted
Hi!
I have found this source and will play around with. But in my project i use vb.net. SO i have converted the following C# code:
-----------------------------------------------------<snip>--------------------------------------------
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" %> <%@ Import Namespace="System.Web.Configuration" %> <script runat="server"> private const string VirtualImageRoot = "~/"; string selectedFolderName; private void Page_Init() { UserRoles.DataSource = Roles.GetAllRoles(); UserRoles.DataBind(); UserList.DataSource = Membership.GetAllUsers(); UserList.DataBind(); if (IsPostBack) { selectedFolderName = ""; } else { selectedFolderName = Request.QueryString["selectedFolderName"]; } } private void Page_Load() { if (!IsPostBack) { PopulateTree(); } } private void Page_PreRender() { if (FolderTree.SelectedNode != null) { DisplayAccessRules(FolderTree.SelectedValue); SecurityInfoSection.Visible = true; } } private void PopulateTree() { // Populate the tree based on the subfolders of the specified VirtualImageRoot DirectoryInfo rootFolder = new DirectoryInfo(Server.MapPath(VirtualImageRoot)); TreeNode root = AddNodeAndDescendents(rootFolder, null); FolderTree.Nodes.Add(root); try { FolderTree.SelectedNode.ImageUrl = "/Simple/i/target.gif"; } catch { } } private TreeNode AddNodeAndDescendents(DirectoryInfo folder, TreeNode parentNode) { // Add the TreeNode, displaying the folder's name and storing the full path to the folder as the value... string virtualFolderPath; if (parentNode == null) { virtualFolderPath = VirtualImageRoot; } else { virtualFolderPath = parentNode.Value + folder.Name + "/"; } TreeNode node = new TreeNode(folder.Name, virtualFolderPath); node.Selected = (folder.Name == selectedFolderName); // Recurse through this folder's subfolders DirectoryInfo[] subFolders = folder.GetDirectories(); foreach(DirectoryInfo subFolder in subFolders) { if (subFolder.Name != "_controls" && subFolder.Name != "App_Data") { TreeNode child = AddNodeAndDescendents(subFolder, node); node.ChildNodes.Add(child); } } return node; // Return the new TreeNode } protected void FolderTree_SelectedNodeChanged(object sender, EventArgs e) { ActionDeny.Checked = true; ActionAllow.Checked = false; ApplyRole.Checked = true; ApplyUser.Checked = false; ApplyAllUsers.Checked = false; ApplyAnonUser.Checked = false; UserRoles.SelectedIndex = 0; UserList.SelectedIndex = 0; RuleCreationError.Visible = false; ResetFolderImageUrls(FolderTree.Nodes[0]); // Restore previously selected folder's ImageUrl. FolderTree.SelectedNode.ImageUrl = "/Simple/i/target.gif"; // Set the newly selected folder's ImageUrl. } private void ResetFolderImageUrls(TreeNode parentNode) { parentNode.ImageUrl = "/Simple/i/folder.gif"; // Recurse through this node's child nodes. TreeNodeCollection nodes = parentNode.ChildNodes; foreach (TreeNode childNode in nodes) { ResetFolderImageUrls(childNode); } } private void DisplayAccessRules(string virtualFolderPath) { if (!virtualFolderPath.StartsWith(VirtualImageRoot) || virtualFolderPath.IndexOf("..") >= 0) { throw new ApplicationException("An attempt to access a folder outside of the website directory has been detected and blocked."); } Configuration config = WebConfigurationManager.OpenWebConfiguration(virtualFolderPath); SystemWebSectionGroup systemWeb = (SystemWebSectionGroup)config.GetSectionGroup("system.web"); AuthorizationRuleCollection authorizationRules = systemWeb.Authorization.Rules; RulesGrid.DataSource = authorizationRules; RulesGrid.DataBind(); TitleOne.InnerText = "Rules applied to " + virtualFolderPath; TitleTwo.InnerText = "Create new rule for " + virtualFolderPath; } private void RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { AuthorizationRule rule = (AuthorizationRule)e.Row.DataItem; if (!rule.ElementInformation.IsPresent) { e.Row.Cells[3].Text = "Inherited from higher level"; e.Row.Cells[4].Text = "Inherited from higher level"; e.Row.CssClass = "odd"; } } } private string GetAction(AuthorizationRule rule) { return rule.Action.ToString(); } private string GetRole(AuthorizationRule rule) { return rule.Roles.ToString(); } private string GetUser(AuthorizationRule rule) { return rule.Users.ToString(); } private void DeleteRule(object sender, EventArgs e) { Button button = (Button)sender; GridViewRow item = (GridViewRow)button.Parent.Parent; string virtualFolderPath = FolderTree.SelectedValue; Configuration config = WebConfigurationManager.OpenWebConfiguration(virtualFolderPath); SystemWebSectionGroup systemWeb = (SystemWebSectionGroup)config.GetSectionGroup("system.web"); AuthorizationSection section = (AuthorizationSection)systemWeb.Sections["authorization"]; section.Rules.RemoveAt(item.RowIndex); config.Save(); } private void MoveUp(object sender, EventArgs e) { MoveRule(sender, e, "up"); } private void MoveDown(object sender, EventArgs e) { MoveRule(sender, e, "down"); } private void MoveRule(object sender, EventArgs e, string upOrDown) { upOrDown = upOrDown.ToLower(); if (upOrDown == "up" || upOrDown == "down") { Button button = (Button)sender; GridViewRow item = (GridViewRow)button.Parent.Parent; int selectedIndex = item.RowIndex; if ((selectedIndex > 0 && upOrDown == "up") || (upOrDown == "down")) { string virtualFolderPath = FolderTree.SelectedValue; Configuration config = WebConfigurationManager.OpenWebConfiguration(virtualFolderPath); SystemWebSectionGroup systemWeb = (SystemWebSectionGroup)config.GetSectionGroup("system.web"); AuthorizationSection section = (AuthorizationSection)systemWeb.Sections["authorization"]; // Pull the local rules out of the authorization section, deleting them from same: ArrayList rulesArray = PullLocalRulesOutOfAuthorizationSection(section); if (upOrDown == "up") { LoadRulesInNewOrder(section, rulesArray, selectedIndex, upOrDown); } else if (upOrDown == "down") { if (selectedIndex < rulesArray.Count - 1) { LoadRulesInNewOrder(section, rulesArray, selectedIndex, upOrDown); } else { // DOWN button in last row was pressed. Load the rules array back in without resorting. for (int x = 0; x < rulesArray.Count; x++) { section.Rules.Add((AuthorizationRule)rulesArray[x]); } } } config.Save(); } } } private void LoadRulesInNewOrder(AuthorizationSection section, ArrayList rulesArray, int selectedIndex, string upOrDown) { AddFirstGroupOfRules(section, rulesArray, selectedIndex, upOrDown); AddTheTwoSwappedRules(section, rulesArray, selectedIndex, upOrDown); AddFinalGroupOfRules(section, rulesArray, selectedIndex, upOrDown); } private void AddFirstGroupOfRules(AuthorizationSection section, ArrayList rulesArray, int selectedIndex, string upOrDown) { int adj; if (upOrDown == "up") adj = 1; else adj = 0; for (int x = 0; x < selectedIndex - adj; x++) { section.Rules.Add((AuthorizationRule)rulesArray[x]); } } private void AddTheTwoSwappedRules(AuthorizationSection section, ArrayList rulesArray, int selectedIndex, string upOrDown) { if (upOrDown == "up") { section.Rules.Add((AuthorizationRule)rulesArray[selectedIndex]); section.Rules.Add((AuthorizationRule)rulesArray[selectedIndex - 1]); } else if (upOrDown == "down") { section.Rules.Add((AuthorizationRule)rulesArray[selectedIndex + 1]); section.Rules.Add((AuthorizationRule)rulesArray[selectedIndex]); } } private void AddFinalGroupOfRules(AuthorizationSection section, ArrayList rulesArray, int selectedIndex, string upOrDown) { int adj; if (upOrDown == "up") adj = 1; else adj = 2; for (int x = selectedIndex + adj; x < rulesArray.Count; x++) { section.Rules.Add((AuthorizationRule)rulesArray[x]); } } private ArrayList PullLocalRulesOutOfAuthorizationSection(AuthorizationSection section) { // First load the local rules into an ArrayList. ArrayList rulesArray = new ArrayList(); foreach (AuthorizationRule rule in section.Rules) { if (rule.ElementInformation.IsPresent) { rulesArray.Add(rule); } } // Next delete the rules from the section. foreach (AuthorizationRule rule in rulesArray) { section.Rules.Remove(rule); } return rulesArray; } private void CreateRule(object sender, EventArgs e) { AuthorizationRule newRule; if (ActionAllow.Checked) newRule = new AuthorizationRule(AuthorizationRuleAction.Allow); else newRule = new AuthorizationRule(AuthorizationRuleAction.Deny); if (ApplyRole.Checked && UserRoles.SelectedIndex > 0) { newRule.Roles.Add(UserRoles.Text); AddRule(newRule); } else if (ApplyUser.Checked && UserList.SelectedIndex > 0) { newRule.Users.Add(UserList.Text); AddRule(newRule); } else if (ApplyAllUsers.Checked) { newRule.Users.Add("*"); AddRule(newRule); } else if (ApplyAnonUser.Checked) { newRule.Users.Add("?"); AddRule(newRule); } } private void AddRule(AuthorizationRule newRule) { string virtualFolderPath = FolderTree.SelectedValue; Configuration config = WebConfigurationManager.OpenWebConfiguration(virtualFolderPath); SystemWebSectionGroup systemWeb = (SystemWebSectionGroup)config.GetSectionGroup("system.web"); AuthorizationSection section = (AuthorizationSection)systemWeb.Sections["authorization"]; section.Rules.Add(newRule); try { config.Save(); RuleCreationError.Visible = false; } catch (Exception ex) { RuleCreationError.Visible = true; RuleCreationError.Text = "<div class=\"alert\"><br />An error occurred and the rule was not added. I saw this happen during testing when I attempted to create a rule that the ASP.NET infrastructure realized was redundant. Specifically, I had the rule <i>DENY ALL USERS</i> in one folder, then attempted to add the same rule in a subfolder, which caused ASP.NET to throw an exception.<br /><br />Here's the error message that was thrown just now:<br /><br /><i>" + ex.Message + "</i></div>"; } } </script> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <div id="main"> <div class="page_margins"> <div class="page"> <!-- #include file="_nav.aspx" --> <h1>Website Access Rules</h1> <br /> <p> Use this page to manage access rules for your Web site. Rules are applied to folders, thus providing robust folder-level security enforced by the ASP.NET infrastructure. Rules are persisted as XML in each folder's Web.config file. <i>Page-level security and inner-page security are not handled using this tool — they are handled using specialized code that is available to the Web Developers.</i> </p> <table> <tr> <td valign="top" style="padding-right: 30px;"> <div class="treeview"> <asp:TreeView runat="server" ID="FolderTree" OnSelectedNodeChanged="FolderTree_SelectedNodeChanged"> <RootNodeStyle ImageUrl="/Simple/i/folder.gif" /> <ParentNodeStyle ImageUrl="/Simple/i/folder.gif" /> <LeafNodeStyle ImageUrl="/Simple/i/folder.gif" /> <SelectedNodeStyle Font-Underline="true" ForeColor="#A21818" /> </asp:TreeView> </div> </td> <td valign="top" style="padding-left: 30px; border-left: 1px solid #999;"> <asp:Panel runat="server" ID="SecurityInfoSection" Visible="false"> <h2 runat="server" id="TitleOne" class="alert"></h2> <p> Rules are applied in order. The first rule that matches applies, and the permission in each rule overrides the permissions in all following rules. Use the Move Up and Move Down buttons to change the order of the selected rule. Rules that appear dimmed are inherited from the parent and cannot be changed at this level. </p> <asp:GridView runat="server" ID="RulesGrid" AutoGenerateColumns="false" CssClass="list" GridLines="none" OnRowDataBound="RowDataBound" > <Columns> <asp:TemplateField HeaderText="Action"> <ItemTemplate> <%# GetAction((AuthorizationRule)Container.DataItem) %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Roles"> <ItemTemplate> <%# GetRole((AuthorizationRule)Container.DataItem) %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="User"> <ItemTemplate> <%# GetUser((AuthorizationRule)Container.DataItem) %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Delete Rule"> <ItemTemplate> <asp:Button ID="Button1" runat="server" Text="Delete Rule" CommandArgument="<%# (AuthorizationRule)Container.DataItem %>" OnClick="DeleteRule" OnClientClick="return confirm('Click OK to delete this rule.')" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Move Rule"> <ItemTemplate> <asp:Button ID="Button2" runat="server" Text=" Up " CommandArgument="<%# (AuthorizationRule)Container.DataItem %>" OnClick="MoveUp" /> <asp:Button ID="Button3" runat="server" Text="Down" CommandArgument="<%# (AuthorizationRule)Container.DataItem %>" OnClick="MoveDown" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <hr /> <h2 runat="server" id="TitleTwo" class="alert"></h2> <b>Action:</b> <asp:RadioButton runat="server" ID="ActionDeny" GroupName="action" Text="Deny" Checked="true" /> <asp:RadioButton runat="server" ID="ActionAllow" GroupName="action" Text="Allow" /> <br /><br /> <b>Rule applies to:</b> <br /> <asp:RadioButton runat="server" ID="ApplyRole" GroupName="applyto" Text="This Role:" Checked="true" /> <asp:DropDownList ID="UserRoles" runat="server" AppendDataBoundItems="true"> <asp:ListItem>Select Role</asp:ListItem> </asp:DropDownList> <br /> <asp:RadioButton runat="server" ID="ApplyUser" GroupName="applyto" Text="This User:" /> <asp:DropDownList ID="UserList" runat="server" AppendDataBoundItems="true"> <asp:ListItem>Select User</asp:ListItem> </asp:DropDownList> <br /> <asp:RadioButton runat="server" ID="ApplyAllUsers" GroupName="applyto" Text="All Users (*)" /> <br /> <asp:RadioButton runat="server" ID="ApplyAnonUser" GroupName="applyto" Text="Anonymous Users (?)" /> <br /><br /> <asp:Button ID="Button4" runat="server" Text="Create Rule" OnClick="CreateRule" OnClientClick="return confirm('Click OK to create this rule.');" /> <asp:Literal runat="server" ID="RuleCreationError"></asp:Literal> </asp:Panel> </td> </tr> </table> </div> </div> </div> </asp:Content>
--------------------------------------------<snip>-----------------------------------------------------------
The above code is good running. No problems. but the following vb.net code is wrong.
--------------------------------------------<snip>-----------------------------------------------------------
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" %> <script runat="server"> Private Const VirtualImageRoot As String = "~/" Private selectedFolderName As String Private Sub Page_Init() UserRoles.DataSource = Roles.GetAllRoles() UserRoles.DataBind() UserList.DataSource = Membership.GetAllUsers() UserList.DataBind() If IsPostBack Then selectedFolderName = "" Else selectedFolderName = Request.QueryString("selectedFolderName") End If End Sub Private Sub Page_Load() If Not IsPostBack Then PopulateTree() End If End Sub Private Sub Page_PreRender() If FolderTree.SelectedNode IsNot Nothing Then DisplayAccessRules(FolderTree.SelectedValue) SecurityInfoSection.Visible = True End If End Sub Private Sub PopulateTree() ' Populate the tree based on the subfolders of the specified VirtualImageRoot Dim rootFolder As New DirectoryInfo(Server.MapPath(VirtualImageRoot)) Dim root As TreeNode = AddNodeAndDescendents(rootFolder, Nothing) FolderTree.Nodes.Add(root) Try FolderTree.SelectedNode.ImageUrl = "/Images/target.gif" Catch End Try End Sub Private Function AddNodeAndDescendents(ByVal folder As DirectoryInfo, ByVal parentNode As TreeNode) As TreeNode ' Add the TreeNode, displaying the folder's name and storing the full path to the folder as the value... Dim virtualFolderPath As String If parentNode Is Nothing Then virtualFolderPath = VirtualImageRoot Else virtualFolderPath = parentNode.Value + folder.Name & "/" End If Dim node As New TreeNode(folder.Name, virtualFolderPath) node.Selected = (folder.Name = selectedFolderName) ' Recurse through this folder's subfolders Dim subFolders As DirectoryInfo() = folder.GetDirectories() For Each subFolder As DirectoryInfo In subFolders If subFolder.Name <> "_controls" AndAlso subFolder.Name <> "App_Data" Then Dim child As TreeNode = AddNodeAndDescendents(subFolder, node) node.ChildNodes.Add(child) End If Next ' Return the new TreeNode Return node End Function Protected Sub FolderTree_SelectedNodeChanged(ByVal sender As Object, ByVal e As EventArgs) ActionDeny.Checked = True ActionAllow.Checked = False ApplyRole.Checked = True ApplyUser.Checked = False ApplyAllUsers.Checked = False ApplyAnonUser.Checked = False UserRoles.SelectedIndex = 0 UserList.SelectedIndex = 0 RuleCreationError.Visible = False ResetFolderImageUrls(FolderTree.Nodes(0)) ' Restore previously selected folder's ImageUrl. ' Set the newly selected folder's ImageUrl. FolderTree.SelectedNode.ImageUrl = "/Images/target.gif" End Sub Private Sub ResetFolderImageUrls(ByVal parentNode As TreeNode) parentNode.ImageUrl = "/Images/folder.gif" ' Recurse through this node's child nodes. Dim nodes As TreeNodeCollection = parentNode.ChildNodes For Each childNode As TreeNode In nodes ResetFolderImageUrls(childNode) Next End Sub Private Sub DisplayAccessRules(ByVal virtualFolderPath As String) If Not virtualFolderPath.StartsWith(VirtualImageRoot) OrElse virtualFolderPath.IndexOf("..") >= 0 Then Throw New ApplicationException("An attempt to access a folder outside of the website directory has been detected and blocked.") End If Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(virtualFolderPath) Dim systemWeb As SystemWebSectionGroup = DirectCast(config.GetSectionGroup("system.web"), SystemWebSectionGroup) Dim authorizationRules As AuthorizationRuleCollection = systemWeb.Authorization.Rules RulesGrid.DataSource = authorizationRules RulesGrid.DataBind() TitleOne.InnerText = "Rules applied to " & virtualFolderPath TitleTwo.InnerText = "Create new rule for " & virtualFolderPath End Sub Public Sub RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim rule As AuthorizationRule = DirectCast(e.Row.DataItem, AuthorizationRule) If Not rule.ElementInformation.IsPresent Then e.Row.Cells(3).Text = "Inherited from higher level" e.Row.Cells(4).Text = "Inherited from higher level" e.Row.CssClass = "odd" End If End If End Sub Public Function GetAction(ByVal rule As AuthorizationRule) As String Return rule.Action.ToString() End Function Private Function GetRole(ByVal rule As AuthorizationRule) As String Return rule.Roles.ToString() End Function Private Function GetUser(ByVal rule As AuthorizationRule) As String Return rule.Users.ToString() End Function Private Sub DeleteRule(ByVal sender As Object, ByVal e As EventArgs)
Dim button As Button = DirectCast(sender, Button) Dim item As GridViewRow = DirectCast(button.Parent.Parent, GridViewRow) Dim virtualFolderPath As String = FolderTree.SelectedValue Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(virtualFolderPath) Dim systemWeb As SystemWebSectionGroup = DirectCast(config.GetSectionGroup("system.web"), SystemWebSectionGroup) Dim section As AuthorizationSection = DirectCast(systemWeb.Sections("authorization"), AuthorizationSection) section.Rules.RemoveAt(item.RowIndex) config.Save() End Sub Private Sub MoveUp(ByVal sender As Object, ByVal e As EventArgs) MoveRule(sender, e, "up") End Sub Private Sub MoveDown(ByVal sender As Object, ByVal e As EventArgs) MoveRule(sender, e, "down") End Sub Private Sub MoveRule(ByVal sender As Object, ByVal e As EventArgs, ByVal upOrDown As String) upOrDown = upOrDown.ToLower() If upOrDown = "up" OrElse upOrDown = "down" Then Dim button As Button = DirectCast(sender, Button) Dim item As GridViewRow = DirectCast(button.Parent.Parent, GridViewRow) Dim selectedIndex As Integer = item.RowIndex If (selectedIndex > 0 AndAlso upOrDown = "up") OrElse (upOrDown = "down") Then Dim virtualFolderPath As String = FolderTree.SelectedValue Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(virtualFolderPath) Dim systemWeb As SystemWebSectionGroup = DirectCast(config.GetSectionGroup("system.web"), SystemWebSectionGroup) Dim section As AuthorizationSection = DirectCast(systemWeb.Sections("authorization"), AuthorizationSection) ' Pull the local rules out of the authorization section, deleting them from same: Dim rulesArray As ArrayList = PullLocalRulesOutOfAuthorizationSection(section) If upOrDown = "up" Then LoadRulesInNewOrder(section, rulesArray, selectedIndex, upOrDown) ElseIf upOrDown = "down" Then If selectedIndex < rulesArray.Count - 1 Then LoadRulesInNewOrder(section, rulesArray, selectedIndex, upOrDown) Else ' DOWN button in last row was pressed. Load the rules array back in without resorting. For x As Integer = 0 To rulesArray.Count - 1 section.Rules.Add(DirectCast(rulesArray(x), AuthorizationRule)) Next End If End If config.Save() End If End If End Sub Private Sub LoadRulesInNewOrder(ByVal section As AuthorizationSection, ByVal rulesArray As ArrayList, ByVal selectedIndex As Integer, ByVal upOrDown As String) AddFirstGroupOfRules(section, rulesArray, selectedIndex, upOrDown) AddTheTwoSwappedRules(section, rulesArray, selectedIndex, upOrDown) AddFinalGroupOfRules(section, rulesArray, selectedIndex, upOrDown) End Sub Private Sub AddFirstGroupOfRules(ByVal section As AuthorizationSection, ByVal rulesArray As ArrayList, ByVal selectedIndex As Integer, ByVal upOrDown As String) Dim adj As Integer If upOrDown = "up" Then adj = 1 Else adj = 0 End If For x As Integer = 0 To selectedIndex - adj - 1 section.Rules.Add(DirectCast(rulesArray(x), AuthorizationRule)) Next End Sub Private Sub AddTheTwoSwappedRules(ByVal section As AuthorizationSection, ByVal rulesArray As ArrayList, ByVal selectedIndex As Integer, ByVal upOrDown As String) If upOrDown = "up" Then section.Rules.Add(DirectCast(rulesArray(selectedIndex), AuthorizationRule)) section.Rules.Add(DirectCast(rulesArray(selectedIndex - 1), AuthorizationRule)) ElseIf upOrDown = "down" Then section.Rules.Add(DirectCast(rulesArray(selectedIndex + 1), AuthorizationRule)) section.Rules.Add(DirectCast(rulesArray(selectedIndex), AuthorizationRule)) End If End Sub Private Sub AddFinalGroupOfRules(ByVal section As AuthorizationSection, ByVal rulesArray As ArrayList, ByVal selectedIndex As Integer, ByVal upOrDown As String) Dim adj As Integer If upOrDown = "up" Then adj = 1 Else adj = 2 End If For x As Integer = selectedIndex + adj To rulesArray.Count - 1 section.Rules.Add(DirectCast(rulesArray(x), AuthorizationRule)) Next End Sub Private Function PullLocalRulesOutOfAuthorizationSection(ByVal section As AuthorizationSection) As ArrayList ' First load the local rules into an ArrayList. Dim rulesArray As New ArrayList() For Each rule As AuthorizationRule In section.Rules If rule.ElementInformation.IsPresent Then rulesArray.Add(rule) End If Next ' Next delete the rules from the section. For Each rule As AuthorizationRule In rulesArray section.Rules.Remove(rule) Next Return rulesArray End Function Private Sub CreateRule(ByVal sender As Object, ByVal e As EventArgs) Dim newRule As AuthorizationRule If ActionAllow.Checked Then newRule = New AuthorizationRule(AuthorizationRuleAction.Allow) Else newRule = New AuthorizationRule(AuthorizationRuleAction.Deny) End If If ApplyRole.Checked AndAlso UserRoles.SelectedIndex > 0 Then newRule.Roles.Add(UserRoles.Text) AddRule(newRule) ElseIf ApplyUser.Checked AndAlso UserList.SelectedIndex > 0 Then newRule.Users.Add(UserList.Text) AddRule(newRule) ElseIf ApplyAllUsers.Checked Then newRule.Users.Add("*") AddRule(newRule) ElseIf ApplyAnonUser.Checked Then newRule.Users.Add("?") AddRule(newRule) End If End Sub Private Sub AddRule(ByVal newRule As AuthorizationRule) Dim virtualFolderPath As String = FolderTree.SelectedValue Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(virtualFolderPath) Dim systemWeb As SystemWebSectionGroup = DirectCast(config.GetSectionGroup("system.web"), SystemWebSectionGroup) Dim section As AuthorizationSection = DirectCast(systemWeb.Sections("authorization"), AuthorizationSection) section.Rules.Add(newRule) Try config.Save() RuleCreationError.Visible = False Catch ex As Exception RuleCreationError.Visible = True RuleCreationError.Text = "<div class=""alert""><br />An error occurred and the rule was not added. I saw this happen during testing when I attempted to create a rule that the ASP.NET infrastructure realized was redundant. Specifically, I had the rule <i>DENY ALL USERS</i> in one folder, then attempted to add the same rule in a subfolder, which caused ASP.NET to throw an exception.<br /><br />Here's the error message that was thrown just now:<br /><br /><i>" & ex.Message & "</i></div>" End Try End Sub </script> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <div id="main"> <div class="page_margins"> <div class="page"> <!-- #include file="_nav.aspx" --> <h1>Website Access Rules</h1> <br /> <p> Use this page to manage access rules for your Web site. Rules are applied to folders, thus providing robust folder-level security enforced by the ASP.NET infrastructure. Rules are persisted as XML in each folder's Web.config file. <i>Page-level security and inner-page security are not handled using this tool — they are handled using specialized code that is available to the Web Developers.</i> </p> <table> <tr> <td valign="top" style="padding-right: 30px;"> <div class="treeview"> <asp:TreeView runat="server" ID="FolderTree" OnSelectedNodeChanged="FolderTree_SelectedNodeChanged"> <RootNodeStyle ImageUrl="/Images/folder.gif" /> <ParentNodeStyle ImageUrl="/Images/folder.gif" /> <LeafNodeStyle ImageUrl="/Images/folder.gif" /> <SelectedNodeStyle Font-Underline="true" ForeColor="#A21818" /> </asp:TreeView> </div> </td> <td valign="top" style="padding-left: 30px; border-left: 1px solid #999;"> <asp:Panel runat="server" ID="SecurityInfoSection" Visible="false"> <h2 runat="server" id="TitleOne" class="alert"></h2> <p> Rules are applied in order. The first rule that matches applies, and the permission in each rule overrides the permissions in all following rules. Use the Move Up and Move Down buttons to change the order of the selected rule. Rules that appear dimmed are inherited from the parent and cannot be changed at this level. </p> <asp:GridView runat="server" ID="RulesGrid" AutoGenerateColumns="false" CssClass="list" GridLines="none" OnRowDataBound="RowDataBound" > <Columns> <asp:TemplateField HeaderText="Action"> <ItemTemplate> <%# GetAction((AuthorizationRule)Container.DataItem) %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Roles"> <ItemTemplate> <%# GetRole((AuthorizationRule)Container.DataItem) %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="User"> <ItemTemplate> <%# GetUser((AuthorizationRule)Container.DataItem) %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Delete Rule"> <ItemTemplate> <asp:Button ID="Button1" runat="server" Text="Delete Rule" CommandArgument="<%# (AuthorizationRule)Container.DataItem %>" OnClick="DeleteRule" OnClientClick="return confirm('Click OK to delete this rule.')" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Move Rule"> <ItemTemplate> <asp:Button ID="Button2" runat="server" Text=" Up " CommandArgument="<%# (AuthorizationRule)Container.DataItem %>" OnClick="MoveUp" /> <asp:Button ID="Button3" runat="server" Text="Down" CommandArgument="<%# (AuthorizationRule)Container.DataItem %>" OnClick="MoveDown" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <hr /> <h2 runat="server" id="TitleTwo" class="alert"></h2> <b>Action:</b> <asp:RadioButton runat="server" ID="ActionDeny" GroupName="action" Text="Deny" Checked="true" /> <asp:RadioButton runat="server" ID="ActionAllow" GroupName="action" Text="Allow" /> <br /><br /> <b>Rule applies to:</b> <br /> <asp:RadioButton runat="server" ID="ApplyRole" GroupName="applyto" Text="This Role:" Checked="true" /> <asp:DropDownList ID="UserRoles" runat="server" AppendDataBoundItems="true"> <asp:ListItem>Select Role</asp:ListItem> </asp:DropDownList> <br /> <asp:RadioButton runat="server" ID="ApplyUser" GroupName="applyto" Text="This User:" /> <asp:DropDownList ID="UserList" runat="server" AppendDataBoundItems="true"> <asp:ListItem>Select User</asp:ListItem> </asp:DropDownList> <br /> <asp:RadioButton runat="server" ID="ApplyAllUsers" GroupName="applyto" Text="All Users (*)" /> <br /> <asp:RadioButton runat="server" ID="ApplyAnonUser" GroupName="applyto" Text="Anonymous Users (?)" /> <br /><br /> <asp:Button ID="Button4" runat="server" Text="Create Rule" OnClick="CreateRule" OnClientClick="return confirm('Click OK to create this rule.');" /> <asp:Literal runat="server" ID="RuleCreationError"></asp:Literal> </asp:Panel> </td> </tr> </table> </div> </div> </div> </asp:Content>
--------------------------------------------<snip>-----------------------------------------------------------
<ItemTemplate>
<%# GetAction((AuthorizationRule)Container.DataItem) %>
</ItemTemplate>
The red line is the error line when i use the vb.net code. In C# i have no error.
The error from compiler is:
BC30108: "AuthorizationRule" is a type, and so is not a valid expression.
Can you help me? Thanks you
petzifun
PS: code c# is from 4guysfromrolla ... i've it translated to vb.net
Thursday, September 3, 2009 9:07 AM
Answers
-
User397347636 posted
That's just a cast.
Use the following:
<%#GetAction(CType(Container.DataItem, AuthorizationRule))%>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 3, 2009 10:10 AM
All replies
-
User1052258516 posted
Try removing the "(AuthorizationRule)" from the code <%# GetAction((AuthorizationRule)Container.DataItem) %>
Thursday, September 3, 2009 9:55 AM -
User397347636 posted
That's just a cast.
Use the following:
<%#GetAction(CType(Container.DataItem, AuthorizationRule))%>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 3, 2009 10:10 AM -
User-2130600361 posted
Thanks a lot to all ... Thats was it ..
Thursday, September 3, 2009 11:10 AM