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

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

Текущая версия на 11:57, 26 мая 2010

LoginView control controls who views what on a particular part of a page

<%@ Page Language="VB" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Changing the View</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:LoginStatus ID="LoginStatus1" Runat="server" />
        
        <asp:LoginView ID="LoginView1" Runat="server">
            <LoggedInTemplate>
                REALLY important 
            </LoggedInTemplate>
            <AnonymousTemplate>
                basic information
            </AnonymousTemplate>
        </asp:LoginView>
    </form>
</body>
</html>


Providing a view for a particular group

<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Changing the View</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:LoginStatus ID="LoginStatus1" Runat="server" />
        
        <asp:LoginView ID="LoginView1" Runat="server">
            <LoggedInTemplate>
                REALLY important 
            </LoggedInTemplate>
            <AnonymousTemplate>
                basic information 
            </AnonymousTemplate>
            <RoleGroups>
                <asp:RoleGroup Roles="Admins">
                    <ContentTemplate>
                       You are an Admin!
                    </ContentTemplate>
                </asp:RoleGroup>
                <asp:RoleGroup Roles="CoolPeople">
                    <ContentTemplate>
                       You are cool!
                    </ContentTemplate>
                </asp:RoleGroup>
            </RoleGroups>
        </asp:LoginView>
    </form>
</body>
</html>


The LoginView can display different content to different users depending on their authentication status.

<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
    <title>Show LoginView</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:LoginStatus
        id="LoginStatus"
        Runat="server" />
    <hr />
    <asp:LoginView
        id="LoginView1"
        Runat="server">
        <AnonymousTemplate>
        This content is displayed to anonymous users.
        </AnonymousTemplate>
        <LoggedInTemplate>
        This content is displayed to authenticated users.
        </LoggedInTemplate>
    </asp:LoginView>
    </div>
    </form>
</body>
</html>


Using Roles with the LoginView Control

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        MembershipCreateStatus status;
        Membership.CreateUser("Bill","secret_","bill@somewhere.ru","dog","rover",true, out status);
        
        Membership.CreateUser("Ted", "secret_", "ted@somewhere.ru", "dog", "rover", true,out status);
        
        Membership.CreateUser("Fred", "secret_", "fred@somewhere.ru", "dog", "rover", true, out status);
        
        if (!Roles.RoleExists("Administrator"))
        {
            Roles.CreateRole("Administrator");
            Roles.AddUserToRole("Bill", "Administrator");
        }
        
        if (!Roles.RoleExists("Manager"))
        {
            Roles.CreateRole("Manager");
            Roles.AddUserToRole("Bill", "Manager");
            Roles.AddUserToRole("Ted", "Manager");
        }
        
        if (!Roles.RoleExists("Worker"))
        {
            Roles.CreateRole("Worker");
            Roles.AddUserToRole("Fred", "Worker");
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>LoginView Roles</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:LoginStatus
        id="LoginStatus"
        Runat="server" />
    <hr />
    <asp:LoginView
        id="LoginView1"
        Runat="server">
        <RoleGroups>
        <asp:RoleGroup Roles="Administrator">
        <ContentTemplate>
        This content is displayed to Administrators.
        </ContentTemplate>
        </asp:RoleGroup>
        <asp:RoleGroup Roles="Manager,Worker">
        <ContentTemplate>
        This content is displayed to Managers
        and Workers.
        </ContentTemplate>
        </asp:RoleGroup>
        </RoleGroups>
    </asp:LoginView>
    </div>
    </form>
</body>
</html>
You must enable roles in the web configuration file. 
File: Web.Config
<configuration>
  <system.web>
    <authentication mode="Forms" />
    <roleManager enabled="true" />
  </system.web>
</configuration>