ASP.NET Tutorial/Authentication Authorization/LoginView
Содержание
LoginView control controls who views what on a particular part of a page
<source lang="csharp">
<%@ 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></source>
Providing a view for a particular group
<source lang="csharp">
<%@ 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></source>
The LoginView can display different content to different users depending on their authentication status.
<source lang="csharp">
<%@ 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">
<asp:LoginStatus id="LoginStatus" Runat="server" />
<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>
</form>
</body> </html></source>
Using Roles with the LoginView Control
<source lang="csharp">
<%@ 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">
<asp:LoginStatus id="LoginStatus" Runat="server" />
<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>
</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></source>