ASP.NET Tutorial/Configuration/AuthorizationSection — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:30, 26 мая 2010

Add user and role to AuthorizationRule

<%@ page language="C#" %>
<%@ import namespace="System.Web.Management" %>
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{
    string appPath = this.Request.ApplicationPath;    
    Configuration config = Configuration.GetConfigurationForUrl(appPath);
    AuthorizationSection authorization = config.Web.Authorization;
    AuthorizationRule rule = new AuthorizationRule();
    rule.Action = AuthorizationRuleAction.Allow;
    rule.Users.Add("TestUser");
    rule.Roles.Add("TestRole");
    authorization.Rules.Add(rule);
    config.Update();
}
    
</script>
<html>
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="Form1" runat="server">
    </form>
</body>
</html>


Bind datalist to Authorization Section in your configuration

<%@ page language="C#" %>
<%@ import namespace="System.Web.Management" %>
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{
    string appPath = this.Request.ApplicationPath;    
    Configuration config = Configuration.GetConfigurationForUrl(appPath);
    AuthorizationSection authorization = config.Web.Authorization;
    
    this.DL_Rules.DataSource = authorization.Rules;
    this.DL_Rules.DataBind();
}
    
</script>
<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form runat="server">
        <asp:datalist id="DL_Rules" runat="server">
            <itemtemplate>
                Action: <asp:literal id="Literal1" runat="server" text="<%# ((AuthorizationRule) Container.DataItem).Action %>"/>
                <br />
                Users: 
                <asp:repeater runat="server" DataSource="<%# ((AuthorizationRule) Container.DataItem).Users %>">
                    <itemtemplate><%# Container.DataItem %></itemtemplate>
                    <separatortemplate>-</separatortemplate>
                </asp:repeater>
                <br />
                Roles: 
                <asp:repeater runat="server" datasource="<%# ((AuthorizationRule) Container.DataItem).Roles %>">
                    <itemtemplate><%# Container.DataItem %></itemtemplate>
                    <separatortemplate>-</separatortemplate>
                </asp:repeater>
            </itemtemplate>
        </asp:datalist>
    </form>
</body>
</html>